This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have a multidimensional array:
Array
(
[type] => Array
(
[0] => text
[1] => portfolio
[2] => slide
[3] => text
)
[grid] => Array
(
[0] => 3
[1] => 5
[2] => 3
[3] => 4
)
[title] => Array
(
[0] => title1
[3] => title2
)
[content] => Array
(
[0] => content1
[3] => content2
)
[item] => Array
(
[1] => 6
[2] => 7
)
[pagination] => Array
(
[1] => 8
)
[order] => Array
(
[1] => desc
[2] => asc
)
)
And want to group it by [type] key given in the array:
Array (
[0] => Array (
[type] => text
[grid] => 3
[title] => title1
[content] => content1
)
[1] => Array (
[type] => portfolio
[grid] => 5
[item] => 6
[pagination] => 1
[order] => desc
)
[2] => Array (
[type] => slide
[grid] => 3
[item] => 7
[order] => asc
)
[3] => Array (
[type] => text
[grid] => 4
[title] => title2
[content] => content2
)
Is there a way or PHP function to do array grouping like that?
This snippet achieves that:
$result = array();
foreach ($array as $key => $data) {
foreach ($data as $offset => $value) {
if (isset($result[$offset])) {
$result[$offset][$key] = $value;
} else {
$result[$offset] = array($key => $value);
}
}
}
Working DEMO
array_map() with null for the callback will do exactly what you want. However it will have number for the index instead of names.
If you write your own callback then you can return an array with the names you need.
Since apparently people want the actual code:
array_map(null, $type_array, $grid_array, $title_array, $content_array, $item_array);
It really is as simple as that. Most of the other answers are so large and unnecessary.
Note: This assumes a fixed number of arrays - if it's not fixed then this won't work, and then go with Florent's answer.
You can do it with this function:
$type_array = array('text', 'portfolio', 'slide', 'text');
$grid_array = array(3, 5, 3, 4);
$title_array = array(0 => 'title1', 3 => 'title2');
$content_array = array(0 => 'content1', 3 => 'content2');
$item_array = array(1 => 6, 2 => 7);
function group_arrays($type_array, $grid_array, $title_array, $content_array, $item_array) {
$temp_array = array();
for($i = 0; $i < count($type_array); $i++) {
$temp_array[$i] = array( 'type' => #$type_array[$i],
'grid' => #$grid_array[$i],
'title' => #$title_array[$i],
'content' => #$content_array[$i],
'item' => #$item_array[$i]);
}
return $temp_array;
}
print_r(group_arrays($type_array, $grid_array, $title_array, $content_array, $item_array));
Hope this helps!
Related
This question already has answers here:
How to group subarrays by a column value?
(20 answers)
Closed 1 year ago.
I am facing one issue while splitting array by key value. My array looks like below :-
Array
(
[0] => Array
(
[product_id] => 6
[brand_id] => 2
)
[1] => Array
(
[product_id] => 1
[brand_id] => 1
)
[2] => Array
(
[product_id] => 5
[brand_id] => 1
)
)
Now i want to filter split the array based on brand_id. My expected output is like below:-
Array(
[0] => Array(
[0] => Array
(
[product_id] => 6
[brand_id] => 2
)
)
[1] => Array(
[0] => Array
(
[product_id] => 1
[brand_id] => 1
)
[1] => Array
(
[product_id] => 5
[brand_id] => 1
)
)
)
My Input array is stored in $proArray variable
My attempt below:-
$brands = array();
foreach ($proArr as $key => $pro) {
$brands[] = $pro['brand_id'];
}
$brands = array_unique($brands);
$ckey = 0;
foreach($brands as $brand){
}
One way to do it with simple foreach() loop to push values based on your brand_id like below-
$key = 'brand_id';
$return = array();
foreach($array as $v) {
$return[$v[$key]][] = $v;
}
print_r($return);
WORKING DEMO: https://3v4l.org/bHuWV
Code:
$arr = array(
array(
'product_id' => 6,
'brand_id' => 2
),
array(
'product_id' => 1,
'brand_id' => 1
),
array(
'product_id' => 5,
'brand_id' => 1
)
);
$res = [];
foreach ($arr as $key => $value)
$res[$value['brand_id']][] = $value;
$res = [...$res];
print_r($res);
Output:
Array
(
[0] => Array
(
[0] => Array
(
[product_id] => 6
[brand_id] => 2
)
)
[1] => Array
(
[0] => Array
(
[product_id] => 1
[brand_id] => 1
)
[1] => Array
(
[product_id] => 5
[brand_id] => 1
)
)
)
I am trying to combine arrays into one single multidimensional array. There could be more than 5 arrays that needs to be combined so I need a code that will automatically combine all arrays no matter how many they are. I tried array_merge but it requires manual defining of arrays in comma formatted parameters.
The code to convert is:
Array
(
[id] => 1
[name] => Item 1
[slug] => item-slug-1
[parent] => 0
)
Array
(
[id] => 2
[name] => Item 2
[slug] => item-slug-2
[parent] => 1
)
Array
(
[id] => 3
[name] => Item 3
[slug] => item-slug-3
[parent] => 2
)
Array
(
[id] => 4
[name] => Item 4
[slug] => item-slug-4
[parent] => 3
)
Array
(
[id] => 5
[name] => Item 5
[slug] => item-slug-5
[parent] => 3
)
And this is how I would like it to look:
Array
(
[0] => Array
{
[id] => 1
[name] => Item 1
[slug] => item-slug-1
[parent] => 0
}
[1] => Array
{
[id] => 2
[name] => Item 2
[slug] => item-slug-2
[parent] => 1
}
[2] => Array
{
[id] => 3
[name] => Item 3
[slug] => item-slug-3
[parent] => 2
}
[3] => Array
{
[id] => 4
[name] => Item 4
[slug] => item-slug-4
[parent] => 3
}
[4] => Array
{
[id] => 5
[name] => Item 5
[slug] => item-slug-5
[parent] => 3
}
)
Here is how the arrays are generated:
I receive a response JSON from the server that looks like this:
[{"slug":"item-slug-1","name":"Item 1","id":1},{"slug":"item-slug-2","name":"Item 2","id":2},{"slug":"item-slug-3","name":"Item 3","id":3,"children":[{"slug":"item-slug-4","name":"Item 4","id":4},{slug":"item-slug-5","name":"Item 5","id":5}]}]
I decode the JSON then convert it to an array like this:
$categories_obj = json_decode( $_POST['order'] );
$categories_arr = json_decode(json_encode( $categories_obj ), true);
I created a function that walks through each item so it would be easier to insert into my database:
function walk_and_update($data, $parent = 0, $count = 0) {
if( is_array($data) ) {
$combine = array();
/* The arrays are generated here */
foreach( $data as $key => $row ) {
$formatted = array(
'id' => $row['id'],
'name' => $row['name'],
'slug' => $row['slug'],
'parent' => $parent
);
print_r( $formatted );
/* My SQL update is here */
if( isset( $row['children'] ) ) {
walk_and_update( $row['children'], $row['id'], $count );
}
}
}
}
Then I use the function like this:
walk_and_update( $categories_arr );
Like this:
$arr1 = Array(
'id' => 1,
'name' => 'Item 1',
'slug' => 'item-slug-1',
'parent' => 0);
$arr2 = Array(
'id' => 2,
'name' => 'Item 2',
'slug' => 'item-slug-2',
'parent' => 1);
$arr3 = Array(
'id' => 3,
'name' => 'Item 3',
'slug' => 'item-slug-3',
'parent' => 2);
$new_arr = array();
for($i=1;$i<=3;$i++) {
$var_name = 'arr'.$i;
array_push($new_arr,$$var_name);
}
echo "<pre>";print_r($new_arr);
Output:
Array
(
[0] => Array
(
[id] => 1
[name] => Item 1
[slug] => item-slug-1
[parent] => 0
)
[1] => Array
(
[id] => 2
[name] => Item 2
[slug] => item-slug-2
[parent] => 1
)
[2] => Array
(
[id] => 3
[name] => Item 3
[slug] => item-slug-3
[parent] => 2
)
)
you could make function to handle the array, then return expected array to multidimensional array.
can i know first, that the array you want to merging , did they produce one by one or just one time that can produce many array ?
//you could do this if your array produces one time only
$counter = 0;
$new_array=array();
foreach (your array as $key => $val)
{
$new_array[$counter]['id']= $val['id'];
$new_array[$counter]['name']= $val['name'];
$new_array[$counter]['slug']= $val['slug'];
$new_array[$counter]['parent'] = $val['parent'];
$counter++;
}
//if your array produces more than one time
function main ()
{
$data['counter']=0;
$data['newarray']=array();
$data = $this->mergin_array(your array,$data['counter'])
}
function merging_array(your array,$counter)
{
$data = array();
$counter_new = $counter;
foreach(your array as $key => $val)
{
$data['newarray'][$counter]['id'] = $val['id'];
$data['newarray'][$counter]['name'] = $val['name'];
$data['newarray'][$counter]['slug'] = $val['slug'];
$data['newarray'][$counter]['parent'] = $val['parent'];
$counter_new++;
}
$data['counter'] = $counter_new;
return $data;
}
so no matter how many you use , when you access the $data['newarray'] in your main function it will get you the combined for many array
I am currently working on a project where the fields scale when clicking on the "Add" button.
I am grouping each field like this: name="packaging[]", name="packaging[1]", name="packaging[2]" and so on. When I submit the form, this is how the data looks like when posted:
Array
(
[packaging] => Array
(
[0] => 1
[1] => 2
)
[quantity] => Array
(
[0] => 1
[1] => 2
)
[total-weight] => Array
(
[0] => 1
[1] => 2
)
[length] => Array
(
[0] => 1
[1] => 2
)
)
Using PHP I would like to convert the above code to look like this:
Array
(
[0] => Array
(
[packaging] => 1,
[quantity] => 1,
[total-weight] => 1,
[length] => 1,
)
[1] => Array
(
[packaging] => 2,
[quantity] => 2,
[total-weight] => 2,
[length] => 2,
)
)
Any help would be greatly appreciated.
Try this....
$array=array();
foreach($data as $key=>$value){
foreach($value as $k=>$val){
$array[$k][$key]=$val;
}
}
DEMO
Try this code:
$rows = array ('packaging' => array ('0'=> 1,'1' => 2),'quantity' => array('0'=> 1,'1' => 2),'total-weight' => array ('0'=> 1,'1' => 2),
'length' =>array ('0'=> 1,'1' => 2)
);
$res_array = array();
$total_records = count($rows['packaging']);
for($i=0;$i<$total_records;$i++)
{
$res_array[] = array('packaging'=>$rows['packaging'] [$i],'quantity'=>$rows['quantity'][$i],
'total-weight'=>$rows['total-weight'][$i],'length'=>$rows['length'] [$i]);
}
print_r($res_array);
My array is like this:
Array
(
[0] => Array
(
[id] => 1
[name] => a
[hardware_type] => keybord
)
[1] => Array
(
[id] => 2
[name] => b
[hardware_type] => mouse
)
[2] => Array
(
[id] => 1
[name] => a
[hardware_type] => mouse
)
[3] => Array
(
[id] => 1
[name] => a
[hardware_type] => moniter
)
[4] => Array
(
[id] => 2
[name] =>b
[hardware_type] => keyboad
)
)
required out put like this i want only merge hardware type
Array(
[0] => Array
(
[id] => 1
[name] => a
[hardware_type] => keybord, mouse, moniter
)
[1] => Array
(
[id] => 1
[name] => b
[hardware_type] => keyboard, mouse
)
)
Where $array is the input array you described, where $newarray is the output array you desire, and assuming every value of id has the same name as in your example input:
$temp = array();
foreach ($array as $item) {
$temp[$item['id']] = array('id' => $item['id'], 'name' => $item['name']);
if (empty($newarray[$item['id']]['hardware_type']))
$temp[$item['id']]['hardware_type'] = $item['hardware_type'];
else
$temp[$item['id']]['hardware_type'] .= ', ' . $item['hardware_type'];
}
$newarray = array_values($temp);
If you want the hardware_type to be an array instead of a comma-separated list of strings, do this instead:
$temp = array();
foreach ($array as $item) {
$temp[$item['id']] = array('id' => $item['id'], 'name' => $item['name']);
$temp[$item['id']]['hardware_type'][] = $item['hardware_type'];
}
$newarray = array_values($temp);
I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke