1

I need to create an dynamic array and can't get it right. I need something like this:

Product Name

  • top
    • White
    • Black
  • Bottom
    • Red
    • Green

I came up with this code, which is producing the above text, so my logic must be about right.

$set = array();
$set['name'] = "Product Name";

$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");

echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
    echo "<pre>- $o</pre>";
    $set['options'][]['name'] = $o;

    foreach ($values[$o] as $v) {
        echo "<pre>-- $v</pre>";
        $set['options'][]['values']['name'] = $v;
    }
}

The array that is created with the above code is:

Array
(
  [name] => Product Name
  [options] => Array
  (
    [0] => Array
    (
     [name] => top
    )
[1] => Array
                (
                    [values] => Array
                        (
                            [name] => White
                        )

                )

            [2] => Array
                (
                    [values] => Array
                        (
                            [name] => Black
                        )

                )

            [3] => Array
                (
                    [name] => bottom
                )

            [4] => Array
                (
                    [values] => Array
                        (
                            [name] => Red
                        )

                )

            [5] => Array
                (
                    [values] => Array
                        (
                            [name] => Green
                        )
                )
        )
)

THe output I want is:

Array
(
    [name] => Product Name
    [options] => Array
        (
            [0] => Array
                (
                    [name] => top
                    [values] => Array
                        (
                            [0] => Array
                                (
                                    [name] => White
                                )

                            [1] => Array
                                (
                                    [name] => Black
                                )

                        )

                )

            [1] => Array
                (
                    [name] => bottom
                    [values] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Red
                                )

                            [1] => Array
                                (
                                    [name] => Green
                                )

                        )

                )

        )

)

What am I missing ?

1
  • if you are creating array by yourself then you can create as desired output array('top' => array('top','bottom')); Commented Nov 17, 2017 at 13:58

1 Answer 1

2

You got

$set['options'][]['name'] = $o;
                ^
             this one 

and

$set['options'][]['values']['name'] = $v;
                ^
             This one

in outer as well as inner loop which was adding new item to array (so there were indexes like 0, 1, 2 ...) so could not produce what you wanted.

You may correct your array like below:

Demo

<?php

$set = array();
$set['name'] = "Product Name";

$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");

echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
    echo "<pre>- $o</pre>";

    $vals = array();
    foreach ($values[$o] as $v) {
        echo "<pre>-- $v</pre>";
         $vals[] = array('name' => $v );
    }
     $set['options'][] = array('name' => $o, 'values' => $vals );

}

print_r($set);

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

Comments

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.