Creating nested parent child array from multi dimensional array in php - php

I have this array
$array = Array
(
[a] => Array
(
[0] => b
[1] => h
)
[b] => c
[c] => d
[h] => m
)
And I need to convert the array to like below
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
[h] => Array
(
[m] => Array
(
)
)
)
)
I already asked this question for One Dimensional array.
I tried with [Creating nested parent child array from one dimensional array in php and I got the below array
Array
(
[a] => Array
(
[b] => Array
(
[a] => Array
(
[h] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
[h] => Array
(
[m] => Array
(
)
)
)
)
)
)
)
)
)
How to Check wheather the key is present in multi dimensional array and if present add the child to the existing key. Help to resolve the Problem. Thanks in Advance

<?php
$array = array(
'a' => array(0=>'b',1=>'h'),
'b' => 'c',
'c' => 'd',
'h' => 'm',
);
$newArray = array();
$secondarray = array();
$part = &$newArray;
$i=1;
foreach($array as $first => $second)
{
if($i==1)
{
$firstone=$first;
}
else
{
if($i==count($array))
{
$newArray[$first] = array($second => array());
$secondarray[$firstone]=$newArray;
}
else
{
$part = &$part[$first];
$part[$second] = array();
}
}
$i++;
}
echo '<pre>';print_r($secondarray);
output
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
[h] => Array
(
[m] => Array
(
)
)
)
)

Related

How to transform an array into multidimensional array [duplicate]

I have 2 array.
First Array:
$array = array(
'a' => 'b',
'b' => 'c',
'c' => 'd',
);
Second Array:
$array = Array
(
[a] => Array
(
[0] => b
[1] => h
)
[b] => c
[c] => d
[h] => m
)
i need to change this array like
First array:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => d
)
)
)
)
Second Array:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
[h] => Array
(
[m] => Array
(
)
)
)
)
sirwilliam answers helped for first array problem. and I need it for multi dimensional array. Help to resolve the problem. Thanks in advance
You can try to use & (references):
PHP:
$array = array(
'a' => 'b',
'b' => 'c',
'c' => 'd',
);
$newArray = array();
$newArray[key($array)] = array();
$part = &$newArray;
foreach($array as $first => $second){
$part = &$part[$first];
$part[$second] = array();
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
?>
Result:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
)
)
Then you can create a loop for the last part.

How can i rearrange a php array without using key names

My array is below i need to arrange like array2 (without use $aa['caption1'] like names directly)
arrray1 is
Array
(
[0] => Array
(
[caption1] => Array
(
[0] => gfdhgfjhg
[1] => dfhfgjghk
)
[caption2] => Array
(
[0] => shgjgh
[1] => dhfgkgjl
)
[banner_image] => Array
(
[0] => assets/images/page_content/img_namT7.jpg
[1] => assets/images/page_content/img_R8mzP.jpg
)
)
[1] => Array
(
[heading] => Array
(
[0] =>
)
[pragraph] => Array
(
[0] =>
)
)
)
arrray2 is(Required format )
Array
(
[0] => Array
(
array('caption1'=>'caption1','caption2'=>'shgjgh','banner_image'=>'assets/images/page_content/img_namT7.jpg'),
array('caption1'=>'dfhfgjghk','caption2'=>'dhfgkgjl','banner_image'=>'page_content/img_R8mzP.jpg')
)
[1] => Array
(
array('heading'=>'','pragraph'=>''),
array('heading'=>'fgh','pragraph'=>'ghgh'),
)
)
please any one help me.
The solution using array_keys, array_map and array_combine functions:
// $arr is your initial array
$result = [];
foreach($arr as $v){
$keys = array_keys($v);
$data = call_user_func_array('array_map', [null] + $v);
$result[] = array_map(function($item) use($keys){
return array_combine($keys, $item);
}, $data);
}
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Array
(
[caption1] => gfdhgfjhg
[caption2] => shgjgh
[banner_image] => assets/images/page_content/img_namT7.jpg
)
[1] => Array
(
[caption1] => dfhfgjghk
[caption2] => dhfgkgjl
[banner_image] => assets/images/page_content/img_R8mzP.jpg
)
)
[1] => Array
(
[0] => Array
(
[heading] =>
[pragraph] =>
)
)
)

read array key from inside array

