in php how do i make an like array(“one”,“two”,“three”) into $somearray[“one”][“two”][“three”]?

StackOverflow https://stackoverflow.com/questions/4225516

  •  26-09-2019
  •  | 
  •  

Question

I would like to take an array, say:

array("one", "two", "three");

and make it into an array of arrays, like:

$someArray["one"]["two"]["three"];

The array of arrays ($someArray) would have to be created by some sort of loop as the initial array is created by an explode() so i don't know how deep $someArray would be.

I hope this is clear, thanks all for your time!

I am reading up on it at the moment myself, would array_map() work for this?

Was it helpful?

Solution

You should use array_reduce. The solution is quite simple.

To do the trick, reverse the array and only then apply the reduction.

$a = array('a','b','c');
$x = array_reduce(array_reverse($a), function ($r, $c) {
        return array($c=>$r);
     },array());

EDIT an expanded and explained version:

In php we don't have a function to go deep into an array automatically but we haven't to. If you start from the bottom, we will be able to enclose an array into the previous one with a simple assignation.

$a = array('a','b','c');
$result=array(); // initially the accumulator is empty
$result = array_reduce(
    array_reverse($a), 
    function ($partialResult, $currentElement) {
        /* enclose the partially computed result into a key of a new array */
        $partialResult = array($currentElement=>$partialResult); 
        return $partialResult;
    }, 
    $result
);

By the way I prefer the shorter form. I think it is a functional idiom and doesn't need further explanation to a middle experienced developer (with a bit of functional background). The second one add a lot of noise that, it is suitable to learn but source of distraction into production code (obviously I'm referring to function with just a return statement).

OTHER TIPS

You can do this:

$path = array("one", "two", "three");
$ref = &$arr;
foreach ($path as $key) {
    if (!isset($ref[$key])) break;
    $ref = &$ref[$key];
}

At the end $ref is a reference to $arr['one']['two']['three'] if it exists. And if you replace break by $ref[$key] = array() you will build an array with that structure instead.

i'm not sure why you would want this.

But but the basis of what your wanting would be like this

$numbers = array("one", "two", "three");

$array = array();
$pointer = &$array;

$last_val = NULL;

foreach($numbers as $key => $val){
     if(!empty($pointer[$last_val])){
         $pointer = &$pointer[$last_val];
         $pointer = new array($val);
         $last_val = $val;
     }else{
         $pointer = new array($val);
         $last_val = $val;
     }
}

This should then right what you want to $array;

This is untested but i think that should work if it does not just tell me what its doing and i will have a look

$arrNew = array();
$ref = &$arrNew;
$arrParts = array("one", "two", "three");

foreach($arrParts as $arrPart){

   $ref[$arrPart] = array(); 
   $ref = &$ref[$arrPart];
}

When you print $arrNew, It will print this..

Array
(
    [one] => Array
        (
            [two] => Array
            (
                [three] => Array
                    (
                    )

            )

    )

)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top