1
$arr_ramones = array(
          1=>array('name'=>'johnny', 'display'=>1), 
          2=>array('name'=>'joey', 'display'=>1), 
          3=>array('name'=>'cj', 'display'=>0), 
          4=>array('name'=>'deedee', 'display'=>1), 
          5=>array('name'=>'marky', 'display'=>0)
  );

I'd like to loop through my array but only with rows that have a display value of 1.

I read how to do this with a regular array. Can something similar be done with multidimensional arrays without costing performance? My example array is small - real world example contains thousands of values.

1
  • 1
    If the 'real world' example array would contain thousands of values then really it should be held in a database, you would then be able to filter them as they are requested from the DB. Commented Apr 2, 2014 at 15:39

4 Answers 4

2

also works with array_filter.

$arr_ramones = array(
    1 => array('name'=>'johnny', 'display'=>1), 
    2 => array('name'=>'joey', 'display'=>1), 
    3 => array('name'=>'cj', 'display'=>0), 
    4 => array('name'=>'deedee', 'display'=>1), 
    5 => array('name'=>'marky', 'display'=>0)
);
$filter = array_filter($arr_ramones, function($arr) {
    return $arr['display'] == 1;
});
Sign up to request clarification or add additional context in comments.

Comments

1

Just try with:

foreach ($arr_ramones as $item) {
  if ($item['display']) {
    echo $item['name'];
  }
}

or use array_filter

$display = array_filter($arr_ramones, function($item){
  return $item['display'];
});

foreach ($display as $item) {
  echo $item['name'];
}

Comments

1

Try array_walk if you just want to display data without creating new array. In case of creating array use array_map

array_walk($arr_ramones, function($a) { // displaying information from array based on requirements
    if ( $a['display'] == 1) {
        echo $a['name'] . " "; // johnny joey deedee 
    }
});

$n = array_map(function($a) { // creating new array based on requirements
    if ( $a['display'] == 1) {
        return $a['name'];
    }
}, $arr_ramones);

2 Comments

Is there a performance benefit in using array_walk as opposed to foreach?
@acoder With performance they are pretty much the same while this function's algorithm uses loop as well, though it has another benefits. See this
0

A simple foreach like this.

foreach($arr as $k=>$arr)
{
 if($arr['display']==1)
  {
    echo $arr['name']."<br>";
  }
}

Using an array_filter

function filt($var)
{
   return $var['display']==1;
}  

$new_arr=array_filter ($arr_ramones,"filt" );
print_r($new_arr);

Demo

2 Comments

Right, that came to mind immediately. Wondered if there is a function to essentially "query" the array.. so I could get a count of values having display=1.
@acoder, array_filter can be used too as shown in the illustration. I would prefer a foreach instead.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.