我有這個 JSON 數據,我正在使用這個代碼來匹配標題。它有效,但現在我想更進一步,列出所有低于某個價格、或高于某個價格或等于某個價格的產品$json = '[{ "id": 2042049298481, "title": "Test Product 53", "variants": [{ "id": 15733087797297, "title": "Default Title", "price": "100.00", "sku": "no" }]}, { "id": 2196682342449, "title": "its test yar", "variants": [{ "id": 16385289879601, "title": "Default Title", "price": "144.00", "sku": "no" }]}, { "id": 4175970041905, "title": "uhjkjhkhk", "variants": [{ "id": 30302326554673, "title": "Default Title", "price": "1000.00", "sku": "10" }]}, { "id": 4183828791345, "title": "kaminsss", "variants": [{ "id": 30346449518641, "title": "Default Title", "price": "111.00", "sku": "no" }]}, { "id": 4247884890161, "title": "hellooo", "variants": [{ "id": 30579860832305, "title": "Default Title", "price": "1111.00", "sku": "no" }]}, { "id": 4248143822897, "title": "10oct latest collection", "variants": [{ "id": 31157780938801, "title": "50", "price": "111.00", "sku": "no-1" }, { "id": 31157802270769, "title": "60", "price": "101.00", "sku": "" }, { "id": 31157804761137, "title": "70", "price": "111.00", "sku": "" }]}, { "id": 4284600746033, "title": "18oct2019", "variants": [{ "id": 31595410030641, "title": "120 / black", "price": "100.00", "sku": "10" }]}echo json_encode($expected88,true);這可以完美地工作,title但我們希望基于variant price. 一種變體可能有多個價格,如果$filter_value大于、等于或小于,任何人都可能出現。例如,用戶可能搜索最低價格為 50 的產品,因此在這種情況下,如果任何變體符合條件,則它將列出這些產品,或者用戶可以搜索所有變體的價格應低于 50。我們試過這樣$expected = array_filter($array, function ($var) use ($filter_value) { return ($var[$filter_name] >= $filter_value);});但它沒有用。我們再次嘗試foreach和測試,但它總是給出空結果
1 回答

嗶嗶one
TA貢獻1854條經驗 獲得超8個贊
這是可能的解決方案
$filter_name ='variants';
$filter_value = 200;
$filter_field = "price";
$expected88 = array_filter($array, function($el) use ($filter_value, $filter_name, $filter_field) {
return ((int)$el[$filter_name][0][$filter_field] >= (int)$filter_value);
});
- 1 回答
- 0 關注
- 111 瀏覽
添加回答
舉報
0/150
提交
取消