Spitzer Spitzer - 4 months ago 7
PHP Question

Increase the speed of this code? Foreach

$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.

Answer

If ['Data']['Show'] will be true or false then:

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

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

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