提问人:Bo Harris 提问时间:12/24/2022 最后编辑:Bo Harris 更新时间:12/29/2022 访问量:53
array_search能否返回多维数组或嵌套数组的内部数组的键?
Can array_search return the key of the inner array of a multidimensional or nested array?
问:
我正在处理一个购物车项目,并且有一个 $_SESSION['productSummary'] 数组,如下所示。
[productSummary] => Array
(
[0] => Array
(
[name] => Individual Coins
[code] => 8774
[IDs] => Array
(
[0] => 8774
[1] => 8775
[2] => 8778
[3] => 8765
)
[pluID] => 6105
[qty] => 4
[finalPrice] => 0.0000
[listPrice] => 0.0000
[shipping] => 0.0000
)
[1] => Array
(
[name] => Business Package - 5,000
[code] => 8770
[IDs] => Array
(
[0] => 8770
)
[pluID] => 6087
[qty] => 1
[finalPrice] => 125.0000
[listPrice] => 125.0000
[shipping] => 0.0000
)
)
当用户想要从购物车中删除商品 #8778 时,我需要找到“8778”的索引键的值,以便我可以从 ID 数组中删除该值。
这是我到目前为止的代码片段。我得到了很好的$matchingKey值,但 $matchingKey 2 始终是空的。
// Find the key for the parent array
$matchingKey = array_search(8778, array_column($_SESSION["productSummary"], 'IDs'));
// Find the key for the element in the nested IDs array
$matchingKey2 = array_search(8778, array_column($_SESSION['productSummary'][$matchingKey]['IDs']));
// If it found a match, unset that element
if(!empty($matchingKey2)) {
unset($_SESSION["productSummary"][$matchingKey]['IDs'][$matchingKey2]);
}
// Reindex the array
$_SESSION["productSummary"][$matchingKey]['IDs'] = array_values($_SESSION["productSummary"][$matchingKey]['IDs']);
我期待
$matchingKey2 = array_search(8778, array_column($_SESSION['productSummary'][$matchingKey]['IDs']));
在此示例中返回值 2,但 if 返回为 NULL
更新:已解决根据给出的答案,我将我的代码更新为此,它现在似乎按预期工作。(或者至少在我再次打破它之前。谢谢大家
// $results is pulled from query of database based on a packageID - Short version is it puts all packaged inventory items in the cart and allows all to be taken out of the cart in as a group.
foreach ($results as $cartItem) {
// set cart time in the database back to NULL so it will appear in the store again
$query = "hidden";
dbWriteQuery($query);
// Remove item from SESSION['products']
$indexA = array_search($cartItem['inventoryID'], array_column($_SESSION["products"], 'code'));
if($indexA !== false) {
unset($_SESSION["products"][$indexA]);
}
// Reindex the array
$_SESSION["products"] = array_values($_SESSION["products"]);
// Remove item (or reduce qty) from SESSION['productSummary']
foreach ($_SESSION["productSummary"] as $key => $product) {
$indexB = array_search($cartItem['inventoryID'], $product['IDs']);
//Found a matching element
if ($indexB !== false) {
// if count of $product['IDs'] > 1, reduce the qty and remove the inventoryID from ID's
if(count($product['IDs'])>1) {
//reduct the qty
$_SESSION['productSummary'][$key]['qty'] = $_SESSION['productSummary'][$key]['qty'] - 1;
// remove the inventoryID from the IDs array
array_splice($_SESSION['productSummary'][$key]['IDs'], $indexB, 1);
// set the 'code' value equal to the next element in the IDs array
$_SESSION['productSummary'][$key]['code'] = $_SESSION['productSummary'][$key]['IDs'][0];
// else unset the whole element from ProductSummary
} else {
unset($_SESSION["productSummary"][$key]);
}
break;
}
}
// Reindex the array
$_SESSION["productSummary"] = array_values($_SESSION["productSummary"]);
}
答:
1赞
Barmar
12/24/2022
#1
不能用于搜索嵌套数组。使用普通循环。array_search()
foreach
另请注意,您不能使用此工具判断是否已找到该项目。如果该项是数组中的第一个元素,则为 ,该元素被视为空。您必须与 进行严格的比较。!empty($matchingKey2)
$matchingKey2
0
false
foreach ($_SESSION["productSummary"] as &$product) {
$index = array_search(8778, $product['IDs']);
if ($index !== false) {
array_splice($product['IDs'], $index, 1);
break;
}
}
评论
0赞
Bo Harris
12/27/2022
foreach 语句中的“&”号是故意的还是错别字?我以前从未见过这样使用。不过,这是一个令人着迷的答案。我认为这将解决我试图实现的目标。谢谢
0赞
Barmar
12/27/2022
这是有意为之的,创建一个参考变量。查看 stackoverflow.com/questions/7733290/...
评论
array_search()
不搜索嵌套数组。我不明白你怎么能得到正确的.$matchingKey
$matchingKey
false
0