3

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,2,3" OR "seller_id"=>"3,2,1" OR "seller_id"=>"4,6,2" OR "seller_id"=>"5,3,4" OR "seller_id"=>"2,1,2" Array given below

$currentUser=1; $arr= array_filter($arrItems['items'], function($e) use ($currentUser) { return in_array($currentUser, explode(",", $e["current_seller_id"])); });

actually am apply filter on Order Items Collection. Then it filter perfectly but not set array in order grid. Order Grid is buffering not display data. When i write same code in foreach loop then data in order items grid set perfectly But my foreach loop not filter right. it return some extra array where seller_id not exist. My Foreach loop code is there :

$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'] == $currentUser){
            $seller_order_arr[] = $row;
        }
    }
    $arrItems['items'] = $seller_order_arr;

anyone help me to re-write array_filter code in foreach code.

1 Answer 1

2

I guess this should be the equivalent to your array_filter approach:

$currentUser=1;
$seller_order_arr = [];

foreach ($arrItems['items'] as  $row) {
    if ($row['current_seller_id'] != null){
        if (in_array($currentUser, explode(",", $row['current_seller_id']))){
            $seller_order_arr[] = $row;
        }
    }
}
$arrItems['items'] = $seller_order_arr;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.