note 94553 added to function.array-intersect-key

From: Date: Wed, 11 Nov 2009 18:23:06 +0000
Subject: note 94553 added to function.array-intersect-key
Groups: php.notes 
Request: Send a blank email to php-notes+get-162774@lists.php.net to get a copy of this message
Regarding php at keithtylerdotcom solution to emulate <?php $z = someFuncReturningAnArray()['some_key']; ?> His recommended solution will still return an array. To get the value of a single key in an array returned by a function, simply add implode() to the recipe: <?php function someFuncReturningAnArray() { return array( 'a' => 'b', 'c' => 'd', 'e' => 'f', 'g' => 'h', 'i' => 'j' ); } //traditional way $temp = someFuncReturningAnArray(); $b = $temp['a']; echo print_r($b, 1) . "\n----------\n"; //keithtylerdotcom one-line method $b = array_intersect_key(someFuncReturningAnArray(), array('a'=>'')); echo print_r($b, 1) . "\n----------\n"; //better one line method $b = implode('', array_intersect_key(someFuncReturningAnArray(), array('a'=>''))); echo print_r($b, 1) . "\n----------\n"; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 71.248.188.31 ---- Manual Page -- http://www.php.net/manual/en/function.array-intersect-key.php Edit -- https://master.php.net/note/edit/94553 Del: integrated -- https://master.php.net/note/delete/94553/integrated Del: useless -- https://master.php.net/note/delete/94553/useless Del: bad code -- https://master.php.net/note/delete/94553/bad+code Del: spam -- https://master.php.net/note/delete/94553/spam Del: non-english -- https://master.php.net/note/delete/94553/non-english Del: in docs -- https://master.php.net/note/delete/94553/in+docs Del: other reasons-- https://master.php.net/note/delete/94553 Reject -- https://master.php.net/note/reject/94553 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162774) next »