Shashank
Shashank

Reputation: 462

compare elements of array and based on comparison retrieve array data

I have two arrays, there is one element which is 'name' common in first and second array. Now, I want to retrieved values from second array if first array value match to second one.

code for first array:

    $rs = array();
    foreach ( $ex_array as $data ) {

                $rs[] = array( 'name' => $data['name'] );

            }

Second Array:

$entries_data = array();
foreach ( $array as $entry ) {

            $name = $entry['name']['value'];
            $email = $entry['email']['value'];

            $entries_data[] = array(
                'name' => $name,
                'email' => $email
            );
        }

Problem is, there is only multiple names in first array, and then i have to compare first array names with the second one array, if there is match then whole data is retrieved from second array for specific name. I am trying to do this by using in_array function for search names in second array but can't fetch whole values. Any suggestions or help would be grateful for me.

Upvotes: 0

Views: 87

Answers (1)

Essam Elmasry
Essam Elmasry

Reputation: 1252

is_array() is used for 1d arrays which isnt ur case use this function taken from the php documentation comments and edited by me to work for ur example

function in_multiarray($elem, $array)
    {
        $top = sizeof($array) - 1;
        $bottom = 0;
        while($bottom <= $top)
        {
            if($array[$bottom]['name'] == $elem)
                return true;
            else 
                if(is_array($array[$bottom]['name']))
                    if(in_multiarray($elem, ($array[$bottom]['name'])))
                        return true;

            $bottom++;
        }        
        return false;
    }

Upvotes: 1

Related Questions