0

Suppose I have:

Array
    (
        [some_title] => Array
        (
            [0] => blah blah blah
            [1] => More blah.
            [2] => Stuff and things
        )

        [diff_random_name] => Array
        (
            [0] => blooh blah
        )

        [totally_new_name] => Array
        (
            [0] => interesting insights
            [1] => other.
        )
    )

How can I knock all these titled arrays into their own separate arrays? So it would be one array of some_title and its values, and one of diff_random_name, etc. The names are generated dynamically each time and the count of each sub-array changes too.

1

1 Answer 1

1
foreach($base as $key=>$value)
{
    $$key = $value;
}

Assuming $base is the name of the original array with the sub-arrays, you should be able to use $$ to treat the $key variable as the name of your new array which will give you the new arrays you want.

Sign up to request clarification or add additional context in comments.

6 Comments

I appreciate the help - I have tried various ways of looping through and making new arrays[] to no avail - I tried Nathan's method as well as using $$key to create new arrays but I'm still getting one giant array as an output
what variable are you getting the output from? there should be 3 separate arrays and the original one still. if you get the output from the original then it will be unchanged. try getting output from $some_title, $diff_random_name, and $totally_new_name
Ah ok cool - that did the trick. Thanks again. I guess I'm also wrangling with how to do this in a smart way - at some point down the road after building up the arrays I'll need to split them like this, so this is very helpful.
to be honest i'm not sure this is the best way to go. typically we leave them all in the original array and use a foreach loop to iterate over all of them. otherwise you won't know what the name of your new arrays will be if they are dynamically generated...
No worries - my real problem was that eventually I'll have to send them all off for separate processing to an external service and was mostly worried about how to access them at the changing-title level, so $$key is a good trick
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.