Thursday, 30 August 2018

Read a multidimensional array with Condition using PHP

I have a multidimensional array as follows,

array('Hundai'=>array('Sonata','i20','Santro','Verna'),'Suzuki'=>array('Alto','Ritz'),'WW'=>array('Polo','Passat','Vento'))

I would like to retrieve only
Sonata, i20, Santro and Verna
when Hundai is my input. Anybody suggest me solution with minimum number of loop

$arr = array(
  'Hyundai' => array('Sonata', 'i20', 'Santro', 'Verna'),
  'Suzuki' => array('Alto', 'Ritz'),
  'VW' => array('Polo', 'Passat', 'Vento')
);

$key = 'Hyundai';

if (isset($arr[$key]) && is_array($arr[$key])) {
  echo implode(', ', $arr[$key]);
}

0 comments:

Post a Comment