I need to sort a multi-dimensional array which represents filesystem structure:
Array
(
[dir1] => Array
(
[dir2] => Array
(
[dir3] => Array
(
[dir4] => Array
(
[0] => file1.php
[1] => abc.php
)
)
[0] => file2.php
[1] => abc.php
)
)
[abc] => Array
(
[abc] => Array
(
[abc] => Array
(
[0] => file5.php
)
)
)
)
I have no idea what is the algorithm.
http://php.net/sort#51088
replace sort($a) at the beginning of the mulsort function by ksort($a)
EDIT: sorry, just change the mulsort code to :
function mulsort(&$a)
{
ksort($a);
foreach($a as &$value)
if (is_array($value))
mulsort($value);
}
Related
I have an array that has some keys that are repeating multiple times. Would like to combine them so that I can have them in one array, within the same array.
I have an array of this type;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
)
)
Array
(
[Shoes] => Array
(
[FUBU] => Array
(
[0] => size6
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => White
)
)
)
I would like an output of the following kind;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I have tried array_merge, array_merge_recursive but all are not working.
foreach ($county as $value) { print_r(array_merge($value)); } //$value contains the array
foreach ($county as $value) { print_r(array_merge_recursive($value)); } //$value contains the array
Kindly assist if you have an idea on how to solve this in PHP.
You could do it with a few nested array_walk() calls:
$arr=array(array("Shoes" => array("POLO" => array("Size5"))),
array("Shoes" => array("FUBU" => array("size6"))),
array("Bag" => array("HPBAG" => array("Black"))),
array("Bag" => array("HPBAG" => array("White"))));
$res=[];
array_walk($arr,function($a) use (&$res){
array_walk($a, function($ar,$type) use (&$res){
array_walk($ar, function ($arr,$brand) use (&$res,$type){
$res[$type][$brand]=array_merge($res[$type][$brand]??[],$arr);
});
});
});
print_r($res);
See the demo here: https://rextester.com/RFLQ18142
It produces this result:
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
i just want to assign array value to my last array inside multidimensional array.
Here is my array
Array (
[college] => Array (
[student] => Array (
[contact] => Array ()
)
[parents] => Array (
[contact] => Array ()
)
)
[school] => Array (
[parents] => Array (
[contact] => Array ()
)
[student] => Array (
[contact] => Array ()
)
[data] => Array (
[contact] => Array()
)
))
Things are that every things is dynamic some time it could be simple array or might be multidimensional array.
every key and value are dynamic
i want to assign array value to last array eg.
i want something like that.
$assignArray = array('primary_number'=>123,'main_number'=>123);
Array (
[college] => Array (
[student] => Array (
[contact] => Array ('primary_number'=>123,'main_number'=>123)
)
[parents] => Array (
[contact] => Array ('primary_number'=>123,'main_number'=>123)
)
)
[school] => Array (
[parents] => Array (
[contact] => Array ('primary_number'=>123,'main_number'=>123)
)
[student] => Array (
[contact] => Array ('primary_number'=>123,'main_number'=>123)
)
[data] => Array (
[contact] => Array('primary_number'=>123,'main_number'=>123)
)
))
can any one solve it?
any help will appreciate..
Use a recursive procedure:
function replace_leaf_arrays(&$array, $replacement) {
foreach ($array as &$val) {
if (empty($val)) {
$val = $replacement;
} else {
replace_leaf_arrays($val, $replacement);
}
}
}
replace_leaf_arrays($main_array, $assignArray);
I'd like to filter an array which is created by converting XML to an array.
I'd like to remove all parent keys of arrays with key = 0 and an empty value (f.e. "InvalidKey"), but not the ones with a custom name and no value (f.e. "column").
I've already used array_filter (even in combination with array_map), but those functions will filter too few or too much information from the array.
I've also tried to create a loopable function to check if the current array has a key of 0 and an empty value, but I don't know how to get the parent key of the current array, f.e.:
Array
(
[InvalidKey] => Array
(
[0] => *NULL*
)
);
key($arrInput) = 0;
parent::key($arrInput) = "InvalidKey";
So, how to get from:
Array
(
[test] =>
[demo] => 524018
[column] =>
[xml] => Array
(
[Header] => Array
(
[0] => Array
(
[ValidKey] => Array
(
[0] => 1234
)
[InvalidKey] => Array
(
[0] =>
)
)
)
[Body] => Array
(
[0] => *NULL*
)
[Footer] => Array
(
[0] => Array
(
[ValidKey] => Array
(
[0] => I am valid
)
[MoreValidKey] => Array
(
[0] => I am valid too
)
[InvalidKey] => Array
(
[0] =>
)
)
)
)
)
To:
Array
(
[test] =>
[demo] => 524018
[column] =>
[xml] => Array
(
[Header] => Array
(
[0] => Array
(
[ValidKey] => Array
(
[0] => 1234
)
)
)
[Footer] => Array
(
[0] => Array
(
[ValidKey] => Array
(
[0] => I am valid
)
[MoreValidKey] => Array
(
[0] => I am valid too
)
)
)
)
)
PS: The used array key names are variable. For simplicity I used "(In)ValidKey". The array can be as much levels deep as possible, so I can't suffice with 2 for loops.
You need to write a custom script which checks all elements of your array.
Example:
<?php
$test = [
[
[
"asdf",
"",
0,
false,
true,
[
"asdf",
"",
[]
]
],
[]
],
[]
];
function removeEmptyElements(array $array)
{
foreach ($array as $key => $value) {
if (is_array($value))
$value = removeEmptyElements($value);
if (empty($value) && false !== $value && 0 !== $value)
unset($array[$key]);
else
$array[$key] = $value;
}
return $array;
}
print_r(removeEmptyElements($test));
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] =>
)
)
)
I have following multidimensional Array and I want to get the difference, if there is just one sub Array or multiple in that array.
For Example:
In Array [1] there is just one sub Array [example]
In Array [2] there are two sub Arrays [example]
[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
Now to get the [value] from the first Array I would try:
foreach ($content as $example) {
echo($content['example']['value']);
}
And to get each [value] from the second Array I would try:
foreach ($content as $example) {
foreach ($example as $values) {
echo($value['value']);
}
}
So far so good but how do I decide which function to run? Am I missing something?
Is there an if-statement which can help me there?
Something like:
if(multiple sub-arrays){
// do first code example
} else {
// do second code example
}
I simply want a method to get all values called [value] out of the array.
Thank you in advance!
The most obvious solution is to change function which generates your content array so as it always generates sub arrays in a format like:
[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
But if you don't have such option - then use a simple check:
foreach ($content as $item) {
// here check if your `$item` has an `value` subkey under `example` key
if (array_key_exists('value', $item['example'])) {
echo($item['example']['value']);
} else {
foreach ($item['example'] as $values) {
echo ($values['value']);
}
}
}
Assuming that your final dimension allways as a 'value' node:
function arrayIterate($array){
foreach ($content as $example) {
if(!isset($example['value'])){
arrayIterate($example);
}else{
echo($example['value']);
}
}
}