I have a multidimensional indexed array. Each element is an associative array with an id column which is unique between elements (its value never repeats within the array).
[indexed] =>Array
(
[0] => Array
(
[id] => john
[name] => John
[age] => 29
),
[1] => Array
(
[id] => peter
[name] => Peter
[age] => 30
),
[2] => Array
(
[id] => harry
[name] => Harry
[age] => 19
)
)
My goal is to convert this array into a multidimensional associative array, indexed by id values.
[indexed] =>Array
(
[john] => Array
(
[id] => john
[name] => John
[age] => 29
),
[peter] => Array
(
[id] => peter
[name] => Peter
[age] => 30
),
[harry] => Array
(
[id] => harry
[name] => Harry
[age] => 19
)
)
My best attempt so far is to loop over array elements and manually create the final array.
$associative = array();
foreach($indexed as $key=>$val) $associative[$val['id']] = $val;
I think it's not the most elegant solution. Is it possible to obtain the same result with built-in (more efficient) functions?
The truth is php DOES offer a single, native function that allows you to replace the outer indexes with the values of a single column. The "magic" is in the 2nd parameter which tells php not to touch the subarray values when assigning the new keys.
Code: (Demo)
$indexed = [
['id' => 'john', 'name' => 'John', 'age' => 29],
['id' => 'peter', 'name' => 'Peter', 'age' => 30],
['id' => 'harry', 'name' => 'Harry', 'age' => 19],
];
var_export(array_column($indexed, null, 'id'));
Output:
array (
'john' =>
array (
'id' => 'john',
'name' => 'John',
'age' => 29,
),
'peter' =>
array (
'id' => 'peter',
'name' => 'Peter',
'age' => 30,
),
'harry' =>
array (
'id' => 'harry',
'name' => 'Harry',
'age' => 19,
),
)
This even works on an array of objects. The end result is an array of objects with new, associative first-level keys. (Demo)
$indexed = [
(object)['id' => 'john', 'name' => 'John', 'age' => 29],
(object)['id' => 'peter', 'name' => 'Peter', 'age' => 30],
(object)['id' => 'harry', 'name' => 'Harry', 'age' => 19],
];
var_export(array_column($indexed, null, 'id'));
Output:
array (
'john' =>
(object) array(
'id' => 'john',
'name' => 'John',
'age' => 29,
),
'peter' =>
(object) array(
'id' => 'peter',
'name' => 'Peter',
'age' => 30,
),
'harry' =>
(object) array(
'id' => 'harry',
'name' => 'Harry',
'age' => 19,
),
)
Here is another way of doing it (assuming $arr is your original array):
$associative = array_combine(array_map(function($item) { return $item['id']; }, $arr), $arr);
But I think using a foreach is still shorter and more readable compare to this.
Related
This question already has answers here:
Sort multidimensional array based on another array [duplicate]
(3 answers)
Closed 26 days ago.
Hi I have an array that looks like this
array[
0 => array[
'id' => 1,
'name' => 'Test 1'
'classId' => 3
],
1 => array[
'id' => 1,
'name' => 'Test 1'
'classId' => 15
],
2 => array[
'id' => 1,
'name' => 'Test 1'
'classId' => 17
],
]
And I have another array that contains classIds like:
classIds = [15, 17, 3]
And I want to sort my array based on classIds
I can do a a double loop to compare it. I am just wondering is there anyother way to get it done?
Actually one loop i enough:
<?php
$order = [15, 17, 3];
$input = [
[
'id' => 1,
'name' => 'Test 1',
'classId' => 3,
],
[
'id' => 1,
'name' => 'Test 1',
'classId' => 15,
],
[
'id' => 1,
'name' => 'Test 1',
'classId' => 17,
],
];
$output = [];
array_walk($input, function($entry) use ($order, &$output) {
$output[array_search($entry['classId'], $order)] = $entry;
});
ksort($output);
print_r($output);
In case you consider array_search(...) also a "loop" (though it internally works different), that would be an alternative which produces the same output:
<?php
$order = array_flip([15, 17, 3]);
$input = array_values([
[
'id' => 1,
'name' => 'Test 1',
'classId' => 3,
],
[
'id' => 1,
'name' => 'Test 1',
'classId' => 15,
],
[
'id' => 1,
'name' => 'Test 1',
'classId' => 17,
],
]);
$output = [];
array_walk($input, function($entry, $index) use ($order, &$output) {
$output[$order[$entry['classId']]] = $entry;
});
ksort($output);
print_r($output);
The output of both approaches is:
Array
(
[0] => Array
(
[id] => 1
[name] => Test 1
[classId] => 15
)
[1] => Array
(
[id] => 1
[name] => Test 1
[classId] => 17
)
[2] => Array
(
[id] => 1
[name] => Test 1
[classId] => 3
)
)
You can sort the array directly and in-place using usort and an anonymous comparison function that retrieves the target indices from $classIds.
Given
<?php
$arr = array(
0 => array(
'id' => 1,
'name' => 'Test 1',
'classId' => 3
),
1 => array(
'id' => 1,
'name' => 'Test 1',
'classId' => 15
),
2 => array(
'id' => 1,
'name' => 'Test 1',
'classId' => 17
),
);
$classIds = [15, 17, 3];
you can sort $arr with
// Create assoc array mapping classIds to their desired sorted position.
$classIdsOrder = array_combine($classIds, range(0, count($classIds)-1));
// Sort $arr according to the 'classId' order described by $classIdsOrder
usort($arr, function ($left, $right) use ($classIdsOrder) {
return $classIdsOrder[$left['classId']] <=> $classIdsOrder[$right['classId']];
});
print_r($arr);
which outputs
Array
(
[0] => Array
(
[id] => 1
[name] => Test 1
[classId] => 15
)
[1] => Array
(
[id] => 1
[name] => Test 1
[classId] => 17
)
[2] => Array
(
[id] => 1
[name] => Test 1
[classId] => 3
)
)
Try it online!
I am using php 7.1.
I have seen that to eliminate the duplicate elements it is enough with this
array_unique($array, SORT_REGULAR);
I've also seen this work
array_map("unserialize", array_unique(array_map("serialize", $array)));
But that only deletes the elements that are duplicated from the array, I want to delete those that are duplicated but I don't want it to leave me only 1 without a duplicate, I want it to also delete that original on which it has been based to verify that it is duplicated
How could I do it?
For example i have this
$array = array(
[0] = array(
[id] => 1,
[number] => 12345,
[date] => 2022-05-09
)
[1] = array(
[id] => 2,
[number] => 123456,
[date] => 2022-05-09
)
[2] = array(
[id] => 3,
[number] => 123456,
[date] => 2022-05-09
)
[3] = array(
[id] => 3,
[number] => 123456,
[date] => 2022-05-09
)
)
How can i let it become this:?
$array = array(
[0] = array(
[id] => 1,
[number] => 12345,
[date] => 2022-05-09
)
[1] = array(
[id] => 2,
[number] => 123456,
[date] => 2022-05-09
)
)
This should be straightforward. Pluck all IDs using array_column and use array_count_values to get counts of occurrences of each ID. Then, use array_filter to filter only unique ones.
<?php
$unique_ids = array_count_values(array_column($array,'id'));
$res = array_filter($array, fn($v) => $unique_ids[$v['id']] === 1);
print_r($res);
Online Demo
Implementing the advice from How to remove values from an array if occurring more than one time?, for best time complexity, keep a lookup array of previously encountered values and their index. When a value is encountered more than once, delete the current and original row from the input array. It is perfectly safe to call unset() on an element that has already been unset(), no breakage will occur.
I have extended your input array to demonstrate that unset() will not cause trouble. I am using "array destructuring" in the foreach() to make the code more concise.
Code: (Demo)
$array = [
['id' => 1, 'number' => 12345, 'date' => '2022-05-09'],
['id' => 2, 'number' => 123456, 'date' => '2022-05-09'],
['id' => 3, 'number' => 123456, 'date' => '2022-05-09'],
['id' => 3, 'number' => 123456, 'date' => '2022-05-09'],
['id' => 4, 'number' => 123457, 'date' => '2022-05-10'],
['id' => 4, 'number' => 123458, 'date' => '2022-05-11'],
['id' => 3, 'number' => 123459, 'date' => '2022-05-12']
];
$found = [];
foreach ($array as $index => ['id' => $id]) {
if (isset($found[$id])) {
unset($array[$index], $array[$found[$id]]);
} else {
$found[$id] = $index;
}
}
var_export($array);
Output:
array (
0 =>
array (
'id' => 1,
'number' => 12345,
'date' => '2022-05-09',
),
1 =>
array (
'id' => 2,
'number' => 123456,
'date' => '2022-05-09',
),
)
This question already has answers here:
Merge two arrays containing objects and remove duplicate values
(7 answers)
Closed 5 months ago.
in my symfony project, I need to be able to merge several arrays of objects while removing duplicates.
For example :
array 1 :
Array
(
[0] => Absence Object
(
[id] => 1
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 12-11-2019 00:00:00
)
[1] => Absence Object
(
[id] => 2
[type] => TypeConge Object ([id] => 5, [nom] => "CA")
[user] => User Object (......)
[debut] => 13-11-2019 00:00:00
)
[2] => Absence Object
(
[id] => 3
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 11-11-2019 00:00:00
)
)
Array 2:
Array
(
[0] => Absence Object
(
[id] => 1
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 12-11-2019 00:00:00
)
[1] => Absence Object
(
[id] => 8
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 17-11-2019 00:00:00
)
)
output:
Array
(
[0] => Absence Object
(
[id] => 1
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 12-11-2019 00:00:00
)
[1] => Absence Object
(
[id] => 2
[type] => TypeConge Object ([id] => 5, [nom] => "CA")
[user] => User Object (......)
[debut] => 13-11-2019 00:00:00
)
[2] => Absence Object
(
[id] => 3
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 11-11-2019 00:00:00
)
[3] => Absence Object
(
[id] => 8
[type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
[user] => User Object (......)
[debut] => 17-11-2019 00:00:00
)
)
I use this code to do it :
$demandes = $this->getDemandesValidateur($unUser, $search, $superValidation);
$tabDemandes = array_merge($tabDemandes, $demandes);
$tabDemandes = array_map("unserialize", array_unique(array_map("serialize", $tabDemandes)));
But I get the impression that it causes bugs because of serialization and deserialization. I get the good final table, but some data seems unusable. For example I can not call:
$absence->getType()->getNom();
It returns null sometimes. Is it because of my code ?
My case :
To clarify my case, here's what I'm supposed to do:
I have an empty board at the beginning.
Then I make a loop to retrieve other tables that I merge. And in the end, I remove the duplicates.
It looks like this:
case "ROLE_SUPPLEANT":
$tabDemandes = [];
$users = $this->repoUsers->getUsersWhereIsSuppleant($user);
foreach($users as $unUser)
{
$demandes = array_column($this->getDemandesCongesForCalendar($unUser, $demandesConges, "ROLE_VALIDATEUR"), null, 'id');
$tabDemandes = $tabDemandes + $demandes;
}
if($user->hasRole("ROLE_VALIDATEUR"))
{
$demandes = array_column($this->getDemandesCongesForCalendar($user, $demandesConges, "ROLE_VALIDATEUR"), null, 'id');
$tabDemandes = $tabDemandes + $demandes;
}
break;
Knowing that the function getDemandesCongesForCalendar () returns an array of objects (which themselves contain objects).
I have the impression of having misused the code that you advised me, because it will not remove duplicates I think at the end.
The variable $ requests will in any case contain an array with unique values, there will be no duplicates.
But since each time, I add its values to the general table ($ tabDemands), there may be duplicates in the latter. And that's where I should step in to remove duplicates
Based on splash58's comment, you can use array_column() and then merge arrays while keeping only the first item. A sample can be seen here.
<?php
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
)
);
$records2 = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
),
array(
'id' => 5624,
'first_name' => 'Peter',
'last_name' => 'Doe',
)
);
$first = array_column($records, null, 'id');
$second = array_column($records2, null, 'id');
$third = $first + $second;
// if you want to reset indexes
// $third = array_values($third);
echo print_r($third);
You can do something like this:
$result = [];
$array1 = [
[
"id" => 1,
"other_data" => "text1"
],
[
"id" => 2,
"other_data" => "text2"
],
[
"id" => 3,
"other_data" => "text3"
],
];
$array2 = [
[
"id" => 1,
"other_data" => "text1"
],
[
"id" => 8,
"other_data" => "text8"
],
];
$merge_array = function($array, $result) {
foreach($array as $element) {
$id = $element["id"]; //In your case it could be $element->getId()
if(!isset($result[$id])) {
$result[$id] = $element;
}
}
return $result;
};
$result = $merge_array($array1, $result);
$result = $merge_array($array2, $result);
print_r(array_values($result));
My Code was :
$data = array();
foreach ($table as $key => $var) {
$data[] = ['id' => $var->id, 'value' => $var->designation];
}
My Data array should be like this
array (
0 => array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 22,
'value' => 'laravel developer',
),
3 => array (
'id' => 23,
'value' => 'laravel casts',
),
4 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
I need only one value i tried all the php core library function output:
array (
0 =>
array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 23,
'value' => 'laravel casts',
),
3 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
Based on the name only i need to remove duplicate bacause in my search bar it shows repeated mode.
You can use array_unique, wich saves indexes, and get the result by array_intersect_key
$temp = array_unique(array_column($arr, 'value'));
$res = array_intersect_key($arr, $temp);
print_r($res);
I got this array. I want all the other 3 array come into this [0] => Array. Don't want unique value just want to merge all array flat in to [0] => Array.
Array
(
[0] => Array
(
[0] => Array
(
[Campaign] => xxx
[Phone] => 111
[State] => cd
)
)
[1] => Array
(
[0] => Array
(
[Campaign] => zxxxzx
[Phone] => 111111
[State] => zxxx
)
)
[2] => Array
(
[0] => Array
(
[Campaign] => aaaa
[Phone] => 111
[State] => Csd
)
)
[3] => Array
(
[0] => Array
(
[Campaign] => sasa
[Phone] => 111
[State] => asas
)
)
)
This is another example of how important the naming is. What you are working with is basically:
$recordsGroups = array(
// first group:
array(
// record 1:
array(
'key1' => 'val1',
'key2' => 'val2',
),
// record 2:
array(
'key1' => 'aaa',
'key2' => 'bbb',
),
),
// 2nd group:
array(
// record 3:
array(
'key1' => 'ccc',
'key2' => 'ddd',
),
),
);
And what you are probably trying to do is:
$records = array();
foreach ($recordsGroups as $group)
foreach ($group as $record)
$records[] = $record;
Which will give you:
$records = array(
// record 1:
array(
'key1' => 'val1',
'key2' => 'val2',
),
// record 2:
array(
'key1' => 'aaa',
'key2' => 'bbb',
),
// record 3:
array(
'key1' => 'ccc',
'key2' => 'ddd',
),
);
This should do nicely:
$array = call_user_func_array('array_merge', $array);
Or Argument unpacking via ... (splat operator):
$array = array_merge(...$array);
Because arrays can't have duplicate keys, the best that this multi-dim array can be condensed is down to an indexed array of associative arrays. array_column() will make quick work of this task.
Code: (Demo)
$array=[
[
['Campaign'=>'xxx','Phone'=>'111','State'=>'cd']
],
[
['Campaign'=>'zxxxzx','Phone'=>'111111','State'=>'zxxx']
],
[
['Campaign'=>'aaaa','Phone'=>'111','State'=>'Csd']
],
[
['Campaign'=>'sasa','Phone'=>'111','State'=>'asas']
]
];
var_export(array_column($array,0));
Output:
array (
0 =>
array (
'Campaign' => 'xxx',
'Phone' => '111',
'State' => 'cd',
),
1 =>
array (
'Campaign' => 'zxxxzx',
'Phone' => '111111',
'State' => 'zxxx',
),
2 =>
array (
'Campaign' => 'aaaa',
'Phone' => '111',
'State' => 'Csd',
),
3 =>
array (
'Campaign' => 'sasa',
'Phone' => '111',
'State' => 'asas',
),
)