matching id's of two array in php - php

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!

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

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;
}

Related

How to map a 2D array to 1D array in PHP

I can't figure out how to do the following array_map in php. Any help is much appreciated.
Input:
$arrayA = [
[
'slug' => 'bob',
'name' => 'Bob',
'age' => '10',
],
[
'slug' => 'alice',
'name' => 'Alice',
'age' => '15',
],
[
'slug' => 'carl',
'name' => 'Carl',
'age' => '17',
]
]
Desired Output:
$arrayB = [
'bob' => 'Bob',
'alice' => 'Alice',
'carl' => 'Carl'
]
What I have so far:
Here I am mapping to an array and I know it's not what I want but I can not figure out if there is some syntax for me to return just $x['slug'] => $x['name'] without the brackets?
$arrayB = array_map(fn($x) => [$x['slug'] => $x['name']], $arrayA);
My current output (not what I want):
$arrayB = [
[ 'bob' => 'Bob' ],
[ 'alice' => 'Alice' ],
[ 'carl' => 'Carl' ]
];
There is a PHP function that can do exactly what you want: array_column()
$arrayA = [
[
'slug' => 'bob',
'name' => 'Bob',
'age' => '10',
],
[
'slug' => 'alice',
'name' => 'Alice',
'age' => '15',
],
[
'slug' => 'carl',
'name' => 'Carl',
'age' => '17',
]
];
$arrayB = array_column($arrayA, 'name', 'slug');
That will give you:
Array
(
[bob] => Bob
[alice] => Alice
[carl] => Carl
)
Here's a demo: https://3v4l.org/LGcES
With array_map you cant generate a array with the schema what you want. Use instead array_map foreach. like that:
<?php
$arrayA = [
[
'slug' => 'bob',
'name' => 'Bob',
'age' => '10',
],
[
'slug' => 'alice',
'name' => 'Alice',
'age' => '15',
],
[
'slug' => 'carl',
'name' => 'Carl',
'age' => '17',
]
];
$arrayB = [];
foreach($arrayA as $i) {
$arrayB[$i['slug']] = $i['name'];
}
print_r($arrayB);
// output:
/* Array (
[bob] => Bob
[alice] => Alice
[carl] => Carl
)

How to group PHP arrays by key before sorting by another key?

I have an array that contains the key counted and placement which I am trying to group by before I sort. The array should be first sorted by counted and then, for each duplicate counted, should then sort by placement.
$array = [
[
'id' => 1,
'placement' => 8,
'counted' => 3
'user' => ['name' => 'foo'],
],
[
'id' => 2,
'placement' => 5,
'counted' => 3
'user' => ['name' => 'bar'],
],
[
'id' => 3,
'placement' => 1,
'counted' => 2
'user' => ['name' => 'foobar'],
]
];
My expected output here would be:
$array = [
[
'id' => 2,
'placement' => 5,
'counted' => 3
'user' => ['name' => 'bar'],
],
[
'id' => 1,
'placement' => 8,
'counted' => 3
'user' => ['name' => 'foo'],
],
[
'id' => 3,
'placement' => 1,
'counted' => 2
'user' => ['name' => 'foobar'],
]
];
I have tried to usort to achieve this:
usort($array, fn($a, $b) => ((int)$a['placement'] <=> (int)$b['counted']) * -1);
But this gives me an unexpected result. Everything I try seems to not work, any ideas would be appreciated.
Since you prefer using usort so this is my answser
$array = [
[
'id' => 1,
'placement' => 8,
'counted' => 3,
'user' => ['name' => 'foo'],
],
[
'id' => 2,
'placement' => 5,
'counted' => 3,
'user' => ['name' => 'bar'],
],
[
'id' => 3,
'placement' => 1,
'counted' => 2,
'user' => ['name' => 'foobar'],
]
];
usort($array, function ($a, $b) {
if ($a['counted'] < $b['counted']) {
return 1;
}
if ($a['counted'] === $b['counted'] && $a['placement'] > $b['placement']) {
return 1;
}
});
If you don’t care about efficiency, you can write like this
collect($array)
->sortByDesc('counted')
->groupBy('counted')
->map(function ($group) {
return $group->sortBy('placement');
})
->flatten(1)
->toArray()

How to transform nested array to a flat array of nodes PHP?

I have a nested array with a structure as detailed below.
It is an org chart where an employ could have other employees related:
$tree_array = [
[
'id' => 1,
'employee' => 'John',
'leader_id' => NULL,
'team' => [
[
'id' => 2,
'employee' => 'Maria',
'leader_id' => 1,
'team' => [],
],
[
'id' => 3,
'employee' => 'Kevin',
'leader_id' => 1,
'team' => [
[
'id' => 4,
'employee' => 'Alan',
'leader_id' => 3,
'team' => [],
],
[
'id' => 5,
'employee' => 'Bret',
'leader_id' => 3,
'team' => [],
],
],
],
],
]
];
Every node has an ID and a team, that could be an array of other nodes and so on.
I need to obtain each node in a flat array, like this structure:
$flat_array = array(
array(
'id' => 1,
'employee' => 'John',
'leader_id' => NULL
),
array(
'id' => 2,
'employee' => 'Maria',
'leader_id' => 1
),
array(
'id' => 3,
'employee' => 'Kevin',
'leader_id' => 1
),
...
);
I've tried to implement a recursive function, but I get only the nodes of the last iteration.
Any help?
You can make a recursive function, like this:
function flatten($arr, $final=array()) {
foreach($arr as $a) {
$tmp = $a;
unset($tmp['team']);
$final[]= $tmp;
if ($a['team'] && count($a['team']) > 0) $final = flatten($a['team'], $final);
}
return $final;
}
$flat =flatten($tree_array);
Test it here: https://www.tehplayground.com/UHWCccFIkrS5v75Z

Search in multidimensional array by several values

I have an array which I'm sure there are some duplicate values in it, I want to search in this array and find the duplicate values and return the key of that array.
let me explain with an example, this is my array:
[
0 => [
'name' => 'name0',
'family' => 'family0',
'email' => 'email0#sample.com',
'rate' => 10
],
1 => [
'name' => 'name1',
'family' => 'family1',
'email' => 'email1#sample.com',
'rate' => 4
],
2 => [
'name' => 'name0',
'family' => 'family0',
'email' => 'email0#sample.com',
'rate' => 6
]
];
Now, I want to search in this array by name, family, and email at the same time and return the key of the parent (in this example 0 and 2). because I want to create a new array like this :
[
0 => [
'name' => 'name0',
'family' => 'family0',
'email' => 'email0#sample.com',
'rate' => [
10,
6
]
],
1 => [
'name' => 'name1',
'family' => 'family1',
'email' => 'email1#sample.com',
'rate' => [
4
]
],
];
How can I do this in PHP?
You can use array-reduce and use the 3 similar fields as keys.
Define a function who create the key and set or add rate:
function combineRate($carry, $item) {
$k = implode('###', array($item['name'], $item['family'], $item['email']));
if (isset($carry[$k]))
$carry[$k]['rate'][] = $item['rate'];
else {
$item['rate'] = [$item['rate']];
$carry[$k] = $item;
}
return $carry;
}
Call it with empty array:
$res = array_values(array_reduce($a, 'combineRate', array()));
Live example: 3v4l

Sort multi-dimensional array so specific sub-array is first

I want to sort an array so that a specific array with a specific value is shown as the first in the array.
The array I have:
array = [
[0] => [
'id' => 123,
'name' => 'Random'
],
[1] => [
'id' => 156,
'name' => 'keyboard'
],
[2] => [
'id' => 12235,
'name' => 'Text'
],
];
I want the sub-array where the name is 'keyboard' to be the first in line of the big array.
Does anyone have suggestions?
usort Sort an array by values using a user-defined comparison function
$array = [
0 => [
'id' => 123,
'name' => 'Random'
],
1 => [
'id' => 156,
'name' => 'keyboard'
],
2 => [
'id' => 12235,
'name' => 'Text'
],
];
usort($array, function ($item) {
return $item['name'] != 'keyboard';
});
print_r($array);
See the demo
$myArray = [
[0] => [
'id' => 123,
'name' => 'Random'
],
[1] => [
'id' => 156,
'name' => 'keyboard'
],
[2] => [
'id' => 12235,
'name' => 'Text'
],
];
$temp = $myArray[0];
$myArray[0] = $myArray[1];
$myArray[1] = $temp;

Categories