Skip to content

Instantly share code, notes, and snippets.

@slav123
Last active October 10, 2015 19:48
Show Gist options
  • Save slav123/3742268 to your computer and use it in GitHub Desktop.
Save slav123/3742268 to your computer and use it in GitHub Desktop.
make assoc
<?php
/**
* @param $arr
* @param string $key
*
* @return void
*/
function make_assoc(&$arr, $key = 'id')
{
$tmp = $arr;
$max = count($arr);
$kold = array();
$knew = array();
for ($a = 0; $a < $max; $a ++) {
$kold[] = $a;
if (isset($tmp[$a][$key])) {
$knew[] = $tmp[$a][$key];
$arr[$tmp[$a][$key]] = $tmp[$a];
}
}
unset($tmp);
$dif = array_diff($kold, $knew);
foreach ($dif as $d) unset($arr[$d]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment