Return to Snippet

Revision: 41715
at February 22, 2011 22:31 by slav123


Initial Code
public function make_assoc(&$arr, $key = 'id') {
	$tmp = $arr;

	$max = count($arr);
	$kold = array();
	$knew = array();
	for ($a = 0;$a<$max;$a++) {
	    $kold[] = $a;
	    $knew[] = $tmp[$a][$key];
	    $arr[$tmp[$a][$key]] = $tmp[$a];
	}

	unset($tmp);
	$dif = array_diff($kold,$knew);
	foreach ($dif as $d) unset($arr[$d]);
    }

Initial URL

                                

Initial Description
converts array (which is reference) in format 

<code>
array(0 => array('id' => 1, 'name' => 'orange'), 1 => array('id' => 3, 'name' => 'banana'));
</code>

to

<code>
array(1 => array('id' => 1, 'name' => 'orange'), 3 => array('id' => 3, 'name' => 'banana'));
</code>

Initial Title
create associative array from any index array

Initial Tags
php, array

Initial Language
PHP