How to add an array value to the middle of an associative array?

前端 未结 13 1633
余生分开走
余生分开走 2020-12-08 00:36

Lets say I have this array:

$array = array(\'a\'=>1,\'z\'=>2,\'d\'=>4);

Later in the script, I want to add the value \'c\'=>3<

13条回答
  •  太阳男子
    2020-12-08 01:20

    you can add it by doing

    $array['c']=3;
    

    and if you absolutely want it sorted for printing purposes, you can use php's ksort($array) function

    if the keys are not sortable by ksort, then you will have to create your own sort by using php's uasort function. see examples here

    http://php.net/manual/en/function.uasort.php

提交回复
热议问题