Cain Nuke
Cain Nuke

Reputation: 3083

Get the value of a key in array

I have a variable and when I output it with print_r like this:

print_r($sort_order[$field->name]);

I get this:

Array ( [0] => Array ( [sort_order] => 92 ) ) 

but I only need the value which is 92. How can I do so it outputs only that when echoing it? example:

echo $sort_order[$field->name];

should output simple

92

Upvotes: 1

Views: 51

Answers (3)

mhall
mhall

Reputation: 3701

Your $sortOrder array is actually an array of arrays, like:

[
    [ 'sort_order' => 92 ]
]

That's why you can't print it like you expect.

Try:

echo $sort_order[0]['sort_order'];

Output:

92

Upvotes: 1

rst
rst

Reputation: 2714

The command print_r displays the variable in a human readable way. So if you need to know all the info in a variable (in particular for large arrays), then you use that. For other use, e.g. when you only need to know the content (in I guess 99.999% of all the cases) you should either use echo as you already mentioned it or print (althoug, they are more or less the same).

Please consider this links for futher information

http://php.net/manual/en/function.print-r.php

What's the difference between echo, print, and print_r in PHP?

Upvotes: 0

Navneet
Navneet

Reputation: 4813

The print_r() function is used to print human-readable information about a variable.

You can do both print and echo to output the required value:

echo $sort_order[$field->name];

print $sort_order[$field->name];

Hope this helps.

Upvotes: 1

Related Questions