How to merge array by key values which are repeating - php

I have an array like this what i want is to sub array converts into unique key sub array with multiple unique keys. I have tried array_merge_recursive function which is not working because i have multiple arrays in loop and the function works with 2 or 3 arrays.
Array
(
[0] => Array
(
[fromWho] => abc#email.se
)
[1] => Array
(
[tid] => 2013-05-05 23:35
)
[2] => Array
(
[answer] => Company
)
[3] => Array
(
[name] => message
)
[4] => Array
(
[label] => Message
)
[5] => Array
(
[fromWho] => bcd#email.se
)
[6] => Array
(
[tid] => 2013-05-05 23:35
)
[7] => Array
(
[answer] => Star Company
)
[8] => Array
(
[name] => name
)
[9] => Array
(
[label] => Name
)
)
I want the array like this array keys order doesn't matter:
Array
(
[0] => Array
(
[label] => Message
[name] => message
[answer] => Company
[tid] => 2013-05-05 23:35
[fromWho] => abc#email.se
)
[0] => Array
(
[label] => Name
[name] => name
[answer] => Star Company
[tid] => 2013-05-05 23:35
[fromWho] => bcd#email.se
)
)

Use array_chunk() to group the array into chunks of 5 elements. Then you can convert each chunk into an associative array.
$chunks = array_chunk($data, 5);
$result = array_map(function($chunk) {
$arr = [];
foreach ($chunk as $entry) {
foreach ($entry as $key => $val) {
$arr[$key] = $val;
}
}
return $arr;
}, $chunks);

Related

How to Sort multidimensional array by value in php

Hi I need to Sort the multi dimensional array by its value My array is like this I need to group the array by its position to create a new array sorting is just confusing How do i create a sorted array from this
Array
(
[prodata] => Array
(
[4] => Array
(
[position] => 3
[products] => Array
(
[0] => 227
)
)
[3] => Array
(
[position] => 4
[products] => Array
(
[0] => 441
[1] => 54
)
)
)
[richtext] => Array
(
[1] => Array
(
[position] => 1
[header] => qwewqe
[content] => contentadaqsdas
)
)
)
I this array i need to sort by it position and create a new array,
Goal
Array
(
[0]=>Array(
[richtext] => Array
(
[position] => 1
[header] => qwewqe
[content] => contentadaqsdas
)
)
[1]=>Array(
[prodata] => Array
(
[position] => 3
[products] => Array
(
[0] => 227
)
)
)
[2]=>Array(
[prodata] => Array
(
[position] => 4
[products] => Array
(
[0] => 441
[1] => 54
)
)
)
)
How do I achieve this help
You can flat the multi dimension array first, then sort it. Demo
$flat_array = [];
foreach($multi_dimension_array as $k => $array){
foreach($array as $value){
$flat_array[] = array(
$k => $value
);
}
}
usort($flat_array,function($a,$b){return current($a)["position"] - current($b)["position"];});
print_r($flat_array);
Though this code works, But I suggest restruct the data source for better data format. Which can make it a easy work

Filter multidimensional php array with another array

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

Merge Array by value PHP

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

Retrieve the value from the array and then add it to the new PHP

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.

How to split multiple values of keys into separate keys in an Array?

I have this Array
Array (
[0] => Array (
[0] => Array ( [value] => Cooking, [occurence] => 1 )
[1] => Array ( [value] => Music, [occurence] => 1 )
[2] => Array ( [value] => Football,Cooking, [occurence] => 1 )
[3] => Array ( [value] => Travel, [occurence] => 1 )
[4] => Array ( [value] => Cooking,Reading, [occurence] => 2 )
[5] => Array ( [value] => Football,Travel, [occurence] => 1 )
[6] => Array ( [value] => Football, [occurence] => 1 )
[7] => Array ( [value] => Music,Cooking, [occurence] => 1 )
[8] => Array ( [value] => Reading,Travel, [occurence] => 1 )
)
)
The [2], [4], [5], [7] and [8] have 2 values for the key [value].
What I want to do is to split the 2 values of these keys in different keys.
The new values should not go to new Arrays, but they will be added to the similar existing Arrays.
For example, if I break the [2] (Football,Cooking) the result will be that the occurence of [6] (Football) will be incremented by 1 and the [occurence] of [0] (Cooking) will be incremented by 1 also.
Thank you !
Yann
$newdata = array()
foreach($array as $data) { // $array being the inner array with the 9 elements
$keys = explode(',', $data['value']);
foreach ($keys as $subkey) {
$newdata[$subkey]++;
}
}
which would give you something like
$newdata = array(
'Cooking' => 4,
'Football' => 3
etc...
);
Not sure how you want your structure to look afterwards, but at least this'll do the inventory for you.

Categories