note 94403 modified in function.array-intersect-key by danbrown

From: Date: Tue, 03 Nov 2009 01:04:50 +0000
Subject: note 94403 modified in function.array-intersect-key by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162349@lists.php.net to get a copy of this message
A Q&D way to grab a specific entry from an assoc array returned from a function as a one liner: Normally when a function returns an array, and you only want part of that array, you have to do: <?php $a=func(); $z=$a["desired"]; // ... use $z ?> However, using an immediate array as the second argument to array_intersect_key allows you to skip this explicit assignment step: <?php $z=array_intersect_key(func(),Array("desired"=>"")); // ... use $z ?> This may be appreciated by those who have been spoiled by the ability of e.g. Python to do: <?python z = func()["desired"] ?> --was-- A Q&D way to grab a specific entry from an assoc array returned from a function as a one liner: Normally when a function returns an array, and you only want part of that array, you have to do: <?php $a=func(); $z=$a["desired"]; ... // use $z ?> However, using an immediate array as the second argument to array_intersect_key allows you to skip this explicit assignment step: <?php $z=array_intersect_key(func(),Array("desired"=>"")); ... // use $z ?> This may be appreciated by those who have been spoiled by the ability of e.g. Python to do: <?python z = func()["desired"] ?> http://php.net/manual/en/function.array-intersect-key.php

« previous php.notes (#162349) next »