php - Get specific key value pairs from associative array -


i`m new php , 1 basic question, but...i have associative array (result of json_decode function). example looks this:

$cars = array   (   array("brand"=>"volvo", "price" => 10000, "type"=> 1),   array("brand"=>"audi", "price" => 20000, "type"=> 2),   array("brand"=>"ford", "price" => 30000, "type"=> 3),   array("brand"=>"audi", "price" => 31000, "type"=> 3),   array("brand"=>"ford", "price" => 32000, "type"=> 2),   array("brand"=>"audi", "price" => 33000, "type"=> 2)   ); 

i need loop $cars , brand , price key value pair brand audi because of need convert each object then.

thanks!

pretty simple. use foreach iterate through each car. if car brand name 'audi' add entry it's on array.

<?php $cars = array   (   array("brand"=>"volvo", "price" => 10000, "type"=> 1),   array("brand"=>"audi", "price" => 20000, "type"=> 2),   array("brand"=>"ford", "price" => 30000, "type"=> 3),   array("brand"=>"audi", "price" => 31000, "type"=> 3),   array("brand"=>"ford", "price" => 32000, "type"=> 2),   array("brand"=>"audi", "price" => 33000, "type"=> 2)   );  $audis = [];   foreach ($cars $car) {   if ($car['brand'] == 'audi') {     $audis[] = ['price' => $car['price'], 'type' => $car['type']];   } }  foreach ($audis $audi) {   print_r($audi); } 

this returns audi's

array (     [price] => 20000     [type] => 2 ) array (     [price] => 31000     [type] => 3 ) array (     [price] => 33000     [type] => 2 ) 

link: https://eval.in/769607


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -