views:

41

answers:

4

How do i check if a specific array key exist and how to compare them?

1. Array looks like this.

[33] => Array
               (
                   [211] =>objectr
                       (
                           [name] => Test
                           [id]=>  211
                    )
               )

[23] => Array
               (
                   [311] =>objectr
                       (
                           [name] => Tester
                           [id]=>  311
                    )
               )

2. Array looks like this

   [0] => 311
   [1] => 211
   [2] => 99

Now i need to compare them and get the id of them.

What im looking for is something like that

   [0] => Tester
   [1] => Test

How do i do that?

+2  A: 

I would transform Array 1 like removing the outer key (at least temporarily) then while iterating through Array 2, i'd compare against transformed Array 1 with array_key_exists.

fabrik
+2  A: 

array_key_exists - http://php.net/manual/en/function.array-key-exists.php

foreach($first_array as $arr) {
    foreach($second_array as $key=>$val)
    {
        if (array_key_exists($val, $first_array)) {
            $final_array[$key] = $arr['name'];
        }
    }
}

or array_search - http://uk.php.net/array_search

foreach($first_array as $arr) {
    foreach($second_array as $val)
    {
        $key = array_search($val, $arr);
        if($key !== false) $final_array[$key] = $arr['name'];
    }
}

In both cases you should end up with:

   [0] => Tester
   [1] => Test
Lizard
+1  A: 

I hope I understood your question, there might be a language barrier, but here we go:

so basically you have 3 arrays and you want to use the last to to check against the first one to see if those values/keys exists in the first? Well the firs thing you want to do is re structure your first array into something that can easily be translated for checking the values and keys of the next two arrays. so lets call the first array $mapArray:

foreach($mapArray as $mapObject){
    foreach($mapObject as $object){
        $mapList[$object->id] = $object->name;
    }
}

Now this should give us something like:

[211] => 'test'
[311] => 'tester'

So now lets call the 2nd array $arrayIds and the 3rd $arrayNames. To see if am id exists and to get its name when given the array $arrayIds, all you need to do is this:

//given [0] => 311
$keyExists = array_key_exists(311, $mapList); //returns true is the key exists
echo $mapList[311]; //returns tester, the name for the id given

And the other way around:

//given [0] => 'test'
$nameExists = in_array('test', $mapList);
if($nameExists) echo array_search('test', $mapList); // returns 211

hope this is what you are looking for or at least helps you find what you are looking for.

Angel S. Moreno
+1  A: 

Another approach: We reduce the first array to one dimension:

$first = array();

foreach($first_array as $val) {
   $first[key($val)] = current($val);
}

gives:

Array
(
    [211] => Array
        (
            [name] => Test
            [id] => 211
        )

    [311] => Array
        (
            [name] => Tester
            [id] => 311
        )

)

(I used an array instead of an object but it works the same).

and then we compute the intersection of the keys:

//assume
$second_array = array(311, 99);

$result = array_intersect_key($first, array_flip($second_array));

which gives:

Array
(
    [311] => Array
        (
            [name] => Tester
            [id] => 311
        )

)

So it is not quite what you want but you can easily access the name property via $element->name.

Felix Kling