I have a problem, I would like to merge arrays by value. Below is a entry example, the entry array have a 100 records
Array
(
[0] => Array
(
[id] => 1
[code] => dfrr5tv5t5vt5
[status] => online
)
[1] => Array
(
[id] => 2
[code] => e32e3e2e2323e23e
[status] => online
)
[2] => Array
(
[id] => 1
[desc] => Some_description
)
[3] => Array
(
[id] => 2
[desc] => Some_description_2
)
....
)
I would like to get the following result through merge array by [id]
Array
(
[0] => Array
(
[id] => 1
[code] => dfrr5tv5t5vt5
[status] => online
[desc] => Some_description
)
[1] => Array
(
[id] => 2
[code] => e32e3e2e2323e23e
[status] => online
[desc] => Some_description_2
)
....
)
Use assocative arrays. Use $row["id"] as associative index.
$result = [];
foreach ($arr as $row) {
$result[$row["id"]] = isset($result[$row["id"]]) ? array_merge($result[$row["id"]], $row) : $row;
}
$result = array_values($result); // optional, this removes associative keys
Related
I've merged two arrays. A duplicate entry is a deleted. Let me explain the as an example (I have compare array using user_id)
$array1 = Array
(
[0] => Array
(
[id] => 44
[user_id] => 2
[name] => jina_testl
[profilePhoto] =>
)
)
$array2 = Array
(
[0] => Array
(
[id] => 2
[user_id] => 3
[name] => demo_test1
[profilePhoto] =>
)
[1] => Array
(
[id] => 3
[user_id] => 2
[name] => abc
[profilePhoto] =>
)
[2] => Array
(
[id] => 4
[user_id] => 4
[name] => test
[profilePhoto] =>
)
)
I have merged array1 and array2 and duplicate array remove. I get the following output:
Array
(
[0] => Array
(
[id] => 44
[user_id] => 2
[name] => jina_testl
[profilePhoto] =>
)
[1] => Array
(
[id] => 2
[user_id] => 3
[name] => demo_test1
[profilePhoto] =>
)
[3] => Array
(
[id] => 4
[user_id] => 4
[name] => abc
[profilePhoto] =>
))
But I want such an output like this:
Array
(
[0] => Array
(
[id] => 2
[user_id] => 3
[name] => demo_test1
[profilePhoto] =>
)
[1] => Array
(
[id] => 4
[user_id] => 4
[name] => abc
[profilePhoto] =>
))
You could do something like the following: first get all the user_ids that we do not want to be included, by taking the intersection of the two columns.
If we then merge the two separate arrays and exclude the ones that are present in the array of unwanted IDs we should get our final result.
$userId = 'user_id';
$userIdsArray1 = array_column($array1, $userId);
$userIdsArray2 = array_column($array2, $userId);
// If we then take the negation we should get something similar to an exclusive or.
// Thus those in one or the other but not in both arrays.
$unwantedUserIds = array_intersect($userIdsArray1, $userIdsArray2);
$result = [];
foreach (array_merge($array1, $array2) as $record) {
if (!in_array($record[$userId], $unwantedUserIds)) {
$result[] = $record;
}
}
echo '<pre>', print_r($result, true), '</pre>';
Result:
Array
(
[0] => Array
(
[id] => 2
[user_id] => 3
[name] => demo_test1
[profilePhoto] =>
)
[1] => Array
(
[id] => 4
[user_id] => 4
[name] => test
[profilePhoto] =>
)
)
I have an array $result as such:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
and an array $items as such:
[0] => Array (
[itemid] => 2
[name] => B
)
[1] => Array (
[itemid] => 4
[name] => D
)
How do I remove all items from the $result array, that occur in the $items array? In this case, the $result would become:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
)
Since the question is mostly code, here's some extra characters to make StackOverflow accept the question.
I think this is what you want. (Not tested yet)
<?php
foreach ($result as $key => $array) {
$result[$key] = array_diff($array, $items);
}
print_r($result);
How to sort an 1st array with 2nd array which has sorted keys in 2nd array without using any loop.
1st Array.
$chunk = array(
[0] => Array
(
[id] => 212
[order] => 1
[title] => fdfdfdfdf
)
[1] => Array
(
[id] => 5
[order] => 2
[title] =>
)
[2] => Array
(
[id] => 781
[order] => 3
[title] =>
)
)
2nd array with sorted keys of 1st array.
$sort = array
(
[2] => 2
[0] => 0
[1] => 1
)
You could use array_map for that:
$arr = array_map(function($val) use($chunk){
return $chunk[$val];
}, $sort);
This is the output:
Array
(
[2] => Array
(
[id] => 781
[order] => 3
[title] =>
)
[0] => Array
(
[id] => 212
[order] => 1
[title] => fdfdfdfdf
)
[1] => Array
(
[id] => 5
[order] => 2
[title] =>
)
)
Now, if you want the keys to be 0,1,2..., you can use array_values, after mapping:
$arr = array_values($arr);
And the output:
Array
(
[0] => Array
(
[id] => 781
[order] => 3
[title] =>
)
[1] => Array
(
[id] => 212
[order] => 1
[title] => fdfdfdfdf
)
[2] => Array
(
[id] => 5
[order] => 2
[title] =>
)
)
Of course, there is no function for this. You'll have to do something similar
<?php
$chunk = [
// data
];
$sorted = [];
$sort = [
// ordered keys
];
foreach($sort as $keyToFind) {
foreach($chunk as $arrayElement) {
if($arrayElement['id'] == $keyToFind)) {
$sorted[$keyToFind] = $arrayElement;
}
}
}
As you can see, this is a bit time and ressources consumming because of the two imbricated foreaches. Let's hope your arrays are not so big
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.
I have an array, seen below. My desired output is to group by store, and then concatenate (I think?) all of the related quantities and denominations into those arrays. I've shown what the desired output would be.
CURRENT ARRAY
Array
(
[denomination] => Array
(
[0] => 25
[1] => 50
[2] => 100
[3] => 200
)
[quantity] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 4
)
[store] => Array
(
[0] => candy store
[1] => candy store
[2] => book store
[3] => candy store
)
)
DESIRED OUTPUT
Array
(
[candy store] => Array
(
[0] => Array
(
[denomination] => Array
(
[0] => 25
[1] => 50
[2] => 200
)
)
[1] => Array
(
[quantity] => Array
(
[0] => 1
[1] => 1
[2] => 4
)
)
)
[book store] => Array
(
[0] => Array
(
[denomination] => Array
(
[0] => 100
)
)
[1] => Array
(
[quantity] => Array
(
[0] => 2
)
)
)
)
$result = array();
foreach ($array['store'] as $index => $type) {
$result[$type]['denomination'][] = $array['demoniation'][$index];
$result[$type]['quantity'][] = $array['quantity'][$index];
}
This is not exactly, what you specified as "desired output", but I don't see a reason, why one should put the denomination- and quantity-arrays into additional arrays.
However, if this has any reason, you can get it similar
$result = array();
foreach ($array['store'] as $index => $type) {
$result[$type][0]['denomination'][] = $array['demoniation'][$index];
$result[$type][1]['quantity'][] = $array['quantity'][$index];
}