0

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.

Im0rtality
  • 3,463
  • 3
  • 31
  • 41
M Arfan
  • 4,384
  • 4
  • 29
  • 46

3 Answers3

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

Im0rtality
  • 3,463
  • 3
  • 31
  • 41
  • HELL NO! Why the f would he wanna do that? Just to make his array even bigger? This is THE MOST shittiest advice! How could the structure break? THAT IS CLEARLY ONE RESULT FROM DB... that will NEVER be a duplicate because of customer_id, man! Impossible! – Ares Draguna Nov 11 '14 at 15:34
  • Try to figure out why there's zero AND named indices 2 weeks later. Then talk about shitty advices. Memory footprint is not big enough to worry. – Im0rtality Nov 11 '14 at 15:37
  • Regardless! There is ABSOLUTELY NO NEED for him to do this... Why would he do it like that? Convince me, PLEASE!! – Ares Draguna Nov 11 '14 at 15:47
  • Look from maintainability perspective. Plus, if you care this much for such tiny memory usage increase, you shouldn't be using PHP at all. – Im0rtality Nov 11 '14 at 15:58
  • You shouldn't use PHP for what? For anything? :)) where do you get this answers man? =)) – Ares Draguna Nov 11 '14 at 16:00
  • You are sacrificing readability for premature optimization. After designing, implementing and maintaining several systems, developers tend to stop doing that. By doing that optimization you win less than one megabyte RAM (~120 bytes per array @PHP5.4) in normal case. I'd like to see case, where running PHP (instead of something more efficient, lower level) is worthwhile. – Im0rtality Nov 11 '14 at 19:45
  • I really don't understand you... how can 1 extra dimension in an array can optimize the array? show me where you read this, please! – Ares Draguna Nov 12 '14 at 09:03
  • exactly... the bigger the array, the more memory it consumes... where is the optimization? Please give me the material that you read, so I can be informed and next time I will not say it again. Until then, I'll stick with what I know... You add extra dimension, the array consumes more. Sorry... – Ares Draguna Nov 12 '14 at 09:49
  • Read again. I am not talking about how to SAVE memory. – Im0rtality Nov 12 '14 at 12:13
  • So... let me get this straight... you want to sacrifice memory usage just to say `$customer['customers'][0]['customer_id']` which DOES NOT MAKE ANY GOD DAMN SENCE?? If you want it like that, it should be `$customer['customer_info']['customer_id']`, this regarding to readability but CERTAINLY NOT the way you put it... It is not more readable that the original way... Sorry... – Ares Draguna Nov 12 '14 at 12:47
0
$customer_id = $customer['customer']['customer_id']
user1187347
  • 318
  • 3
  • 12
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – NKN Nov 12 '14 at 10:45
  • I'm pretty sure it provided an answer to the question when it was asked..! – user1187347 Nov 12 '14 at 10:53
  • You can't see it in the edits of the question, but if you look at the edits from @Im0rtality's answer, you can see that the original question was changed. Having seen that someone else was answering the question, I decided to leave them to it and therefore missed the fact that the code in the question changed. Thanks, though. – user1187347 Nov 12 '14 at 11:04
0

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

Ares Draguna
  • 1,641
  • 2
  • 16
  • 32