Php 基于父id从四个数组创建多维数组

Php 基于父id从四个数组创建多维数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,从这四个阵列中: $root = (FORD, FIAT, GM, KIA, FERRARI); $parentIsFiat = (Panda, Punto, Tipo); $parentIsPanda = (1, 13, 16, 20); $parentIs13 = (Red, Blue, White); 如何创建多维数组以提供以下信息: FORD FIAT --Panda ----1 ----13 ------Red ------Blue ---

从这四个阵列中:

$root = (FORD, FIAT, GM, KIA, FERRARI);  
$parentIsFiat = (Panda, Punto, Tipo);  
$parentIsPanda = (1, 13, 16, 20);  
$parentIs13 = (Red, Blue, White);  
如何创建多维数组以提供以下信息:

FORD  
FIAT  
--Panda  
----1  
----13  
------Red  
------Blue  
------White  
----16  
----20  
--Punto  
--Tipo       
GM  
KIA  
FERRARI
背景:我的网站上的当前菜单在HTML中包含网站上的每个链接。我只想让HTML用于实际可见的菜单项。目前,我得到路径中的每个项目(菲亚特>熊猫>13)及其同级代码如下:

$categoryPath = \XLite\Core\Database::getRepo('\XLite\Model\Category')->getCategoryPath($this->getCategoryId());  
foreach ($categoryPath as $category) {
    $currentCatID = $category->getID();
    $currentCatSiblings = $category->getSiblings(true);
    foreach ($currentCatSiblings as $sibling) {
        $menuItems[$currentCatID][] = $sibling->getName(); // the four arrays above
    }
}
每个阵列都有父ID作为键,因此我可以将其“附加”到正确的位置,但我不知道如何从现有的四个阵列构建阵列

$root = ["FORD", "FIAT", "GM", "KIA", "FERRARI"];
$parentIsFiat = ["Panda", "Punto", "Tipo"];
$parentIsPanda = [1, 13, 16, 20];
$parentIs13 = ["Red", "Blue", "White"];

$desiredOutput = [];

foreach($root as $make){
    $desiredOutput[$make] = [];
}

foreach($parentIsFiat as $model){
    $desiredOutput["FIAT"][$model] = [];
}

foreach($parentIsPanda as $year){
    $desiredOutput["FIAT"]["Panda"][$year] = [];
}

foreach($parentIs13 as $color){
    $desiredOutput["FIAT"]["Panda"][13][$color] = [];
}


var_dump($desiredOutput);
收益率:

array(5) {
  ["FORD"]=>
  array(0) {
  }
  ["FIAT"]=>
  array(3) {
    ["Panda"]=>
    array(4) {
      [1]=>
      array(0) {
      }
      [13]=>
      array(3) {
        ["Red"]=>
        array(0) {
        }
        ["Blue"]=>
        array(0) {
        }
        ["White"]=>
        array(0) {
        }
      }
      [16]=>
      array(0) {
      }
      [20]=>
      array(0) {
      }
    }
    ["Punto"]=>
    array(0) {
    }
    ["Tipo"]=>
    array(0) {
    }
  }
  ["GM"]=>
  array(0) {
  }
  ["KIA"]=>
  array(0) {
  }
  ["FERRARI"]=>
  array(0) {
  }
}
要记住的关键是,PHP中的所有数组更像是其他语言中的“字典”或“哈希集”类型。PHP不像其他语言(Java、C、Javascript等)那样有“数组”。如果您想要一个“数组”,那么可以使用类似于
echo$myStuff[3]的语法
然后您只需创建一个PHP数组(a/k/a“dictionary”),其中每个键都是一个连续的整数

在任何阵列中查看发动机罩下的示例:

$anyArray = getAnyArray();
foreach($anyArray as $key=>$value){
    echo($key . ":" . $value . "<br>");
}
$anyArray=getAnyArray();
foreach($anyArray作为$key=>$value){
回声($key.:“$value.”
”; }
您可以引入一个临时的
$parent
变量,该变量将引用树中必须插入兄弟姐妹的位置,并将该
$parent
引用在树的下一代兄弟姐妹中进一步向下移动:

$tree = [];
$parent = &$tree; // Use `= &` to reference the same location
foreach ($categoryPath as $category) {
    $currentCatID = $category->getID();
    $currentCatSiblings = $category->getSiblings(true);
    foreach ($currentCatSiblings as $sibling) {
        $parent[] = [ "name" => $sibling->getName() ];
        if ($sibling->getID() === $currentCatID) $index = count($parent) - 1;
    }
    $parent[$index]["children"] = [];
    $parent = &$parent[$index]["children"]; // Use `= &` to reference the deeper location
}
在末尾,
$tree
将包含嵌套数组。它将具有“子”键来存储嵌套结构,如下所示:

[
  [ "name" => "Ford" ],
  [
    "name" => "Fiat",
    "children" => [
      [
        "name" => "Panda",
        "children" => [
          [ "name" => "1" ],
          [
            "name" => "13",
            "children" => [
              [ "name" => "Red" ],
              [
                "name" => "Blue",
                "children" => [],
              ],
              [ "name" => "White" ],
            ],
          ],
          [ "name" => "16" ],
          [ "name" => "20" ],
        ],
      ],
      [ "name" => "Punto" ],
      [ "name" => "Tipo"  ],
    ],
  ],
  [ "name" => "GM" ],
  [ "name" => "Kia" ],
  [ "name" => "Ferrari" ],
]
或者,如果希望将名称用作关联数组中的键,则:

$tree = [];
$parent = &$tree; // Use `= &` to reference the same location
foreach ($categoryPath as $category) {
    $currentCatID = $category->getID();
    $currentCatSiblings = $category->getSiblings(true);
    foreach ($currentCatSiblings as $sibling) {
        $parent[$sibling->getName()] = [];
    }
    $parent = &$parent[$category->getName()]; // Use `= &` to reference the deeper location
}
结果:

[
  "Ford" => [],
  "Fiat" => [
    "Panda" => [
      "1" => [],
      "13" => [
        "Red" => [],
        "Blue" => [],
        "White" => []
      ],
      "16" => [],
      "20" => []
    ],
    "Punto" => [],
    "Tipo" => []
  ],
  "GM" => [],
  "Kia" => [],
  "Ferrari" => []
]

那么,红色、蓝色或白色是
$categoryPath
中的最后一项吗?或者你怎么才能得到这些条目?是的\XLite\Core\Database::getRepo('\XLite\Model\Category')->getCategoryPath($this->getCategoryId());以类别对象的形式返回从根目录到活动类别的类别路径。要清楚,此答案不是健壮的/递归的;它使用静态数量的循环。当输出数组中没有四个级别时,此操作将中断。Yep。我认为OP的困难在于理解键和值是如何工作的。我很感激你的回答,但是是的。。我希望看到一些递归的或带有PHP数组操作函数的东西,比如merge()或类似的。谢谢。第二个示例似乎在我的实际应用程序中起作用。我需要稍微研究一下代码,以了解它是如何工作的——我总是与数组打交道。请注意。。在你的结果中,熊猫出现在福特而不是菲亚特的领导下。