0

I have just had this issue. I have a multidimensional array ($varianti) that looks like this:

Array
(
    [pa_taglia] => Array
        (
            [0] => l
            [1] => m
        )

    [pa_colore] => Array
        (
            [0] => blu
            [1] => giallo
            [2] => rosso
        )

)

What I need is to get different arrays for each sub array so I need this result:

Array
(
    [0] => l
    [1] => m
)
Array
(
    [0] => blu
    [1] => giallo
    [2] => rosso
)

The main problem is that I can get as many sub-arrays as needed (this is for my Woocommerce plugin to create product_variations from attributes) so it needs to be flexible.

This is the code I came up with (after 2 hours...):

$keys = array_keys($varianti);//get the main keys

        //split multidimensional array in sub arrays
        foreach ($keys as $key=>$val){          
            $nr_var[$val]= count($varianti[$keys[$key]]);//create array such as array('key1'=> qty1, 'key2'=> qty2);
            $$val = $varianti[$keys[$key]];//create a variable variable from key
        }
        print_r($nr_var);
        foreach ($nr_var as $chiave=>$valore){
            print_r($$chiave);//retrieve values calling variable variable
        }

I hope this may be of help to anyone.

1
  • 2
    I am not sure what the question is? if you have the solution to your own answer then you should post it as a answer instead of a extension of your question. Commented Aug 3, 2016 at 10:20

4 Answers 4

2

You can use extract function which will automatically create new variables basing on the key values:

extract($varianti);
var_dump($pa_colore);
Sign up to request clarification or add additional context in comments.

Comments

1

you can use extract() function of PHP. this function extract array in variable format.

Consider this is in $main_array

Array
(
    [pa_taglia] => Array
        (
            [0] => l
            [1] => m
        )

    [pa_colore] => Array
        (
            [0] => blu
            [1] => giallo
            [2] => rosso
        )

)

extract($main_array);

print_r($pa_taglia);
print_r($pa_colore);

For more detail refer http://php.net/manual/en/function.extract.php

Comments

0

You can just get them by key and store them in a variable.

$array1 = $varianti["pa_taglia"];
$array2 = $varianti["pa_colore"];

var_dump($array1);
var_dump($array2);

Comments

0

Here I have got the all global variable using get_defined_vars(); of your program ,which you obviously be multidimensional array. After that i am access their key with array_keys($orignalArr);. Then finally display every subarray by their key value.

$orignalArr = get_defined_vars();
$keyArr=array_keys($orignalArr);
$arrCount=sizeof($keyArr);
echo  "Values of keys<br><br>";
for($i=0;$i<$arrCount;$i++)
{
echo "<br/>";
print_r($keyArr[$i]);
echo "==";
print_r($orignalArr[$keyArr[$i]]);
echo "<br/>";
}

1 Comment

this is not a particularly useful answer, as not only is the indentation off and makes it almost unreadable - but it is entirely undocumented and without explanation. Please provide this, just so that the user can evaluate this as an answer to accept or not.

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.