note 93301 modified in function.array-map by danbrown

From: Date: Wed, 02 Sep 2009 02:42:58 +0000
Subject: note 93301 modified in function.array-map by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160149@lists.php.net to get a copy of this message
My English is weak but I want to share my code. I want to change formation of array but when I use null is callback function in array_map(). It has problem in result so I write new small function for it. <?php function array_change_formation($arr=array()) { $new_arr = array(); foreach($arr as $k => $v) foreach($v as $k2 => $v2) $new_arr[$k2][$k] = $v[$k2]; return $new_arr; } ?> Input: Array ( [Human] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) [Pet] => Array ( [0] => Cat [1] => Dog [2] => Rabbit [3] => Rat ) ) OutPut: Array ( [0] => Array ( [Human] => 1 [Pet] => Cat ) [1] => Array ( [Human] => 2 [Pet] => Dog ) [2] => Array ( [Human] => 3 [Pet] => Rabbit ) [3] => Array ( [Human] => 4 [Pet] => Rat ) ) I hope it can useful. --was-- My English is weak but I want to share my code. I want to change formation of array but when I use null is callback function in array_map(). It has problem in result so I write new small function for it. function array_change_formation($arr=array()) { $new_arr = array(); foreach($arr as $k => $v) foreach($v as $k2 => $v2) $new_arr[$k2][$k] = $v[$k2]; return $new_arr; } Input: Array ( [Human] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) [Pet] => Array ( [0] => Cat [1] => Dog [2] => Rabbit [3] => Rat ) ) OutPut: Array ( [0] => Array ( [Human] => 1 [Pet] => Cat ) [1] => Array ( [Human] => 2 [Pet] => Dog ) [2] => Array ( [Human] => 3 [Pet] => Rabbit ) [3] => Array ( [Human] => 4 [Pet] => Rat ) ) I hope it can useful. http://php.net/manual/en/function.array-map.php

« previous php.notes (#160149) next »