[英]PHP - get entry by object property from an array of objects
數組函數看起來像這樣
function searchArray($array, $key, $value){
$array_iteration = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$output = array();
foreach($array_iteration as $sub_array){
$sub = $array_iteration->getSubIterator();
if($sub[$key] == $value){
$output[] = iterator_to_array($sub);
}
}
return $output;
}
我有傳遞給購物車腳本的變量,如$color, $size, $product_id
。 在數組中,我試圖根據product_id、color和size求和數量,但它只是將所有具有相同顏色的產品相加而不考慮product_id而不是按product_id 、 color**** 和 **size 進行分類
如何在searchArray()
獲取顏色、大小和 product_id ?
邏輯有什么問題?
這是cart.php
if($_POST['cat_type'] == "single"){
$color = $_POST['color'];
$product_size = str_replace("+", " ", $_POST['product_size']);
$amount = $_POST['sprice'];
$vendor = $_POST['sseller'];
//$quantity = $_POST['quantity'];
$product_id = (int)$_POST['product_id'];
$img = "";
$cat = $_POST['cat_type'];
$product_info = $product->getProductById($product_id);
$title = $product_info[0]->title;
$cart = array();
if(isset($_SESSION['__cart'])){
$cart = $_SESSION['__cart'];
}
$current_item = array();
$current_item['image'] = $product_info[0]->images;
$current_item['pimage'] = $img;
$current_item['title'] = $product_info[0]->title;
$current_item['price'] = $amount;
$current_item['id'] = $product_info[0]->id;
$current_item['vendor'] = $vendor;
$current_item['color'] = $color;
$current_item['size'] = $product_size;
$current_item['name'] = $title;
$current_item['category'] = $cat;
$qty = 1;
if (isset($_POST['quantity'])) {
$qty = $_POST['quantity'];
}
if(!empty($cart)){
$search_result = searchArray($cart, 'color', $color, 'size', $product_size, 'id', $product_id);
$index = 0;
if($search_result){
foreach($cart as $key=>$value){
if($value['color'] == $color && $value['size'] == $product_size && $value['id'] == $product_id){
$index = $key;
break;
}
}
$cart[$index]['quantity'] += $qty;
$cart[$index]['amount'] += $amount*$qty;
} else {
$current_item['amount'] = $amount*$qty;
$current_item['quantity'] = $qty;
$cart[] = $current_item;
}
} else {
$current_item['amount'] = $amount*$qty;
$current_item['quantity'] = $qty;
$cart[] = $current_item;
}
$_SESSION['__cart'] = $cart;
echo "1";
exit;
}
首先,我要感謝本論壇的每一位貢獻者。 你的時間、耐心和教導讓像我這樣的人多年來學到了東西。
回到我上面問題的答案:
對於可能處於相同情況的人,這就是我解決的方法。
在多維數組中,如果不存在唯一的鍵 => 值對(超過一對鍵 => 值),那么在這種情況下,如果我們通過單個鍵 => 值對搜索元素,則它可以返回更多比一項。 因此,我們可以使用多個鍵 => 值對來實現搜索以獲取唯一項。
方法:對於數組中的每個數組,遍歷搜索數組,如果任何搜索鍵值與對應的數組鍵值不匹配,我們丟棄該數組並繼續下一個數組的過程。
假設我們要從包含不同部分產品的產品列表中搜索產品詳細信息,因此,在這種情況下,單獨的product_id可能無法提供正確的輸出。 因此,我們需要使用更多的鍵 => 值(即product_id 、 color和size )來搜索列表。
function searchArray($array, $search_list) {
// Create the result array
$result = array();
// Iterate over each array element
foreach ($array as $key => $value) {
// Iterate over each search condition
foreach ($search_list as $k => $v) {
// If the array element does not meet
// the search condition then continue
// to the next element
if (!isset($value[$k]) || $value[$k] != $v)
{
// Skip two loops
continue 2;
}
}
// Append array element's key to the
//result array
$result[] = $value;
}
// Return result
return $result;
}
傳遞值並迭代
if($_POST['cat_type'] == "single"){
$color = $_POST['color'];
$product_size = str_replace("+", " ", $_POST['product_size']);
$amount = $_POST['sprice'];
$vendor = $_POST['sseller'];
//$quantity = $_POST['quantity'];
$product_id = (int)$_POST['product_id'];
$img = "";
$cat = $_POST['cat_type'];
$product_info = $product->getProductById($product_id);
$title = $product_info[0]->title;
$cart = array();
if(isset($_SESSION['__cart'])){
$cart = $_SESSION['__cart'];
}
$current_item = array();
$current_item['image'] = $product_info[0]->images;
$current_item['pimage'] = $img;
$current_item['title'] = $product_info[0]->title;
$current_item['price'] = $amount;
$current_item['id'] = $product_id;
$current_item['vendor'] = $vendor;
$current_item['color'] = $color;
$current_item['size'] = $product_size;
$current_item['name'] = $title;
$current_item['category'] = $cat;
$qty = 1;
// Define search list with multiple key=>value pair
$search_items = array('color'=>$color, 'size'=>$product_size, 'id'=>$product_id);
if (isset($_POST['quantity'])) {
$qty = $_POST['quantity'];
}
if(!empty($cart)){
// Call search and pass the array and
// the search list
$search_result = searchArray($cart, $search_items);
$index = 0;
if($search_result){
foreach($cart as $key=>$value){
//var_dump($value);
if($value['color'] == $color and $value['size'] == $product_size and $value['id'] == $product_id){
$index = $key;
break;
}
}
$cart[$index]['quantity'] += $qty;
$cart[$index]['amount'] += $amount*$qty;
} else {
$current_item['amount'] = $amount*$qty;
$current_item['quantity'] = $qty;
$cart[] = $current_item;
}
} else {
$current_item['amount'] = $amount*$qty;
$current_item['quantity'] = $qty;
$cart[] = $current_item;
}
$_SESSION['__cart'] = $cart;
echo "1";
exit;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.