如何在php中读取2D数组值

时间:2014-11-11 15:18:17

标签: php arrays json

如何使用密钥获取数组的值?

Array
(
    Array
    (
        [customer_id] => 98
        [vender_id] => 4
        [first_name] => Arfan
        [last_name] => Ali
        [email] => arfan427@gmail.com
        [mobile_number] => 0303030
        [address] => this is a test address
        [password] => cc03e747a6afbbcbf8be7668acfebee5
        [device_token] => 0
        [created_at] => 2014-11-11 14:46:47
    )
    [status] => 1
    [msg] => customer has been founded
)

我可以使用

获得msg
$msg = $customer['msg']

如何获得customer_idvendor_idfirst_name等的价值。

3 个答案:

答案 0 :(得分:0)

$customer_id = $customer[0]['customer_id']; // outputs 98

您需要通过其他键(数组的第0个元素)进行访问。此操作为您提供了另一个数组,您可以再次应用相同的逻辑(customer_id或此时)。

编辑: 如果您对结构有控制权,我建议稍微更改一下,以防止由于使用基于数字的索引和基于字符串的索引而意外破坏:

Array
(
    [customers] => Array
    (
        [customer_id] => 98
        [vender_id] => 4
        ...
    )
    [status] => 1
    [msg] => customer has been founded
)

然后您可以通过$customer['customers'][0]['customer_id']

进行访问

答案 1 :(得分:0)

$customer_id = $customer['customer']['customer_id']

答案 2 :(得分:0)

实际上,要获得这些值,您必须这样做:

$customer_id = $customer[0]['customer_id'];
$vender_id = $customer[0]['vender_id'];
$first_name = $customer[0]['first_name']; 
$last_name = $customer[0]['last_name']; 
$email = $customer[0]['email']; 
$mobile_number = $customer[0]['mobile_number']; 
$address = $customer[0]['address']; 
$password = $customer[0]['password']; 
$device_token= $customer[0]['device_token']; 
$created_at= $customer[0]['created_at']; 

因为第一个数组的键(偏移量)是0

希望这有帮助! :d