如何在php中基于元素从关联数组中选择数组

如何在php中基于元素从关联数组中选择数组,php,arrays,Php,Arrays,我有一个数组$arrtimes['items'],其中还有5个数组(关联数组),每个数组包含5个元素(键为:f\u name、l\u name、contact、address、seller\u id) 我想得到所有这些数组(从$arrtimes['items']),其中seller\u id的元素是1,类似于下面给出的“seller\u id”=>1 请指导我如何使用foreach循环,否则 array(5) { [0] => array(5) { ["f_

我有一个数组
$arrtimes['items']
,其中还有5个数组(关联数组),每个数组包含5个元素(键为:
f\u name、l\u name、contact、address、seller\u id

我想得到所有这些数组(从
$arrtimes['items']
),其中
seller\u id
的元素是1,类似于下面给出的
“seller\u id”=>1

请指导我如何使用foreach循环,否则

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"
    }
  [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)=>"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)=>"5"
}
[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)=>"1"
}
  [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)=>"1"
}
您可以使用:

array_filter-使用回调函数过滤数组的元素

就你而言:

$sellerId = "1";
$arr = array_filter($arrItems['items'], function($e) use ($sellerId) {
    return $sellerId == $e["seller_id"]; });
您可以使用:

array_filter-使用回调函数过滤数组的元素

就你而言:

$sellerId = "1";
$arr = array_filter($arrItems['items'], function($e) use ($sellerId) {
    return $sellerId == $e["seller_id"]; });
这是你需要的吗


这就是您需要的吗?

在这里,您可以使用foreach找到简单的解决方案

$arr = [];
foreach ($arrItems['items'] as $i => $row) {
    if ($row['seller_id'] != 1) {
        // Ignore row if seller_id is not 1
        continue;
    }

    // here you got only elements with seller_id = 1
    // so you can add them to a new array
    $arr[] = $row;
}

// After the loop (foreach) in $row you get only elements with seller_id 1.
// If they must be in $arrItems['items'] use it
$arrItems['items'] = $arr;

echo '<pre>';
print_r($arrItems['items']);
echo '</pre>';

在这里,您可以使用foreach找到简单的解决方案

$arr = [];
foreach ($arrItems['items'] as $i => $row) {
    if ($row['seller_id'] != 1) {
        // Ignore row if seller_id is not 1
        continue;
    }

    // here you got only elements with seller_id = 1
    // so you can add them to a new array
    $arr[] = $row;
}

// After the loop (foreach) in $row you get only elements with seller_id 1.
// If they must be in $arrItems['items'] use it
$arrItems['items'] = $arr;

echo '<pre>';
print_r($arrItems['items']);
echo '</pre>';

您可以使用
array\u filter
筛选数组中的项

$key = array_search(1, array_column($arrayItems, 'seller_id'));

print_r($arrayItems[$key]);

您可以使用
array\u filter
筛选数组中的项

$key = array_search(1, array_column($arrayItems, 'seller_id'));

print_r($arrayItems[$key]);

最简单的方法是只需要两行代码


此方法使用二进制搜索技术,当每个循环的编号10000303时,将花费大量时间来完成它。

对于最简单的方法,只需两行代码即可获得它


此方法使用二进制搜索技术,当每个循环的编号10000303时,将花费大量时间来完成它。

是的,它可以工作!谢谢medskillI,我认为其他答案也不错,或者可能比我的答案更好(因为它们的代码更短)。它过滤正确,但出现的错误必须是数组类型,当我分配$arrItems['items]=$row$row时给出的字符串在循环中只有一行,如果要将所有项目分配给数组,请检查其他答案,他们有更好的答案。我刚刚编辑了我的答案以解决您的问题,您可以重试我的代码。不管怎么说,其他答案(来自詹尼斯、卡马尔·帕利瓦尔和德温德)也比这个好而且短。是的,它奏效了!谢谢medskillI,我认为其他答案也不错,或者可能比我的答案更好(因为它们的代码更短)。它过滤正确,但出现的错误必须是数组类型,当我分配$arrItems['items]=$row$row时给出的字符串在循环中只有一行,如果要将所有项目分配给数组,请检查其他答案,他们有更好的答案。我刚刚编辑了我的答案以解决您的问题,您可以重试我的代码。不管怎么说,其他答案(来自詹尼斯、卡马尔·帕利瓦尔和德温德)也很好,而且比这个短。$userdb mean?@HaFizUmer我改了。听着,对不起,这个解决方案不适合我的问题。您的解决方案只返回“int(1)”意味着它正确过滤,但只返回“seller)id”=>1的数组总数。我也知道数组的值。所以@medskill的答案在这里是完美的。谢谢你回复我。$userdb是什么意思?@HaFizUmer我改了。听着,对不起,这个解决方案不适合我的问题。您的解决方案只返回“int(1)”意味着它正确过滤,但只返回“seller)id”=>1的数组总数。我也知道数组的值。所以@medskill的答案在这里是完美的。谢谢你的回复。