PHP 具有用数组初始化的键的关联数组
PHP associative array with keys initialized with array
所以我想要一个具有数组值的关联键。每当我在 Map 中找到一个不是键的新值时,它应该用空数组的值初始化键,否则如果键已经存在,则添加到该值数组
这里是代码,还有注释,方便阅读。
谢谢!
<?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"])
);
生成输出的一种方法是抓取所有类别,将其展平为单个数组,然后删除重复项,循环遍历它,然后使用 in_array 匹配键的 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
)
)
所以我想要一个具有数组值的关联键。每当我在 Map 中找到一个不是键的新值时,它应该用空数组的值初始化键,否则如果键已经存在,则添加到该值数组
这里是代码,还有注释,方便阅读。
谢谢!
<?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"])
);
生成输出的一种方法是抓取所有类别,将其展平为单个数组,然后删除重复项,循环遍历它,然后使用 in_array 匹配键的 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
)
)