0

I have an array like so:

$input = array("visit", "outdoor", "parks-trailer");
$input_content = "A Value for last array element for input array."

$another_input = array("visit", "outdoor");
$another_input_content = "A Value for last array element for $input_content array."

And I have some text to assign to the last element of the array, once built.

Basically, what I need is the returned array to be like this:

$output = array(
    "visit" => array(
        "outdoor" => array(
            "A Value for last array element for $input_content array."
            "parks-trailer" => "A Value for last array element for input array."
        )
    )
);

How can I do this from values of an $input array that will always be 1 dimensional.

$content = 'My Value';
$output = array();
$flipped = array_flip($input);
$count = count($input) - 1;

foreach($flipped as $key => $flip)
{
    if ($count >= $key)
        $output[$key] = $content;
    else
        $output[$key] = array();
}

The problem here is that array_flip does work, but it doesn't do multidimensional here? And so array_flip transforms to array('visit' => 0, 'outdoor' => 1, 'parks-trailer' => 2), but I'm at a loss on how to get it to do multidimensional array, not singular.

I need to loop through multiples of these and somehow merge them into a global array, the answer given in here is not the same.

So, I need to merge each 1 of these into another array, keeping the values, if they exist. array_merge does not keep the values, array_merge_recursive does not keep the same key structure. How to do this?

11
  • Will the input array always be 1-dimensional? Commented Oct 17, 2016 at 20:19
  • No, it can be different values from the array Commented Oct 17, 2016 at 20:20
  • How do you mean? Will the array you wish to use always be one-dimensional? Commented Oct 17, 2016 at 20:21
  • 1
    Okay, what have you already tried? Commented Oct 17, 2016 at 20:22
  • 3
    stackoverflow.com/questions/34886008/… Commented Oct 17, 2016 at 20:31

1 Answer 1

1

I'm not sure sure why you would want such thing, but this is an option:

$ar = ['a', 'b', 'c'];

function arrayMagicFunction($ar, $last_string = 'STRING') {
    $ret = $last_string;
    $ar = array_reverse($ar);
    foreach ($ar as $v) {
        $ret = [$v => $ret];
    }
    return $ret;
}
var_dump(arrayMagicFunction($ar, 'A Value here'));

Output:

array(1) {
  'a' =>
  array(1) {
    'b' =>
    array(1) {
      'c' =>
      string(12) "A Value here"
    }
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

@AbraCadaver, the downvote is because I didn't search for a duplicate question before answering? Just wanted to make sure... because I'm 100% sure the answer is correct.
Just so you can see that the downvote was not mine, I just downvoted.
and the reason is?
For calling me out when you have no idea who downvoted you. But I might upvote in a minute ;-)
You are right :) sorry for that. Thought you might know know the reason here...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.