【问题标题】:php array get single value [duplicate]php数组获取单个值[重复]
【发布时间】:2017-11-29 18:45:39
【问题描述】:

我有一个数组:

print_r($response);

将显示:

    Response: Array
(
    [errors] => false
    [return] => Array
        (
            [price_recurring] => 0
            [country_code] => NL
            [invoicemethod] => instant
            [product_id] => 0
            [affiliate] => 
            [number_of_periods] => 1
           [description] => Betaling voor eventid 274
            [period_duration] => 1 month
            [date] => 29/11/2017 19:42
            [order_quantity] => 1
            [vat] => 21
            [amount_total] => 4599
            [total_paused_days] => 0
            [custom] => WEB1511980957x194237
            [emailaddress] => ik@ik.nl
            [amount_affiliate_initial] => 0
            [total] => 45,99 
            [current_status] => completed
            [amount_affiliate_recurring] => 0
            [amount_total_affiliate] => 0
            [id] => 1790226
            [price_initial] => 4599
            [current_number_of_periods] => 1
            [firstname] => 
        )

)

如何提取“自定义”值?

我试过了:

$response = array_shift($response); 
echo $response['custom'];  

但它不会显示任何内容。我在这里做错了什么?我知道我很接近。

【问题讨论】:

  • 跟着缩进看层次结构echo $response['return']['custom'];

标签: php arrays


【解决方案1】:

$response 是一个包含两个键 errorsreturn 的数组。 $response['return'] 又是一个包含多个键的数组,包括custom。因此,要访问custom,需要引用$response['return']['custom']

【讨论】:

    【解决方案2】:

    数组中有一个数组

    $response["return"]["custom"]
    

    在您尝试导航时也尝试使用 var_dump(或 print_r 取决于您的喜好)而不是回显

    var_dump( $response["custom"]);
    

    除了回显 null 之外,它会告诉您更多信息,它什么也不打印

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      • 2017-10-17
      • 1970-01-01
      相关资源
      最近更新 更多