Selecting value from array results
Posted
by
Swodahs
on Stack Overflow
See other posts from Stack Overflow
or by Swodahs
Published on 2013-06-27T04:03:56Z
Indexed on
2013/06/27
4:21 UTC
Read the original article
Hit count: 105
Being new to learning PHP I am having trouble with understanding how to select/echo/extract a value from array result a API script returns.
Using the standard:
echo "<pre>";
print_r($ups_rates->rates);
echo "</pre>";
The results returned look like this:
Array
(
[0] => Array
(
[code] => 03
[cost] => 19.58
[desc] => UPS Ground
)
[1] => Array
(
[code] => 12
[cost] => 41.69
[desc] => UPS 3 Day Select
)
[2] => Array
(
[code] => 02
[cost] => 59.90
[desc] => UPS 2nd Day Air
)
)
If I am only needing to work with the values of the first array result: Code 3, 19.58, UPS Ground --- what is the correct way to echo one or more of those values?
I thought:
$test = $ups_rates[0][cost];
echo $test;
This is obviously wrong and my lack of understanding the array results isn't improving, can someone please show me how I would echo an individual value of the returned array and/or assign it to a variable to echo the normal way?
© Stack Overflow or respective owner