note 113942 deleted from function.array-column by sobak

From: Date: Tue, 01 Jul 2014 17:09:21 +0000
Subject: note 113942 deleted from function.array-column by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199799@lists.php.net to get a copy of this message
Note Submitter: ohcc at 163 dot com ---- This function will fail when you try to fetch columns from an indexed array. <?php $a =array( array(1,2,3), array(4,5,6), array(7,8,9) ); print_r(array_column($a,'0')); ?> Expected results should be : Array ( [0] => 1 [1] => 4 [2] => 7 ) But we get : Array ( ) Instead I have create another function. <?php function my_array_column($array,$colName){ $results=array(); if(!is_array($array)) return $results; foreach($array as $child){ if(!is_array($child)) continue; if(array_key_exists($colName,$child)){ $results[] = $child[$colName]; } } return $results; } $a =array( array(1,2,3), array(4,5,6), array(7,8,9) ); print_r(my_array_column($a,'0')); ?> Now we get the right results: Array ( [0] => 1 [1] => 4 [2] => 7 )

« previous php.notes (#199799) next »