cakephp 将子阵列移动到父阵列并更改父/子阵列名称

tv6aics1  于 2022-11-11  发布在  PHP
关注(0)|答案(4)|浏览(146)

我知道可能这是问之前,不确定是否是在这种形式,但我确实尝试了一些重播,从我发现这里关于这一点和失败。
我有一个数组

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

或者我可以使用的另一种方法是,如果我可以更改数据1,数据2,数据3等等。
可以是n数据取决于用户选择了多少产品
转换为相同的名称,例如简单的DataInfo
例如:

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
        )

任何解决方案都可以。
谢谢并致以问候

yqhsw0fo

yqhsw0fo1#

使用以下代码获得结果:

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

}
edqdpe6u

edqdpe6u2#

我会这样做。但是,我会寻找修复为什么数据是在该结构开始。

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

5us2dqdw3#

您第一个选择:

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

brqmpdu1

brqmpdu14#

这是最好的算法

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

相关问题