0

i have a value array like this , how to only showing array where companyID like $companyID

$price = array { 
            [0]=>{ 
                ["Price"]=>"5000" 
                ["leadTime"]=>"2" 
                ["companyID"]=>"1" 
            } 
            [1]=>{ 
                ["Price"]=>"4400" 
                ["leadTime"]=>"2" 
                ["companyID"]=>"2" 
            } 
            [2]=>{ 
                ["Price"]=>"3000" 
                ["leadTime"]=>"2" 
                ["companyID"]=>"3" 
            } 
        } 

if the $companyID=1 the result like this

$price = array { 
            [0]=>{ 
                ["Price"]=>"5000" 
                ["leadTime"]=>"2" 
                ["companyID"]=>"1" 
            } 
        } 
Orocduded
  • 23
  • 2
  • `$price = array_filter($price, function ($v) use ($companyID) { return $v['companyID'] ==$companyID; });` – Nick Nov 13 '19 at 05:09

1 Answers1

0

Use php built in array_filter.

$price = array { 
        [0]=>{ 
            ["Price"]=>"5000" 
            ["leadTime"]=>"2" 
            ["companyID"]=>"1" 
        } 
        [1]=>{ 
            ["Price"]=>"4400" 
            ["leadTime"]=>"2" 
            ["companyID"]=>"2" 
        } 
        [2]=>{ 
            ["Price"]=>"3000" 
            ["leadTime"]=>"2" 
            ["companyID"]=>"3" 
        } 
    } ;

$companyid = 1; 
$price = array_filter($price, function ($el) use ($companyid) { return ($el["companyID"] == $companyid); } ); 
WEBjuju
  • 5,797
  • 4
  • 27
  • 36