将子数组移动到父数组并更改父/子名称

时间:2017-04-03 10:47:04

标签: php arrays cakephp foreach

我知道这可能是在不确定是否采用这种形式之前被问到但是我确实尝试了一些重播,我在这里找到了关于此的重播并失败了。

好的我有这个数组

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
        )

)

我希望结果如下:

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

或我可以使用的另一种方式是,如果我可以更改 Data1,Data2,Data3 等等。

  

可以是n数据取决于用户选择的产品数量

使用相同的名称,例如简单的数据信息

例如:

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
        )

任何解决方案都适合我。

谢谢和问候

3 个答案:

答案 0 :(得分:0)

你的第一个选择:

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

答案 1 :(得分:0)

将此代码用于您的结果:

$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;

}

答案 2 :(得分:0)

我会这样做。但是,我希望解决为什么数据在该结构中开始。

$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);