Popping the key & value from an associative array in PHP

前端 未结 4 1688
梦谈多话
梦谈多话 2021-02-07 00:14

Let S be an associative array in PHP, I need to retrieve and extract from it the first element, both the value and the key.

I would use

value1=array_pop         


        
4条回答
  •  暖寄归人
    2021-02-07 00:31

    array_slice

    $arr = array('k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3');
    
    $a = array_slice($arr, 0, 1);
    var_dump($a);
    
    $arr = array_slice($arr, 1);
    var_dump($arr);
    
    
    array(1) {
      ["k1"]=>
      string(2) "v1"
    }
    array(2) {
      ["k2"]=>
      string(2) "v2"
      ["k3"]=>
      string(2) "v3"
    }
    

提交回复
热议问题