-6
$pass = array();

foreach ($var as $index) 
{
    if($index['Data']['Show'] == false)
        continue;

    $pass[] = $index;
}

echo json_encode($pass);

I need to know how to get the same result in a more streamlined and faster.

iZume
  • 119
  • 1
  • 11
  • 2
    Use `array_filter` – u_mulder Aug 02 '16 at 19:56
  • 3
    How much data do you have that a foreach with a 3 lines of code is not fast enough? I doubt there would be a solution that would speed it up more than this, even array_filter (while it would be faster, it wouldn't be like 100x or anything significant). Perhaps use a database and query for only rows you want to show. – Jonathan Kuhn Aug 02 '16 at 19:57
  • How do you know this code is slow? How much data do you try to process? – E_p Aug 02 '16 at 20:00
  • It is for a dynamic array that will get data from a navigation bar editable from MySQL, for some reason Jquery sometimes takes longer than 200 ms to obtain the information, and i suposse this solution – iZume Aug 02 '16 at 20:07

1 Answers1

5

Might be slightly faster, I haven't tested, but if ['Data']['Show'] will be true or false then this is how I would do it:

$pass = array_filter($var, function($v) { return $v['Data']['Show']; });

If it could be other values that evaluate to false then:

$pass = array_filter($var, function($v) { return $v['Data']['Show'] !== false; });
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87