Can I read array key from inside the array itself , please to suggest php functions and not foreach loops , as I am trying to avoid loops as much as possible?
code looks like this :
array_fill_keys(array('a','b','c', 'd'),array(
'action'=>'getUserLongTermCategoriesAction',
'params'=> 'place key here',
)
Check this.
$arr = array('a','b','c','d');
$temp = array_map(function ($keys) {
return array(
'action'=>'getUserLongTermCategoriesAction',
'params'=> $keys,
);
}, $arr);
$result = array_combine($arr, $temp);
Output:
Array
(
[a] => Array
(
[action] => getUserLongTermCategoriesAction
[params] => a
)
[b] => Array
(
[action] => getUserLongTermCategoriesAction
[params] => b
)
[c] => Array
(
[action] => getUserLongTermCategoriesAction
[params] => c
)
[d] => Array
(
[action] => getUserLongTermCategoriesAction
[params] => d
)
)

Creating nested parent child array from one dimensional array in php

I have 2 array.
First Array:
$array = array(
'a' => 'b',
'b' => 'c',
'c' => 'd',
);
Second Array:
$array = Array
(
[a] => Array
(
[0] => b
[1] => h
)
[b] => c
[c] => d
[h] => m
)
i need to change this array like
First array:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => d
)
)
)
)
Second Array:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
[h] => Array
(
[m] => Array
(
)
)
)
)
sirwilliam answers helped for first array problem. and I need it for multi dimensional array. Help to resolve the problem. Thanks in advance
You can try to use & (references):
PHP:
$array = array(
'a' => 'b',
'b' => 'c',
'c' => 'd',
);
$newArray = array();
$newArray[key($array)] = array();
$part = &$newArray;
foreach($array as $first => $second){
$part = &$part[$first];
$part[$second] = array();
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
?>
Result:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
)
)
Then you can create a loop for the last part.

Editing associative array while retaining reference for preparation to JSON encode

Is there any common way to edit associative array while retaining reference to original array and making the array be prepared for JSON encode?
An easy way is to rearrange array to new one, but I am thinking that better practice is to go recursively and edit it live.
Here is my function for live editing JSON array with PHP:
function editJSON(&$target, &$aimer){
$found = false;
$result = 0;
foreach($aimer as $ak => $av){
foreach($target as $tk => $tv){
if ($tk == $ak){
$found = true;
$result = 1;
if (is_array($tv) && is_array($av)) editJSON($target[$tk], $aimer[$ak]);
}
if (!$found && is_array($tv)) $result += editJSON($target[$tk], $aimer);
}
if ($found && !is_array($av)) $target[$ak] = $av;
}
return $result;
}
Its fast and tested:
$target = array( 'z'=>array('v'=>1, 'c'=>2, 'g'=>3),
'b'=>array(
'a'=>array(
'b'=>array('g'=>array('c'=>11, 'd'=>13), 'b'=>array('x'=>91, 'y'=>92)),
'e'=>array('m'=>2)
)
),
'd'=>array(
'a'=>array(
'b'=>array('g'=>array('c'=>11, 'd'=>13))
)
)
);
$aimer = array(
'a'=>array(
'b'=>array('g'=>array('c'=>998, 'hh'=>999))
)
);
echo 'Target array:';
echo '<br />';
print_r($target);
echo '<br />';
echo 'Aiming array:';
echo '<br />';
print_r($aimer);
echo '<br />';
$result = editJSON($target, $aimer);
echo 'Resulting array:';
echo '<br />';
print_r($target);
echo '<br />';
echo 'JSON has been modified ' . $result . ' times.';
Will output:
Target array:
Array ( [z] => Array ( [v] => 1 [c] => 2 [g] => 3 ) [b] => Array ( [a] => Array ( [b] => Array ( [g] => Array ( [c] => 11 [d] => 13 ) [b] => Array ( [x] => 91 [y] => 92 ) ) [e] => Array ( [m] => 2 ) ) ) [d] => Array ( [a] => Array ( [b] => Array ( [g] => Array ( [c] => 11 [d] => 13 ) ) ) ) )
Aiming array:
Array ( [a] => Array ( [b] => Array ( [g] => Array ( [c] => 998 [hh] => 999 ) ) ) )
Resulting array:
Array ( [z] => Array ( [v] => 1 [c] => 2 [g] => 3 ) [b] => Array ( [a] => Array ( [b] => Array ( [g] => Array ( [c] => 998 [d] => 13 [hh] => 999 ) [b] => Array ( [x] => 91 [y] => 92 ) ) [e] => Array ( [m] => 2 ) ) ) [d] => Array ( [a] => Array ( [b] => Array ( [g] => Array ( [c] => 998 [d] => 13 [hh] => 999 ) ) ) ) )
JSON has been modified 2 times.
EDIT: Added functionality for adding values to array and counting edits.

Categories