Last active
October 10, 2015 19:48
-
-
Save slav123/3742268 to your computer and use it in GitHub Desktop.
make assoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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