Flatten multidimensional array PHP - php

I need to flatten a PHP array but having some issues getting the desired results.
Array
(
[0] => Array
(
[case_code_id] => 1
[parent_id] => 0
[case_code] => Main A
[sub_codes] => Array
(
[0] => Array
(
[case_code_id] => 3
[parent_id] => 1
[case_code] => Sub A
[sub_codes] => Array
(
[0] => Array
(
[case_code_id] => 5
[parent_id] => 3
[case_code] => Sub Sub A
[sub_codes] => Array
(
)
)
)
)
[1] => Array
(
[case_code_id] => 4
[parent_id] => 1
[case_code] => Sub B
[sub_codes] => Array
(
)
)
)
)
[1] => Array
(
[case_code_id] => 2
[parent_id] => 0
[case_code] => Main B
[sub_codes] => Array
(
)
)
)
But I would like to convert this to the following:
Array
(
[0] => Array
(
[case_code_id] => 1
[parent_id] => 0
[case_code] => Main A
)
[1] => Array
(
[case_code_id] => 3
[parent_id] => 1
[case_code] => Sub A
)
[2] => Array
(
[case_code_id] => 5
[parent_id] => 3
[case_code] => Sub Sub A
)
[3] => Array
(
[case_code_id] => 4
[parent_id] => 1
[case_code] => Sub B
)
[4] => Array
(
[case_code_id] => 2
[parent_id] => 0
[case_code] => Main B
[sub_codes] => Array
)
I have tried several loops but nothing returns the full array.
Here is what I have for my loop:
public function array_flatten($array,$list=array()){
for ($i=0;$i<count($array);$i++) {
$results[] = array(
'case_code_id'=>$array[$i]['case_code_id'],
'case_code'=>$array[$i]['case_code'],
'parent_id'=>$array[$i]['parent_id']
);
if (count($array[$i]['sub_codes']) > 0) {
$this->array_flatten($array[$i]['sub_codes'],$results);
} else {
$results[] = $array[$i];
}
}
return $results;
}
And I'm calling it like this: ($multi contains the multidimensional array)
$flat = $this->array_flatten($multi);
The variable $multi is created from this function:
public function build_case_code_tree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = $this->build_case_code_tree($elements, $element['case_code_id']);
$element['sub_codes'] = $children;
$branch[] = $element;
}
}
return $branch;
}
Any thoughts?

function array_flatten($a, $flat = []) {
$entry = [];
foreach ($a as $key => $el) {
if (is_array($el)) {
$flat = array_flatten($el, $flat);
} else {
$entry[$key] = $el;
}
}
if (!empty($entry)) {
$flat[] = $entry;
}
return $flat;
}
print_r(array_flatten($multi));

You're not using $list anywhere in the code, and nothing is passed by reference.
You're close, but your function should use $list in the place of $results, and it should receive $list by reference and modifying it in place instead of returning it.
Something like this (untested though):
function array_flatten($array,&$list=array()){
for ($i=0;$i<count($array);$i++) {
$list[] = array(
'case_code_id'=>$array[$i]['case_code_id'],
'case_code'=>$array[$i]['case_code'],
'parent_id'=>$array[$i]['parent_id']
);
if (count($array[$i]['sub_codes']) > 0) {
$this->array_flatten($array[$i]['sub_codes'],$list);
} else {
$list[] = $array[$i];
}
}
}
And calling it like this:
$flat = Array();
$this->array_flatten($multi, $flat);
// Result is inside $flat now

Related

Merge Parent child array

I have PHP array. I am trying to convert into a parent-child array:
I have used a recursive function but not getting my output.
Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly
[parent_id] => 0
)
[1] => Array
(
[id] => 22
[subscription_name] => Yearly new
[parent_id] => 1
)
[2 => Array
(
[id] => 23
[subscription_name] => Yearly new offer
[parent_id] => 22
)
[3] => Array
(
[id] => 24
[subscription_name] => Weekly
[parent_id] => 0
)
[4] => Array
(
[id] => 25
[subscription_name] => Weekly new offer
[parent_id] => 24
)
)
I expect this result
Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly new offer
[childrens] => Array
(
[0] => Array
(
[id] => 22
)
[1] => Array
(
[id] => 23
)
)
)
[1] => Array
(
[id] => 24
[subscription_name] => Weekly new offer
[childrens] => Array
(
[0] => Array
(
[id] => 25
)
)
)
)
I have tried but not getting my desire Output
My PHP function is
function tree(array $elements, $parent_id = 0) {
echo "<pre>";
$branch = array();
$a=array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parent_id && $element['subscription_type_id'] !=1 ) {
$children = $this->tree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
else {
$element['children'] = array();
}
$branch[] = $element;
}
}
return $branch;
}
Here is the output I am getting from the above function:
Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly
[children] => Array
(
[0] => Array
(
[id] => 22
[subscription_name] => Yearly new
[children] => Array
(
[0] => Array
(
[id] => 23
[subscription_name] => Yearly new offer
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 24
[subscription_name] => Weekly
[children] => Array
(
[0] => Array
(
[id] => 25
[subscription_name] => Weekly new offer
[children] => Array
(
)
)
)
)
Please help me to solve this problem.
Thanks.
Really, i mean REALLY naive version but by the way, your data would be really more easy to exploit, like don't have any parent_id to 0 if it's not a parent Id (NULL for example) I also do not understand why do you have different subscription_name. I think you should work more on change your input instead of find a complexe algorithm to parse them
function tree(array $elements) {
$openList = [];
$result = [];
$id = 0;
foreach ($elements as $key => $element) {
if($key != 0) // I suppose the trial one is not used
{
if($element['parent_id'] == 0) // a root
{
$closeList = [];
$openList[$element['id']] = $element['id'];
$result[$id] = $element;
unset($result[$id]['parent_id']);
$result[$id]['children'] = [];
$newOpenlist = [];
while(count($openList) > 0)
{
foreach ($elements as $key => $element) {
if($element['parent_id'] != 0 && in_array($element['parent_id'], $openList) && !in_array($element['parent_id'], $closeList))
{
$newOpenlist[$element['id']] = $element['id'];
$result[$id]['children'][] = $element;
}
}
foreach($openList as $item)
{
$closeList[] = $item;
}
$openList = $newOpenlist;
$newOpenlist = [];
}
}
$id++;
}
}
return $result;
}
From your question, I do not clearly understand the logic behind the subscription_name. Regarding the childrens below code might work.
<?php
$array = [
[
'id' => 1,
'subscription_name' => 'Yearly',
'parent_id' => 0
],
[
'id' => 22,
'subscription_name' => 'Yearly new',
'parent_id' => 1
],
[
'id' => 23,
'subscription_name' => 'Yearly new offer',
'parent_id' => 22
],
[
'id' => 24,
'subscription_name' => 'Weekly',
'parent_id' => 0
],
[
'id' => 25,
'subscription_name' => 'Weekly new offer',
'parent_id' => 24
],
];
function find_childrens_parent_not_zero($array) {
foreach($array as $key => $value) {
if($value['parent_id'] != 0) {
if(!is_array($array[$key]['childrens'])) {
$array[$key]['childrens'] = [];
}
foreach($array as $k => $v) {
if($v['parent_id'] == $value['id']) {
array_push($array[$key]['childrens'], array('id' => $v['id']));
unset($array[$k]['parent_id']);
}
}
}
}
return $array;
}
function find_childrens_parent_zero($array) {
foreach($array as $key => $value) {
if($value['parent_id'] == 0) {
if(!is_array($array[$key]['childrens'])) {
$array[$key]['childrens'] = [];
}
foreach($array as $k => $v) {
if($v['parent_id'] == $value['id']) {
array_push($array[$key]['childrens'], array('id' => $v['id']));
unset($array[$k]['parent_id']);
}
}
}
}
return $array;
}
function merge_children($array) {
foreach($array as $key => $value) {
if($value['parent_id'] == 0) {
//pluck childrens of it's children
foreach($value['childrens'] as $k => $v) {
foreach($array as $ke => $val) {
if($v['id'] == $val['id']) {
$array[$key]['childrens'] = array_merge($array[$key]['childrens'], $array[$ke]['childrens']);
}
}
}
}
}
return $array;
}
/**
* Remove parent not zero elements
*/
function cleanup_array($array) {
$result = [];
foreach($array as $key => $value) {
if(array_key_exists('parent_id', $value )) {
unset($value['parent_id']);
array_push($result, $value);
}
}
return $result;
}
echo '<pre>';
$result_parent_not_zero = find_childrens_parent_not_zero($array);
$result_parent_zero = find_childrens_parent_zero($result_parent_not_zero);
$result_merged_children = merge_children($result_parent_zero);
$result_cleaned_up = cleanup_array($result_merged_children);
print_r($result_cleaned_up);
Will give you the result as
<pre>Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly
[childrens] => Array
(
[0] => Array
(
[id] => 22
)
[1] => Array
(
[id] => 23
)
)
)
[1] => Array
(
[id] => 24
[subscription_name] => Weekly
[childrens] => Array
(
[0] => Array
(
[id] => 25
)
)
)
)
<?php
$load[0]['id'] = 0;
$load[0]['subscription_name'] = 'Trial';
$load[0]['parent_id'] = 0;
$load[1]['id'] = 1;
$load[1]['subscription_name'] = 'Yearly';
$load[1]['parent_id'] = 0;
$load[2]['id'] = 2;
$load[2]['subscription_name'] = 'Trial';
$load[2]['parent_id'] = 1;
$load[3]['id'] = 3;
$load[3]['subscription_name'] = 'Yearly';
$load[3]['parent_id'] = 1;
$load[4]['id'] = 4;
$load[4]['subscription_name'] = 'Trial';
$load[4]['parent_id'] = 2;
function tree($load){
$output = [];
foreach($load as $l){
$temp = [];
foreach($load as $ll){
if($ll['parent_id']==$l['id']){
if($l['id']==$ll['id'])
continue;
$temp[] = $ll;
}
}
$l['childrens'] = $temp;
$output[] = $l;
}
return $output;
}
echo "<pre>";
print_r(tree($load));

multidimensional array store each list array inside another array

I have nested multidimensional arrays that may be 2 or 3 levels deep. Inside it I may or may not have list arrray. I need to loop over the array:
Array
(
[0] => Array
(
[id] => 1
[name] => cat_name_1
[list] => Array
(
[1] => swgdgbdg
[2] => xcbcxb
)
)
[1] => Array
(
[id] => 3
[name] => cat_name_3
[list] => Array
(
[0] => Array
(
[id] => 1
[name] => cat_name_1
[list] => Array
(
[1] => 543h54h
[2] => 54hrhhfr2
)
)
[1] => Array
(
[id] => 2
[name] => cat_name_2
[list] => Array
(
[1] => eherfherh
[2] => 4564642
)
)
[2] => Array
(
[1] => erggb45yt
[2] => jyuk768k
)
[3] => Array
(
[1] => sdgsdgsdg
[2] => 4tg34g34g
)
)
)
and store the following in another array:
array (
0 => array (
[1] => swgdgbdg
[2] => xcbcxb
) ,
1 => array(
[1] => 543h54h
[2] => 54hrhhfr2
) ,
2 => array(
[1] => eherfherh
[2] => 4564642
),
3 => array(
[1] => erggb45yt
[2] => jyuk768k
),
4 => array(
[1] => sdgsdgsdg
[2] => 4tg34g34g
)
);
You can use array_walk_recursive() to get the key 1,2 and are not array element, like this, check the live demo here.
$result = [];
$temp = [];
array_walk_recursive($array, function($v, $k) use(&$result, &$temp){
if($k == 1)
$temp[$k] = $v;
if($k == 2)
{
$temp[$k] = $v;
$result[] = $temp;
}
});
print_r($result);
Edit for unfixed length and unordered index, live demo.
Extend the array_walk_recursive() with a third parameter for the callfunction to indicate if it's at the start of an subarray.
$result = [];
$temp = [];
$length = 0;
uarray_walk_recursive($array, function($v, $k, $f) use(&$result, &$temp, &$length){
if(is_numeric($k)) {
if($f && $length != 0) {
$result[] = $temp;
$length = 0;
$temp = [];
}
$temp[$k] = $v;
$length++;
}
});
$result[] = $temp;
print_r($result);
function uarray_walk_recursive(&$input, $funcname)
{
if (!is_callable($funcname)) {
if (is_array($funcname)) {
$funcname = $funcname[0] . '::' . $funcname[1];
}
user_error('uarray_walk_recursive() Not a valid callback ' . $user_func,
E_USER_WARNING);
return;
}
if (!is_array($input)) {
user_error('uarray_walk_recursive() The argument should be an array',
E_USER_WARNING);
return;
}
$args = func_get_args();
$flag = true;
foreach ($input as $key => $item) {
if (is_array($item)) {
$args[2] = false;
$flag = true;
uarray_walk_recursive($item, $funcname, $args);
$input[$key] = $item;
} else {
$args[2] = $flag;
$flag = false;
$args[0] = &$item;
$args[1] = &$key;
call_user_func_array($funcname, $args);
$input[$key] = $item;
}
}
}
PseudoCode
function getSomething(var arr)
{
flag = 0;
output = []
for( i = 0 to arr.length)
{
check arr[i] is array,
if yes,then flag = 1 and output.add(getSomething(arr[i]))
if not, continue
}
if flag =0,then return arr
else return output;
}

How to convert multidimensional recursive array into single dimensional array in PHP?

I have an array which comes out by calling a recursion function on the basis of parent id. This array is an n level multidimensional array.
What I want is to break this array into single dimensional, so that every child comes just after their parent.
I am using following function to first convert into recursive tree.
function formatTree($tree, $parent){
$tree2 = array();
foreach($tree as $i => $item){
if($item['cat_parent_id'] == $parent){
$tree2[$item['cat_id']] = $item;
$tree2[$item['cat_id']]['submenu'] = formatTree($tree, $item['cat_id']);
}
}
return $tree2;
}
This is the array I have.
Array
(
[58] => Array
(
[cat_id] => 58
[cat_name] => Desserts
[cat_parent_id] => 0
[submenu] => Array
(
[535] => Array
(
[cat_id] => 535
[cat_name] => dessert child
[cat_parent_id] => 58
[submenu] => Array
(
)
)
)
)
[56] => Array
(
[cat_id] => 56
[cat_name] => Biryani & Rice
[cat_parent_id] => 0
[submenu] => Array
(
)
)
)
This is how I want this.
Array
(
[0] => Array
(
[cat_id] => 58
[cat_name] => Desserts
[cat_parent_id] => 0
[submenu] => Array
(
)
)
[1] => Array
(
[cat_id] => 535
[cat_name] => dessert child
[cat_parent_id] => 58
[submenu] => Array
(
)
)
[2] => Array
(
[cat_id] => 56
[cat_name] => Biryani & Rice
[cat_parent_id] => 0
[submenu] => Array
(
)
)
)
It's almost 2020. I know it's a bit late but for those who are googling around: Their answers were close but they're only returning the parent elements. I've tweaked it a little bit to make it work (using array_merge).
function array_single_dimensional($items)
{
$singleDimensional = [];
foreach ($items as $item) {
$children = isset($item['children']) ? $item['children'] : null; //temporarily store children if set
unset($item['children']); //delete children before adding to new array
$singleDimensional[] = $item; // add parent to new array
if ( !empty($children) ){ // if has children
//convert children to single dimensional
$childrenSingleDimensional = array_single_dimensional($children);
//merge the two, this line did the trick!
$singleDimensional = array_merge($singleDimensional, $childrenSingleDimensional);
}
}
return $singleDimensional;
}
$singleDimensionalArray = array_single_dimensional($multidimensionalArray);
So, I assume you can change your initial function to make an array like the second one... then you should update your function like this:
function formatTree($tree, $parent){
$tree2 = array();
foreach($tree as $i => $item){
if($item['cat_parent_id'] == $parent){
$item['submenu'] = array();
$tree2[] = $item;
formatTree($tree, $item['cat_id']);
}
}
return $tree2;
}
This should work. For your use case you don't need a parent_id in your function.
function formatTree($tree){
$tree2 = array();
foreach($tree as $i => $item){
$submenu = $item['submenu'];
unset($item['submenu']); //clear submenu of parent item
$tree2[] = $item;
if(!empty($submenu)){
$sub = formatTree($submenu); //submenu's return as array in array
$tree2[] = $sub[0]; // remove outer array
}
}
return $tree2;
}
Just try this,
$array = Array
(
"58" => Array
(
"cat_id" => 58,
"cat_name" => "Desserts",
"cat_parent_id" => 0,
"submenu" => Array
(
"535" => Array
(
"cat_id" => 535,
"cat_name" => "dessert child",
"cat_parent_id" => 58,
"submenu" => Array
()
)
)
),
"56" => Array
(
"cat_id" => 56,
"cat_name" => "Biryani & Rice",
"cat_parent_id" => 0,
"submenu" => Array
()
)
);
function singledimensional($array)
{
$res = array();
foreach ($array as $i => $item) {
$temparr = $item;
$item['submenu'] = array();
$res[] = $item;
if (!empty($temparr['submenu']) ){
$child = singledimensional($temparr['submenu']);
$res[] = $child[0];
}
}
return $res;
}
echo '<pre>';
print_r(singledimensional($array));
echo '</pre>';
Output:
Array
(
[0] => Array
(
[cat_id] => 58
[cat_name] => Desserts
[cat_parent_id] => 0
[submenu] => Array
(
)
)
[1] => Array
(
[cat_id] => 535
[cat_name] => dessert child
[cat_parent_id] => 58
[submenu] => Array
(
)
)
[2] => Array
(
[cat_id] => 56
[cat_name] => Biryani & Rice
[cat_parent_id] => 0
[submenu] => Array
(
)
)
)
I hope this will help you :)
(PHP 4 >= 4.0.1, PHP 5)
array_merge_recursive — Merge two or more arrays recursively
`function array_merge_recursive_distinct ( array &$array1, array &$array2 )
{
$merged = $array1;
foreach ( $array2 as $key => &$value )
{
if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) )
{
$merged [$key] = array_merge_recursive_distinct ( $merged [$key], $value );
}
else
{
$merged [$key] = $value;
}
}
return $merged;
}
?>`

Arrange PHP Multidimensional Array By Inner Index

How to arrange this array by last inner index ( 0, 1, 2 ) and get the value of the last inner index as the value of each second index:
Array
(
[text] => Array
(
[grid] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[image] => Array
(
[0] =>
[1] =>
[2] =>
)
[align] => Array
(
[0] => left
[1] => right
[2] => left
)
[title] => Array
(
[0] =>
[1] =>
[2] =>
)
[content] => Array
(
[0] =>
[1] =>
[2] =>
)
)
)
And have the results as below:
Array
(
[text] => Array
(
[0] => Array
(
[grid] => 3
[image] =>
[align] => left
[title] =>
[content] =>
)
[1] => Array
(
[grid] => 4
[image] =>
[align] => right
[title] =>
[content] =>
)
[2] => Array
(
[grid] => 5
[image] =>
[align] => left
[title] =>
[content] =>
)
)
)
This will do the work
function restructure($arr){
$newArr = array();
foreach($arr as $k => $v){
foreach($v as $k1 => $v1){
foreach($v1 as $k2 => $v2){
$newArr[$k][$k2][$k1] = $v2;
}
}
}
return $newArr;
}
As SiGanteng suggested, i dont see other ways than a for/foreach loop:
function buildArray($source, $key = false)
{
// Build the new array
$new_array = array();
// Define groups
$groups = $key === false ? array_keys($source) : array($key);
foreach($groups AS $group)
{
// Get the keys
$keys = array_keys($array[$group]);
// Count the values
$num_entries = count($array[$group][$keys[0]]);
for($i = 0; $i < $num_entries; $i++)
{
foreach($keys AS $key)
{
$new_array[$group][$i][$key] = $array[$group][$key][$i];
}
}
}
return $new_array;
}
This allow you to define the key you need to build; If not specified, the function build the array for every key.
This should work.
function formatit($arr) {
$new = array();
foreach($arr as $k=>$v) {
foreach($v as $k1=>$v1) {
$new[$k1][$k] = $v1;
}
}
return $new;
}
Tested. Call it as
$arr['text'] = formatit($arr['text']);
http://ideone.com/rPzuR

PHP: Modifying array recursively?

I have tried to make a function that iterates through the following array to flatten it and add parent id to children, where applicable. I just can't make it work, so I hope that anyone here has an idea of what to do:
Here's the starting point:
Array
(
[0] => Array (
[id] => 1
[children] => array (
[id] => 2
[children] => Array (
[0] => Array (
[id] => 3
)
)
)
)
The expected result :
Array (
[0] => array (
[id] => 1
)
[1] => array (
[id] => 2
)
[2] => array (
[id] => 3,
[parent] => 2
)
)
Hope that anyone can point me in the right direction. Thanks a lot!
Solution (Thanks to Oli!):
$output = array();
function dejigg($in) {
global $output;
if (!isset($in['children'])) {
$in['children'] = array();
}
$kids = $in['children'] or array();
unset($in['children']);
if (!isset($in['parent'])) {
$in['parent'] = 0; // Not neccessary but makes the top node's parent 0.
}
$output[] = $in;
foreach ($kids as $child) {
$child['parent'] = $in['id'];
dejigg($child); // recurse
}
return $output;
}
foreach ($array as $parent) {
$output[] = dejigg($parent);
}
$array = $output;
print("<pre>".print_r($array,true)."</pre>");
I've tested it this time. This does work!
$input = array( array('id' => 1, 'children'=>array( array('id'=>2, 'children'=>array( array('id'=>3) ) ) ) ) );
$output = [];
function dejigg($in) {
global $output;
$kids = $in['children'] or array();
unset($in['children']);
$output[] = $in;
foreach ($kids as $child) {
$child['parent'] = $in['id'];
dejigg($child); // recurse
}
}
foreach ($input as $parent)
dejigg($parent);
print_r($output);
And it returns:
Array
(
[0] => Array
(
[id] => 1
)
[1] => Array
(
[id] => 2
[parent] => 1
)
[2] => Array
(
[id] => 3
[parent] => 2
)
)

Categories