0

I have a variable for eg: $Foods, which stores arrays like:

array(
    'Fruit' => 'Banana',
    'cake' => array(
        (int) 0 => '10',
        (int) 1 => '11',
        (int) 2 => '12'
    )
)

I want to have this like :

array(
    'Fruit' => 'Banana',
    'cake' =>  '10'
)
array(
    'Fruit' => 'Banana',
    'cake' =>  '11'
)
 array(
    'Fruit' => 'Banana',
    'cake' =>  '12'
)

How can I achieve this?

1 Answer 1

2

try this code

<?php
        $Foods = array('Fruit' => 'Banana', 'cake' => array('10','11','12'));

        $newFoods = array();
        foreach($Foods['cake'] as $key => $val): 
            $newFoods[$key]['Fruit'] =  $Foods['Fruit'];
            $newFoods[$key]['cake'] =  $val;
        endforeach;

        print_r($newFoods);
    ?>

output will be

Array
(
    [0] => Array
        (
            [Fruit] => Banana
            [cake] => 10
        )

    [1] => Array
        (
            [Fruit] => Banana
            [cake] => 11
        )

    [2] => Array
        (
            [Fruit] => Banana
            [cake] => 12
        )

)
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.