How to add elements to an empty array in PHP?

后端 未结 8 604
天命终不由人
天命终不由人 2020-11-22 12:07

If I define an array in PHP such as (I don\'t define its size):

$cart = array();

Do I simply add el

8条回答
  •  忘了有多久
    2020-11-22 12:14

    It's better to not use array_push and just use what you suggested. The functions just add overhead.

    //We don't need to define the array, but in many cases it's the best solution.
    $cart = array();
    
    //Automatic new integer key higher than the highest 
    //existing integer key in the array, starts at 0.
    $cart[] = 13;
    $cart[] = 'text';
    
    //Numeric key
    $cart[4] = $object;
    
    //Text key (assoc)
    $cart['key'] = 'test';
    

提交回复
热议问题