note 35732 deleted from function.array by didou
| From: | didou@php.net | Date: | Thu, 17 Jun 2004 20:06:46 +0000 |
| Subject: | note 35732 deleted from function.array by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71249@lists.php.net to get a copy of this message | ||
Note Submitter: unluckypixie
----
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)