【问题标题】:PHP associative array with keys initialized with array带有用数组初始化的键的 PHP 关联数组
【发布时间】:2022-10-08 06:29:14
【问题描述】:

所以我试图拥有一个具有数组值的关联键。并且每当我在 Map 中找到一个不是键的新值时,它应该使用空数组的值初始化键,否则如果键已经存在则添加到该值数组

这是代码,还有 cmets 以便于阅读。

谢谢!

  <?php
    include_once("./array.php");
    ?>
    <?php
    
$categories = [];
$categoryMap=array();
#map to find with the category as the key then the item as the value 
foreach ($items as $key => $value) {
    # code...
    $currentCategories = $value['categories'];
    for ($i = 0; $i < sizeof($currentCategories); $i++) {
        # code...

        

        // if not in keys for the categoryMap then initialize with the key with a value of an empty array
        // otherwise just add the the array of values using that keys
        
        // visual of how it should be 
        // [
        //     'buger':['bun','tomato','sauce']

        // ]
        
        array_push($categories, $currentCategories[$i]);
    }
}
$categories = array_unique($categories);

这是输入数组

<?php



$items = array(
    array("itemName" => "Hat", "price" => 10.99, "categories" => ["apparel", "head"]),
    array("itemName" => "Scarf", "price" => 7.99, "categories" => ["apparel", "neck"]),
    array("itemName" => "Watch", "price" => 19.99, "categories" => ["jewelry", "electronics"]),
    array("itemName" => "Necklace", "price" => 99.99, "categories" => ["jewelry", "neck"]),
    array("itemName" => "Headphones", "price" => 29.99, "categories" => ["head", "electronics"])
);

【问题讨论】:

  • 请写下您的输入和 $items 是什么?
  • @GiacomoM 我刚刚编辑了它。谢谢!!
  • @LawrenceCherone 可以工作,但由于没有初始化的空数组,它不会给我一个错误吗?
  • 它在 cmets 中,视觉效果在那里 @LawrenceCherone
  • 这只是一种制作示例的方式,但它应该看起来像我们将类别作为键,然后是一个包含值的列表@LawrenceCherone

标签: php hashmap


【解决方案1】:

产生输出的一种方法是获取所有类别,将其展平为单个数组,然后删除重复项,循环它,然后如果类别存在于类别中,则使用 in_array 匹配 key 的 itemName。

<?php
$items = array(
    array("itemName" => "Hat", "price" => 10.99, "categories" => ["apparel", "head"]),
    array("itemName" => "Scarf", "price" => 7.99, "categories" => ["apparel", "neck"]),
    array("itemName" => "Watch", "price" => 19.99, "categories" => ["jewelry", "electronics"]),
    array("itemName" => "Necklace", "price" => 99.99, "categories" => ["jewelry", "neck"]),
    array("itemName" => "Headphones", "price" => 29.99, "categories" => ["head", "electronics"])
);


foreach (array_unique(array_merge(...array_values(array_column($items, 'categories')))) as $value) {
   foreach ($items as $item) {
      if (in_array($value, $item['categories'])) {
          $categories[$value][] = $item['itemName'];
      } 
   }
}

print_r($categories);

结果:(Online example)

Array
(
    [apparel] => Array
        (
            [0] => Hat
            [1] => Scarf
        )

    [head] => Array
        (
            [0] => Hat
            [1] => Headphones
        )

    [neck] => Array
        (
            [0] => Scarf
            [1] => Necklace
        )

    [jewelry] => Array
        (
            [0] => Watch
            [1] => Necklace
        )

    [electronics] => Array
        (
            [0] => Watch
            [1] => Headphones
        )

)

【讨论】:

    【解决方案2】:

    翻转多对多数据点的方向不需要任何条件。只需使用嵌套循环并将 itemName 值推送为由相应类别值键入的子数组值。

    代码:(Demo)

    $result = [];
    foreach ($items as $row) {
        foreach ($row['categories'] as $cat) {
            $result[$cat][] = $row['itemName'];
        }
    }
    var_export($result);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多