0

I'm trying to use a specific object type from a JSON feed, and am having a hard time specifying it. Using the code below I grab and print the specific array (max) I want,

$jsonurl = "LINK";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json,true);
$max_output = $json_output["max"];

echo '<pre>';
print_r($max_output);
echo '</pre>';

And from the Array below, all I want to work with is the [1] objects in each array. How can I specify and get just those values?

Array
(
[0] => Array
    (
        [0] => 1309924800000
        [1] => 28877
    )

[1] => Array
    (
        [0] => 1310011200000
        [1] => 29807
    )

[2] => Array
    (
        [0] => 1310097600000
        [1] => 33345
    )

[3] => Array
    (
        [0] => 1310184000000
        [1] => 33345
    )

[4] => Array
    (
        [0] => 1310270400000
        [1] => 33345
    )

[5] => Array
    (
        [0] => 1310356800000
        [1] => 40703
    )
1

4 Answers 4

1

Well you could fetch those values with array_map:

$max_output = array_map(function($val) { return $val[1]; }, $json_output["max"]);

This requires PHP 5.3, if you use an earlier version, then you can use create_function to achieve similar results:

$max_output = array_map(create_function('$val', 'return $val[1];'), $json_output["max"]);
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for array_map, which, IMHO, is a more semantically appropriate solution than looping. It's worth noting that when I've benchmarked array_map vs. foreach performance on controlled tasks the loop is usually a fair amount faster than the array_map call. This is anecdotal evidence, though, so take it with a grain of salt and run your own benchmarks for oft-executed code blocks.
This did exactly what I needed. Now $max_output shows only the objects I wanted. Huge thanks!
@user1209756 Then you should accept the answer! (click the checkbox and make it green)
@rdlowrey: thank you very much for the comment. A quick google-session turns out that there are other benchmarks which support your point. I guess if the input size is large enough it's definitely better to use foreach. But for small arrays it's just a matter of taste (meaning, the difference won't be significant).
1

When you need to create new array which will contain only second values, you may use either foreach loop which will create it or use array_map() (just for fun with anonymous function available since php 5.3.0):

$newArray = array_map( function( $item){
    return $item[1]
},$array);

Then you want to use last ("max" -> considering array with numeric keys) item, you can use end():

return end( $item);

And when you can process your data sequentially (eg. it's not part of some big getData() function) you can rather use foreach:

foreach( $items as $key => $val){
    echo $val[1] . " is my number\n";
}

Comments

0

After you get $max_output...

for( $i = 0; $i < length( $max_output ); $i++ ) {
    $max_output[$i] = $max_output[$i][1];
}

Comments

0

try this:

$ones = array();
foreach ($max_output as $r)
    $ones[] = $r[1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.