简体   繁体   English

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

[英]How to read 2D array values in php

How to get value of array using key? 如何使用键获取数组的值?

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
)

I can get msg value using 我可以使用以下方式获取msg

$msg = $customer['msg']

How can I get value of customer_id , vendor_id , first_name etc. 我如何获取customer_idvendor_idfirst_name等的值。

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

You need to access via other key (0th element of array). 您需要通过其他键(数组的第0个元素)进行访问。 This operation gives you another array, to which you apply same logic again ( customer_id or whatever this time). 此操作为您提供了另一个数组,您可以再次对它应用相同的逻辑( customer_id或这次)。

EDIT: If you have control of structure, I suggest changing it a little bit to prevent breaking things accidentally due to usage of number based indices AND string based indices: 编辑:如果您具有结构的控制权,我建议您对其进行一些更改,以防止由于使用基于数字的索引和基于字符串的索引而意外破坏东西:

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

Then you access via $customer['customers'][0]['customer_id'] 然后,您可以通过$customer['customers'][0]['customer_id']

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

Actually, to get those values you have to do it like this: 实际上,要获得这些值,您必须这样做:

$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']; 

because key (offset) of first array is 0 因为第一个数组的键(偏移量)为0

hope this helps! 希望这可以帮助! :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM