Switch two items in associative array

后端 未结 17 1881
自闭症患者
自闭症患者 2021-02-05 12:53

Example:

$arr = array(
  \'apple\'      => \'sweet\',
  \'grapefruit\' => \'bitter\',
  \'pear\'       => \'tasty\',
  \'banana\'     => \'yellow\'
)         


        
17条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 13:19

    Arrays in php are ordered maps.

    $arr = array('apple'=>'sweet','grapefruit'=>'bitter','
    pear'=>'tasty','banana'=>'yellow');
    

    doesn't mean that that the first element is 'apple'=>'sweet' and the last - 'banana'=>'yellow' just because you put 'apple' first and 'banana' last. Actually, 'apple'=>'sweet' will be the first and 'banana'=>'yellow' will be the second because of alphabetical ascending sort order.

提交回复
热议问题