我有一个数组 $arrItems['items'],其中还有 5 个数组(关联数组),每个数组包含 5 个元素 ( with the keys: f_name, l_name, contact, address, seller_id
)。
我想获得所有那些数组(来自 $arrItems['items']),其中 Seller_id 的元素为 1,就像下面给出的"seller_id"=>"1,2,3"
OR "seller_id"=>"3,2,1"
OR "seller_id"=>"4,6,2"
OR "seller_id"=>"5,3,4"
OR OR "seller_id"=>"2,1,2"
Array。
array(5)
{
[0] =>
array(5)
{
["f_name"] =>
string(3) "abc"
["l_name"] =>
string(3) "xyz"
["contact"] =>
string(5) "12345"
["address"] =>
string(3) "xyz"
["seller_id"] =>
string(1) => "1,2,3"
}
[1]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"3,2,1"
}
[2]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"4,6,2"
}
[3]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"5,3,4"
}
[4]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"2,1,2"
}
请帮我它实际上订购表我只想选择当前卖家 ID 为 1 的那些数组。例如seller No # 1
是登录。那么所有这些数组选择意味着那些数组保存在其他数组中。
array_filter
(文档)与in_array
(文档):
$sellerId = "1";
$arr = array_filter($arrItems['items'], function($e) use ($sellerId) {
return in_array($sellerId, explode(",", $e["seller_id"]);
});
如果您只想要这 5 个选项,请使用:
$options = array("1,2,3", "3,2,1", "5,3,4", "4,6,2", "2,1,2");
$arr = array_filter($arrItems['items'], function($e) use ($options ) {
return in_array($e["seller_id"], $options);
});
编辑:至于您的请求,这是使用foreach
循环的相同代码的版本:
$sellerId = "1";
$seller_order_arr = [];
foreach ($arrItems['items'] as $row) {
if (in_array($sellerId, explode(",", $row["seller_id"])))
$seller_order_arr[] = $row;
}
现在$seller_order_arr
将保存您的过滤数组
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句