php 转置深度可变的多维数组(2级和3级)

flvtvl50  于 2023-02-07  发布在  PHP
关注(0)|答案(2)|浏览(124)

我需要重新构造一个包含2层和3层数据的数组。所有的值都应该按索引分组,但我需要保持关联关系。
样品输入:

$variation = [
    "sku" =>  [
        0 => "dSADad",
        1 => "ASDAF",
        2 => "ASFAS",
    ],
    "Price" => [
        0 => "1",
        1 => "1",
        2 => "1",
    ],
    "Quantity" =>  [
        0 => "123",
        1 => "123",
        2 => "123434",
    ],
    "attributes" => [
        "Color" => [
            0 => "5",
            1 => "4",
            2 => "4",
        ],
        "Size" =>  [
            0 => "3",
            1 => "3",
            2 => "2",
        ],
        "Material" =>  [
            0 => "7",
            1 => "7",
            2 => "8",
        ],
    ],
];

我想把它转换成不同的变体。我尝试了几个选项,但没有成功的结果。我也尝试了用JS添加一个索引到输入之前提交,但它仍然不工作。唯一的选择是把它转换成PHP。
预期结果:

$variations = [
    [
        "sku" => "dSADad",
        "Price" => "1",
        "Quantity" => "123",
        "attributes" => [
            "Color" => "5",
            "Size" => "3",
            "Material" => "7",
        ],
    ],
    [
        "sku" => "ASDAF",
        "Price" => "1",
        "Quantity" => "123",
        "attributes" => [
            "Color" => "4",
            "Size" => "3",
            "Material" => "7",
        ],
    ],
    [
        "sku" => "ASFAS",
        "Price" => "1",
        "Quantity" => "123434",
        "attributes" => [
            "Color" => "4",
            "Size" => "2",
            "Material" => "8",
        ],
    ],
];
rxztt3cl

rxztt3cl1#

我设法做了这段代码:

function extractVariation($variations, $key)
{
    $variation = [];
    foreach ($variations as $property => $values) {
        if (isset($values[$key])) {
            $variation[$property] = $values[$key];
        } else {
            $variation[$property] = extractVariation($values, $key);
        }
    }
    return $variation;
}

$newVariations = [];

foreach ($variations['sku'] as $key => $sku) {
    $newVariations[] = extractVariation($variations, $key);
}

var_export($newVariations);

请参见此处的工作示例:https://3v4l.org/l4gJQ
注意,我将您的$variation数组重命名为$variations
该函数是递归的,这允许它进入attributes数组。
输出为:

array (
  0 => 
  array (
    'sku' => 'dSADad',
    'Price' => '1',
    'Quantity' => '123',
    'attributes' => 
    array (
      'Color' => '5',
      'Size' => '3',
      'Material' => '7',
    ),
  ),
  1 => 
  array (
    'sku' => 'ASDAF',
    'Price' => '1',
    'Quantity' => '123',
    'attributes' => 
    array (
      'Color' => '4',
      'Size' => '3',
      'Material' => '7',
    ),
  ),
  2 => 
  array (
    'sku' => 'ASFAS',
    'Price' => '1',
    'Quantity' => '123434',
    'attributes' => 
    array (
      'Color' => '4',
      'Size' => '2',
      'Material' => '8',
    ),
  ),
)

最好是展示你所尝试的东西,即使它并不完全有效。这样,这里的人们就可以看到,你并不是简单地让他们为你写代码,而是你真的有问题。

q7solyqu

q7solyqu2#

对于示例数据,不必使用递归,因为输入数组的深度是已知的/静态的。
传统的数组换位(二维数组)是嵌套两个foreach循环,并在两个级别之间切换键。对于数据,必须有条件地处理具有3个深度级别的数据集。请注意is_array()条件内部如何使用另一个循环来迭代子数组,并将数据推到结果数组中新的适当位置。
在所有情况下,包含索引关键字的级别被用作新的第一级关键字,而原始的第一级关键字总是被用作新的第二级关键字。
代码:(Demo

$result = [];
foreach ($array as $k1 => $v1) {
    foreach ($v1 as $k2 => $v2) {
        if (is_array($v2)) {
            foreach ($v2 as $k3 => $v3) {
                $result[$k3][$k1][$k2] = $v3;
            }
        } else {
            $result[$k2][$k1] = $v2;
        }
    }
}
var_export($result);

相关问题