Spitzer Spitzer - 1 year ago 93
PHP Question

Increase the speed of this code? Foreach

$pass = array();

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

$pass[] = $index;

echo json_encode($pass);

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

Answer Source

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; });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download