0

I have an array of dictionnaries like:

$arr = array(
    array(
        'id' => '1',
        'name' => 'machin',
    ),
    array(
        'id' => '2',
        'name' => 'chouette',
    ),
);

How can I find the name of the array containing the id 2 (chouette) ?

Am I forced to reindex the array ?


Thank you all, aparently I'm forced to loop through the array (what I wanted to avoid), I thought that it were some lookup fonctions like Python. So I think I'll reindex with id.

6 Answers 6

3

Just find the index of array that contains the id you want to find. SO has enough questions and answers on this topic available.

Assuming you have a big array with lots of data in your real application, it might be too slow (for your taste). In this case, you indeed need to modify the structure of your arrays, so you can look it up faster, e.g. by using the id as an index for the name (if you are only interested in the name).

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

Comments

2

As a for loop would be the best way to do this, I would suggest changing you array so that the id is the arrays index. For example:

$arr = array(
    1 => 'machin',
    2 => 'chouette',
);

This way you could just get the name for calling $arr[2]. No looping and keeping your program running in linear time.

1 Comment

Yeah that's what I wanted to avoid but I seem to have to do it that way
1
$name;
foreach ($arr as $value){
    if ( $value['id'] == 2  ){
        $name = $value['name'];
        break;
    }
}

Comments

1

I would say that it might be very helpful to reindex the information. If the ID is unique try something like this:

$newarr = array();
for($i = 0;$i < count($arr);$i++){ $newarr[$arr[$i]['id']] = $arr[$i]['name']; }

The result would be:

$newarr = array('1'=>'machin','2'=>'chouette');

Then you can go trough the array with "foreach" like this:

foreach($newarr as $key => $value){
    if($value == "machin"){
        return $key;
    }
}

But of course the same would work with your old array:

foreach($arr as $item){
    if($item['name'] == "machin"){
        return $item['id'];
    }
}

It depends on what you are planning to do with the array ;-)

1 Comment

The OP is interested to find the name for a given id... So after using your reindex method on the array, you could just use $newarr['1'] to get machin as a result.
0

array_key_exist() is the function to check for keys. foreach will help you get down in the multidimensional array. This function will help you get the name element of an array and let you specify a different id value.

function findKey($bigArray, $idxVal) {
    foreach($bigArray as $array) {
        if(array_key_exists('id', $array) && $array['id'] == $idxVal) {
            return $array['name'];
        }
    }
    return false;
}

//Supply your array for $arr
print(findKey($arr, '2')); //"chouette"

Comments

0

It's a bit crude, but this would get you the name...

$name = false;
foreach($arr as $v) {
    if($v['id'] == '2') {
         $name = $v['name'];
         break;
    }   
}
echo $name;

So no, you are not forced to reindex the array, but it would make things easier.

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.