Creating nested parent child array from one dimensional array in php - 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.

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.

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

merging two array in php

I have two arrays
$arr1=Array
(
[0] => Array
(
[0] => 'a'
),
[1]=>Array
(
[0]=>'b'
),
[2] => Array
(
[0] => 'c'
),
[3]=>Array
(
[0]=>'d'
),
[4]=>Array
(
[0]=>'e'
)
);
$arr2=array('1','2');
output should be
$arr3=Array
(
[0] => Array
(
[0] => 'a',
[1]=>'1'
),
[1]=>Array
(
[0]=>'b',
[1]=>'2'
),
[2] => Array
(
[0] => 'c',
[1]=>'1'
),
[3]=>Array
(
[0]=>'d',
[1]=>'2'
),
[4]=>Array
(
[0]=>'e',
[1]=>'1'
)
);
can someone please suggest me some solutions
You can do this with a MultipleIterator and attach the first array as ArrayIterator and the second one as InfiniteIterator, e.g.
<?php
$arr1 = [["a"], ["b"], ["c"], ["d"], ["e"]];
$arr2 = [1,2];
$result = [];
$mIt = new MultipleIterator();
$mIt->attachIterator(new ArrayIterator($arr1));
$mIt->attachIterator(new InfiniteIterator(new ArrayIterator($arr2)));
foreach($mIt as $v)
$result[] = array_merge($v[0], [$v[1]]);
print_r($result);
?>
This version will allow $arr2 to contain any number of values, should that be a requirement:
<?php
$arr1 = [
['a'], ['b'], ['c'], ['d'], ['e'],
];
$arr2 = ['1', '2'];
// wrap the array in an ArrayIterator and then in an
// InfiniteIterator - this allows you to continually
// loop over the array for as long as necessary
$iterator = new InfiniteIterator(new ArrayIterator($arr2));
$iterator->rewind(); // start at the beginning
// loop over each element by reference
// push the current value in `$arr2` into
// each element etc.
foreach ($arr1 as &$subArray) {
$subArray[] = $iterator->current();
$iterator->next();
}
print_r($arr1);
This yields:
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 1
)
[3] => Array
(
[0] => d
[1] => 2
)
[4] => Array
(
[0] => e
[1] => 1
)
)
Hope this helps :)

merging subarrays of multidimensional arrays while sorting array alphabetically

Trying to alphabetically merge arrays with foreach loop.
<?php
$fruits = array(
'Apple' => array('ids'=>array(1,2)),
'Banana' => array('ids'=>array(3,4)),
'Ananas' => array('ids'=>array(5,6))
);
$result = array();
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
$result[$first_letter] = $subarr;
}
print_r($result);
gives me smth like
Array
(
[A] => Array
(
[ids] => Array
(
[0] => 5
[1] => 6
)
)
[B] => Array
(
[ids] => Array
(
[0] => 3
[1] => 4
)
)
)
instead of smth like
[A] => Array
(
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
)
how can I fix it?
You overwrite your result every iteration in this line:
$result[$first_letter] = $subarr;
Just create a new array if the subArray in the result array doesn't exists and merge the ids subArray into your result array.
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
if(!isset($result[$first_letter]))
$result[$first_letter] = [];
$result[$first_letter] = array_merge($result[$first_letter], $subarr["ids"]);
}
Please try to use foreach loop.
$fruits = array(
'Apple' => array('ids'=>array(1,2)),
'Banana' => array('ids'=>array(3,4)),
'Ananas' => array('ids'=>array(5,6))
);
$result = array();
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
foreach($subarr as $key=>$value){
foreach ($value as $gkey => $gvalue) {
$result[$first_letter]['ids'][] = $gvalue;
}
}
}
echo "<pre>";
print_r($result);
Display above code output like below.
Array
(
[A] => Array
(
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
)
[B] => Array
(
[ids] => Array
(
[0] => 3
[1] => 4
)
)
)

Creating nested parent child array from multi dimensional array in 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
(
)
)
)
)

Categories