Jump to content

How To Transform A Simple Php Array To Associative Php Array


subuntug

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...