'Move a child array to parent array and change parent/child name

I know probably this was asked before not sure if was in this form but I did tried some replay from what I found here about this and failed.

ok I have this array

Array
(
    [0] => Array
        (
            [Data3] => Array
                (
                    [id] => 5
                    [category] => Whiskey
                    [name] => Some name
                    [description] => description
                    [image] => asdf.jpg
                    [price] => 83.99
                )

            [ammount] => 1
            [Data_id] => 3
        )

    [1] => Array
        (
            [Data3] => Array
                (
                    [id] => 4
                    [category] => Tequila
                    [name] => Something Red 75cl
                    [description] => description
                    [image] => sierratequilasilver100.jpg
                    [price] => 92.49
                )

            [ammount] => 2
            [Data_id] => 3
        )

    [2] => Array
        (
            [Data4] => Array
                (
                    [id] => 3
                    [category] => Whiskey
                    [name] => Some name Gold
                    [description] => description
                    [image] => asdf.jpg
                    [price] => 83.99
                )

            [ammount] => 1
            [Data_id] => 4
        )

    [3] => Array
        (
            [Data4] => Array
                (
                    [id] => 5
                    [category] => Vodka
                    [name] => Something Blue 100 cl
                    [description] => description
                    [image] => Something.jpg
                    [price] => 32.44
                )

            [ammount] => 1
            [Data_id] => 4
        )

)

What I would like to be the result is something like this:

Array
(
    [0] => Array
        (
            [id] => 5
            [category] => Whiskey
            [name] => Some name
            [description] => description
            [image] => asdf.jpg
            [price] => 83.99
            [ammount] => 1
            [Data_id] => 3
        )

    [1] => Array
        (
            [id] => 4
            [category] => Tequila
            [name] => Something Red 75cl
            [description] => description
            [image] => sierratequilasilver100.jpg
            [price] => 92.49
            [ammount] => 2
            [Data_id] => 3
        )
    [2] => Array
        (
            [id] => 3
            [category] => Whiskey
            [name] => Some name Gold
            [description] => description
            [image] => asdf.jpg
            [price] => 83.99
            [ammount] => 1
            [Data_id] => 4
        )
    [3] => Array
        (
            [id] => 5
            [category] => Vodka
            [name] => Something Blue 100 cl
            [description] => description
            [image] => Something.jpg
            [price] => 32.44
            [ammount] => 1
            [Data_id] => 4
        )
)

or another way I could work with is if I can change Data1, Data2, Data3 and so on ..

can be n Data depends how many producs a user select

into a same name ex simple Data or Info.

ex:

Array
(
    [0] => Array
        (
            [Info] => Array
                (
                    [id] => 5
                    [category] => Whiskey
                    [name] => Some name
                    [description] => description
                    [image] => asdf.jpg
                    [price] => 83.99
                )

            [ammount] => 1
            [Data_id] => 3
        )

    [1] => Array
        (
            [Info] => Array
                (
                    [id] => 4
                    [category] => Tequila
                    [name] => Something Red 75cl
                    [description] => description
                    [image] => sierratequilasilver100.jpg
                    [price] => 92.49
                )

            [ammount] => 2
            [Data_id] => 3
        )

Any solution will be fine for me.

Thanks and regards



Solution 1:[1]

Use this code for your result:

$final_array = array();
foreach($array1 as $offset1 => $array2) {

    $tmp_array = array();

    foreach($array2 as $offset2 =>  $array3) {

        if(is_array($array3)) {
            $tmp_array = $array3;
        } else {
            $tmp_array[$offset2] = $array3
        }

    }

    $final_array = array_merge($final_array, $tmp_array;); 
    //or
    $final_array[] = $tmp_array;

}

Solution 2:[2]

I would do this this way. However I would look to fix why the data is in that structure to begin with.

$aStartArray =  array(array('Data3'=>array('id'=>1, 'cat'=>2), 'amount' => 1, 'Data_id'=>3));

foreach ($aStartArray as $iPos => $aArray) {

    $aKeys = array_keys($aArray); // fetches all the keys
    $aFirstElement = $aArray[$aKeys[0]]; // Get the first element using first key

    // assign/ overwrite data at the same position
    $aStartArray[$iPos] = array($aFirstElement, 'amount' => $aArray['amount'], 'Data_id' => $aArray['Data_id']);
}

echo "<pre>";
var_dump($aStartArray);

Solution 3:[3]

your first option:

foreach($arr as $key => $value){
        foreach($value as $k => $val){
            if(is_array($val)){                    
                $arr[$key] = $val;
                unset($arr[$key][$k]);
            }
        }
    }
    echo "<pre>"; print_r($arr);

Check output here

Solution 4:[4]

This is the best algorithm

const moveArrayToParentArray = (input) => {
  let finalOutput = []

  input.forEach((e) => {
    if (Array.isArray(e)) {
      finalOutput = [...finalOutput, ...e];

    } else {
      finalOutput = [...finalOutput, e];
    }

  })

  return finalOutput
}

const array = ['a', 'b']
const array2 = [array]
const array3 = [array, "c"]

console.log(moveArrayToParentArray(array))
console.log("----------------------------")

console.log(moveArrayToParentArray(array2))
console.log("----------------------------")

console.log(moveArrayToParentArray(array3))
console.log("----------------------------")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gaurav
Solution 2 atoms
Solution 3
Solution 4 Khmiri Mohamed Khalil