Numeric Array to Associative Array


/ Published in: PHP
Save to your folder(s)

This requires a one dimensional array.


Copy this code and paste it in your HTML
  1. function array_numeric_to_assoc($arr) {
  2. $arr = array_values($arr);
  3. $new_arr = array();
  4. for ($i = 0, $count = count($arr); $i < $count; $i += 2) {
  5. $new_arr[$arr[$i]] = isset($arr[$i + 1]) ? $arr[$i + 1] : null;
  6. }
  7. return $new_arr;
  8. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.