0

I have an array that outputs these values:

Array ( 
    [0] => stdClass Object ( 
        [ID] => 6585
        [COLOR] => red 
        [Name] => steve
        ) 
    [1] => stdClass Object ( 
        [ID] => 5476 
        [COLOR] => blue 
        [Name] => sol
        ) 
    [2] => stdClass Object ( 
        [ID] => 7564 
        [COLOR] => yellow 
        [Name] => jake 
        ) 
    [3] => stdClass Object ( 
        [ID] => 3465 
        [COLOR] => green 
        [Name] => helen 
        ) 
    )

Now, I will know the ID of the person, and I need the get the COLOR value for that specific value set. How is this best achieved please?

3
  • How are you getting the data into this format? The preferred method would be to have your ID as the keys in the array so you could reference it with $arr[$id]->COLOR, but that won't be possible with the current format. Commented Mar 12, 2012 at 16:44
  • It's given like that, which means I can make them array keys either. Also it's already in a function so we can't have a function in a function? Commented Mar 12, 2012 at 16:49
  • Please select the answer that helped you most. Commented Jul 3, 2012 at 9:14

4 Answers 4

4

Something like this:

function getColorById($arr, $id){
    foreach($arr as $item){
        if ($item->ID == $id)
            return $item->COLOR;
    }
    return "blah!";
}

Use like echo getColorById($arr, 3465);

EDIT: The way you have the data leads to slowing down access times. A better suggestion is to (since it seems ID is unique), you'd rather have that as the key to your array. You have integer indices to it, now. Construct the array (unless you are receiving it from some area beyond what you have control over) something like the below:

$arr = array();
$arr["ID_4634"] = <object>;
Sign up to request clarification or add additional context in comments.

4 Comments

This solution would work, but would be much slower than necessary, especially as the size of the data would grow. The data should probably be reformatted to query the arrays in the manner that he needs them to.
Yes! You are correct, I thought its unnecessary to point that out, since others have done so. Or wait, I will update my post. Thanks :)
Hi, it's already in a function so we can't have a function in a function, can we?
Yes, you can. But I would suggest keep it outside. It improves readability, and allows other functions to use it, unless you have your reasons not to.
2

You probably want change the way you are storing it up for faster access.

With the current format, you will need to loop through the elements and compare colors until you find a match.

for($i=0;$i<count($array);$i++){
  if($array[$i]['ID'] == $id)
    return $array[$i]['COLOR']
}

alternatively, store it with the ID's as the keys.

2 Comments

This searches by color, not by the 'ID'
@Chris oh missed that :p fixed.
1
$users = array();
$count = count($arr);
for ($i = 0; $i < $count; $i++) {
    $users[$arr[$i]['ID']] = array(
        'Name' => $arr[$i]['Name'],
        'COLOR' => $arr[$i]['COLOR']
    );
}
echo $users[$id]['COLOR'];

where $arr represents the array you outputted in your initial post and $id is the id of the person who's color you're trying to access. You can also then get their name by using

echo $users[$id]['Name'];

Comments

0

IDTry below :

for($i=0; $i<count($array); $i++)
{
  if($array[$i]['ID'] == $id)
    return $array[$i]['COLOR'];
}

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.