2

i have 2 array $dizi1 and $dizi2. And these are like this.

$dizi1 = [
[
  'id' => 1,
  'name' => 'özkan',
  'surname' => 'özdemir',
],
[
    'id' => 2,
    'name' => 'çağrı',
    'surname' => 'uğurel',
],
[
    'id' => 3,
    'name' => 'can',
    'surname' => 'tokay'
],
[
    'id' => 4,
    'name' => 'lütfü',
    'surname' => 'uzun'
]
];

$dizi2 = [
[
    'id' => 2,
    'birthday' => 1993
],
[
    'id' => 3,
    'birthday' => 1990
],
[
    'id' => 4,
    'birthday' => 1989
],
[
    'id' => 1,
    'birthday' => 1987
]
];

and this is what i want

istenenDizi = [
[
    'id' => 1,
    'name' => 'özkan',
    'surname' => 'özdemir',
    'birthday' => 1987,
]
];

i reseaxrh a lot bu i cant find a algortihm to do this. I will also create two excel table and i am gonna use this. Can you please help me how can i do this? Thanksss!

3
  • use foreach, where are the codes? Commented May 8, 2018 at 8:01
  • These are all codes. I printed it as Json. Commented May 8, 2018 at 8:02
  • thats it? not even a smidge of trying with foreach? by the way you don't need an algorithm, you can just start with a loop Commented May 8, 2018 at 8:04

2 Answers 2

3

You can use foreach(), array_search(),array_column() like below:-

$istenenDizi = [];

foreach($dizi1 as $dizi1){

  $istenenDizi[$dizi1['id']] = $dizi1;
  $istenenDizi[$dizi1['id']]['birthday'] = $dizi2[array_search($dizi1['id'],array_column($dizi2,'id'))]['birthday'];
}
$istenenDizi = array_values($istenenDizi);

print_r($istenenDizi);

Output:-https://eval.in/1000838

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

1 Comment

Thank u so much :)
2

As you second array unsorted, you can sort it first. Then combine the two array by items. Demo.

usort($dizi2, function($a, $b){ return $a['id'] > $b['id'];});
foreach($dizi1 as $k=>$v){
    $v['birthday'] = $dizi2[$k]['birthday'];
    $result[] = $v;
}

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.