note 35732 added to function.array

From: Date: Fri, 12 Sep 2003 13:44:55 +0000
Subject: note 35732 added to function.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-56392@lists.php.net to get a copy of this message
I was really hoping that PHP had a function to associate two seperate arrays together, i.e. map the values from one to the values of the other where the keys match. I tried doing this: $moo = array( array(1,2)=>array("one","two") ); Which would have been how I'd implemented it if I'd written PHP, but it doesn't work :O( Curiously though it doesn't create an error or a warning, even with warnings set to E_ALL (I'm using PHP 4.3.0). The results is an empty array. Before anyone flames me - I know it says above that the key in an association must be a string or an index. My problem is that I have two arrays (sample data here): $arr1 = array("a"=>"1", "b"=>"2"); $arr2 = array("a"=>"one", "b"=>"two"); and I can't find a function to join them together, to achieve: $arr = array("1"=>"one", "2"=>"two"); Is the best (and only) way to do: // Doesn't matter which array we iterate over because // only want keys that are in BOTH arrays anyway. foreach( $arr2 as $key=>$value ) { if( isset($arr1[$key])) { $result[ $arr1[$key] ] = $value; } } Also it might be useful if range() could return an array with ranges set in both the keys and the values :O) ---- Manual Page -- http://www.php.net/manual/en/function.array.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35732 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35732&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35732&report=yes

« previous php.notes (#56392) next »