2

I'm a bit confused with the array that I have to work with. The following array:

print_r($myArray);

returns the following:

Array (
    [0] => stdClass Object (
        [id] => 88
        [label] => Bus
    )
    [1] => stdClass Object (
        [id] => 89
        [label] => Bike
    )
    [2] => stdClass Object (
        [id] => 90
        [label] => Plane
    )
    [3] => stdClass Object (
        [id] => 91
        [label] => Submaine
    )
    [4] => stdClass Object (
        [id] => 92
        [label] => Boat
    )
    [5] => stdClass Object (
        [id] => 93
        [label] => Car
    )
    [6] => stdClass Object (
        [id] => 94
        [label] => Truck
    )
) 

How do I get the label value, say, "Submaine", if I have the $id = 91?

2
  • If you can change the way the array is constructed, you should index the items with the "id", that will be easier. Then you can just do $item = $myArray[$id]; Commented May 15, 2011 at 20:04
  • A class is creating this array, so unfortunately I have to work with what I've got. But you're right, I wish I could just call by ID. Commented May 15, 2011 at 20:09

3 Answers 3

7

This will get you the object(s) you seek:

$objects = array_filter($myArray, function($item){ return $item->id == 91 })

Then it's just a matter of getting the attribute of the object that you want.

Sign up to request clarification or add additional context in comments.

7 Comments

Those are objects, so it would be return $item->id == 91
+1 For suggesting array_filter rather than the obvious array looping.
onteria_ is correct, but AJ's answer is the best from three available now (the part mentioned by onteria_ is the only issue here).
It's a lovely solution, just far slower.
@AJ Well, yes, but that isn't exactly unlikely, is it? If we're assuming that the last item is the one we want, the fastest solution is array_pop! (I'm being facetious, but still...)
|
3

You're going to have to loop through the array, I think.

$value = '';
foreach ($myArray as $el) {
    if ($el->id === 91) { // or other number
        $value = $el->label;
        break;
    }
}

The label is now contained in $value.


Benchmark values vs AJ's version for 1000000 iterations (see source):

lonesomeday: 1.8717081546783s
AJ: 4.0924150943756s
James C: 2.9421799182892s

2 Comments

+1 for posting a benchmark, this is very interesting ! What did you use for benchmarking ?
@Matthieu A quick script I wrote myself -- see the link in the answer.
2

What you have there is an array of objects. I'd suggest re-keying the array by id like this:

$new = array();
foreach($array as $obj) {
    $new[ $obj->id ] = $new[ $obj->label ];
}

Now you've got a nice associative array that you can use normally e.g. echo $new[92] will echo "Boat"

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.