1

I have an array $arrItems['items'] in which 5 more arrays (associate array) and each array contain 5 element (with the keys: f_name, l_name, contact, address, seller_id).

I want to get all those arrays (from $arrItems['items']) in which element of seller_id is 1 like "seller_id"=>1 Code given below.

Please guide me how use foreach loop or else...

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"
}
dWinder
  • 11,597
  • 3
  • 24
  • 39
HaFiz Umer
  • 167
  • 9

5 Answers5

2

You can use array-filter:

array_filter — Filters elements of an array using a callback function

In your case:

$sellerId = "1";
$arr = array_filter($arrItems['items'], function($e) use ($sellerId) {
    return $sellerId == $e["seller_id"]; });
dWinder
  • 11,597
  • 3
  • 24
  • 39
1
foreach ($arrItems['items'] as $subarray) {
    if ($subarray[seller_id] === 1) {
        $result[] = $subarray;
    }
}

is this what you need?

1

You can use array_filter to filter the items of array

$arr = array_filter($arrItems['items'], function($arr) {
    return $e["seller_id"] == 1;
});
Kamal Paliwal
  • 1,251
  • 8
  • 16
0

Here you can find simple solution using 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>';

Result:

Array
(
    [f_name] => abc
    [l_name] => xyz
    [contact] => 12345
    [address] => xyz
    [seller_id] => 1
)
Array
(
    [f_name] => abc
    [l_name] => xyz
    [contact] => 12345
    [address] => xyz
    [seller_id] => 1
)
Array
(
    [f_name] => abc
    [l_name] => xyz
    [contact] => 12345
    [address] => xyz
    [seller_id] => 1
)
medskill
  • 320
  • 3
  • 13
  • Yes It Work ! Thanks medskill – HaFiz Umer Feb 21 '19 at 13:03
  • I think the other answers are also good or maybe better than mine (because they have a shorter code). – medskill Feb 21 '19 at 13:05
  • it Filter right But an error occur **must be of the type array, string given** when i assign $arrItems['items']=$row – HaFiz Umer Feb 21 '19 at 13:12
  • $row is only one row in loop, if you want to assign all items to a array, check the other answers, they have a better answer. – medskill Feb 21 '19 at 13:18
  • I have just edited my answer to resolve your issue, you can try my code again. Anyway the other answers (from Jannis, Kamal Paliwal and dWinder) are also good and shorter than this. – medskill Feb 21 '19 at 13:23
  • Yes @medskil it work Perfect. Thank you – HaFiz Umer Feb 21 '19 at 15:20
  • ! actually i use this array for order. if 1 order contain 3 seller_id Like seller_id=>1,2,3 mean in this order the items that contains belong to 3 seller stores. if "seller_id" => 1 or "seller_id"=>1,2,3 then above algorithm Perfect But if seller id like "seller_id"=> 3,2,1 then this algorithm fail to filter. Kindly guid me little more how can filter that arrays also in which id 1 is exist. – HaFiz Umer Feb 22 '19 at 07:30
  • Sorry but i can't understand you, you want to get $arrItems with "seller_id" value "3,2,1" ? Or do you want to sort your array in descending order. – medskill Feb 22 '19 at 07:45
  • medskill i want $arritems with "seller_id"=>"3,2,1" I did this by edit your code. `$seller_order_arr = []; foreach ($arrItems['items'] as $row) { if (strpos($row['current_seller_id'] ,",") > 1 && $row['current_seller_id'] != null){ $seller_order_arr[] = $row; } if($row['current_seller_id'] == 1){ $seller_order_arr[] = $row; } } $arrItems['items'] = $seller_order_arr;` – HaFiz Umer Feb 22 '19 at 08:14
0

For simplest way to get it just two line of code.

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

print_r($arrayItems[$key]);

This method use binary search technics, when number 100000303 foreach loop will take a lot of time to finished it.

Pascal Tovohery
  • 888
  • 7
  • 19