Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following array:

array(15) { 
    [0]=> object(stdClass)#317 (2) { ["id"]=> string(1) "2" ["value"]=> string(1) "1" } 
    [1]=> object(stdClass)#316 (2) { ["id"]=> string(1) "3" ["value"]=> string(531) "awfaww" }   
    [2]=> object(stdClass)#315 (2) { ["id"]=> string(1) "4" ["value"]=> string(1) "1" } 
    [3]=> object(stdClass)#318 (2) { ["id"]=> string(1) "5" ["value"]=> string(1) "1" } 
    [4]=> object(stdClass)#319 (2) { ["id"]=> string(1) "6" ["value"]=> string(1) "1" } 
    [5]=> object(stdClass)#320 (2) { ["id"]=> string(1) "7" ["value"]=> string(1) "1" } 
    [6]=> object(stdClass)#321 (2) { ["id"]=> string(1) "8" ["value"]=> string(1) "1" } 
    [7]=> object(stdClass)#322 (2) { ["id"]=> string(2) "30" ["value"]=> string(8) "12:30:02" }
    [8]=> object(stdClass)#323 (2) { ["id"]=> string(2) "31" ["value"]=> string(8) "18:12:00" }
    [9]=> object(stdClass)#324 (2) { ["id"]=> string(2) "11" ["value"]=> string(10) "2014-06-17" } 
    [10]=> object(stdClass)#325 (2) { ["id"]=> string(2) "12" ["value"]=> string(10) "2014-06-26" } 
    [11]=> object(stdClass)#326 (2) { ["id"]=> string(2) "14" ["value"]=> string(1) "2" }       
    [12]=> object(stdClass)#327 (2) { ["id"]=> string(2) "15" ["value"]=> string(1) "2" } 
    [13]=> object(stdClass)#328 (2) { ["id"]=> string(2) "16" ["value"]=> string(1) "4" } 
    [14]=> object(stdClass)#329 (2) { ["id"]=> string(2) "17" ["value"]=> string(1) "5" } 
} 

I would like to get a specific value from this array using the ID. For example, if the ID: 11 is found in the array I want to retrieve its value. How can I do this?

share|improve this question
1  
@Leri First of all it's "Stare" and second, take your non-constructive comments elsewhere, thanks. –  johnnyTrak Jun 2 at 11:44
    
@johnnyTrak It's perfectly constructive and really provides a hint, how to solve the problem. :) And thanks for noticing typo. –  Leri Jun 2 at 11:45
    
@Leri from what I know about this page its not about providing hints, but useful answers and I dont really find yours very useful. –  MONZTAAA Jun 2 at 11:46
    
@MONZTAAA Solutions go as answers. Asking for additional information, giving hints and linking to resources goes in comments. ;) –  Leri Jun 2 at 11:48
    
@Leri as I said, check the first comment, thats a hint/information, not yours. –  MONZTAAA Jun 2 at 11:49

3 Answers 3

Try something like this:

<?php
function findById($array, $id) {
    foreach ($array as $value) {
        if ($value->id == $id) {
            return $value->value;
        }
    }
    return null;
}
$result = findById($yourArray, 11);
?>
share|improve this answer

if the array is static for each run - i'd suggest changing the array into "key"=>"value" array, using this:

$new_arr = array();
foreach($original_array as $object) {
    $new_arr[$object->id] = $object->value;
}

and then you can just use $new_arr[id], instead of searching the whole original array each time.

share|improve this answer

You can use array_filter:

array_filter($arr, function($i) { return $i->id == '11'; });

See Documentation

share|improve this answer
    
The good thing about php arrays is that they are implemented as hashtables and since id is assumed to be unique, id in object can/should become a key in array so access to it can be O(1) instead of O(n). The latter is the case for array_filter –  Leri Jun 2 at 12:00
    
Well, apparently that's not the case here since the id doesn't match the key. –  haim770 Jun 2 at 12:03
    
Yes, however, I think that should have been noted for future readers who potentially have ability to change data-structure. –  Leri Jun 2 at 12:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.