subuntug Posted November 26, 2009 Report Share Posted November 26, 2009 Hi; I this this array: $myArray = array( array( id1, item1, item2), array(id2, item1, item2), array(id3, item1, item2) ) What I want to do is to transform the above array to an associative array, in other words, when I use the function print_r($myArray), I want to see this: Array( id1 => array(0=>id1, 1=>item1, 2=>item2), id2 => array(0=>id1, 1=>item1, 2=>item2), id3 => array(0=>id1, 1=>item1, 2=>item2) ) Thanks in advence. Link to comment Share on other sites More sharing options...
jeffman Posted November 26, 2009 Report Share Posted November 26, 2009 This code: $myArray = array( array('id1', 1, 2), array('id2', 11, 12), array('id3', 21, 22));foreach ($myArray as $item) { $new[$item[0]] = $item;}print_r($new); gives this result: Array( [id1] => Array ( [0] => id1 [1] => 1 [2] => 2 ) [id2] => Array ( [0] => id2 [1] => 11 [2] => 12 ) [id3] => Array ( [0] => id3 [1] => 21 [2] => 22 )) Link to comment Share on other sites More sharing options...
subuntug Posted November 27, 2009 Author Report Share Posted November 27, 2009 Thanks so much, it works perfectly.PHP is so powerful ! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now