1
<?php

$ar = Array (

     [0] => Array ( [id] => 1 [social_id] => [role_id] => 0 [name] => [first_name] => Jimmy [last_name] => rathore [gender] => male [dob] => 06-02-1992 [language] => english [location] => xyz [address] => xyz [email] => [email protected] [phone] => 9876543210 )


     [1] => Array ( [id] => 3 [social_id] => [role_id] => 0 [name] => [first_name] => Dev [last_name] => Smith [gender] => [dob] => [language] => [location] => xyz [address] => xyz [email] => [email protected] [phone] => 7838344344 )

      [2] => Array ( [id] => 4 [social_id] => [role_id] => 0 [name] => [first_name] => Col [last_name] => Manon [gender] => male [dob] => 02-02-2017 [language] => english [location] => London [address] => ABCD [email] => [email protected] [phone] => 7894561230)
       ) ;


$array = Array (

     [0] => Array ( [id] => 1 [sm_id] => 1 [field_name] => first_name [value] => G2 [created_at] => 2017-02-24 11:05:03 [updated_at] => 2017-02-24 11:05:03 [deleted_at] => )
 );

$arr = array();

foreach($row=0; $row<count($ar); $row++){
    foreach($col=0; $col<count($array); $col++){
        if($ar['0']['id'] == $array['0']['id']){
            $arr['first_name'] = $ar['0']['first_name'];
            $arr['last_name'] = $ar['0']['last_name'];
            $arr['email'] = $ar['0']['email'];
            $arr['phone'] = $ar['0']['phone'];
        }
    }
}

print_r($arr);die;

?>

I want map data from one array to other array based on $ar['0]['id'] with $array['0']['sm_id']

if $ar's id matched with $array's sm_id push to one array with its value how can i achieve the same. please suggest

Expected result

Array( [id] => 1 [first_name] => Jimmy [last_name] => rathore [gender] => male [dob] => 06-02-1992 [language] => english [location] => xyz [address] => xyz [email] => [email protected] [phone] => 9876543210 );
2
  • I don't understand what you are asking. Please provide the expected output. Commented Feb 25, 2017 at 15:34
  • see now trincot Commented Feb 25, 2017 at 15:55

1 Answer 1

1

You could do it like this, given the original array is $ar, and the array with sm_id values $array:

// Create a hash of all sm_id values to be found
$ids = array_flip(array_column($array, "sm_id"));

// filter the original array to get only those with an id value in that hash
$arr = array_filter($ar, function ($row) use ($ids) {
    return isset($ids[$row["id"]]);
});

print_r ($arr);
Sign up to request clarification or add additional context in comments.

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.