简体   繁体   中英

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 = $customer['msg']

How can I get value of customer_id , vendor_id , first_name etc.

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

You need to access via other key (0th element of array). This operation gives you another array, to which you apply same logic again ( customer_id or whatever this time).

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_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

hope this helps! :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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