How to add elements to an empty array in PHP?

后端 未结 8 585
天命终不由人
天命终不由人 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:31
    $products_arr["passenger_details"]=array();
    array_push($products_arr["passenger_details"],array("Name"=>"Isuru Eshan","E-Mail"=>"isuru.eshan@gmail.com"));
    echo "<pre>";
    echo json_encode($products_arr,JSON_PRETTY_PRINT);
    echo "</pre>";
    
    //OR
    
    $countries = array();
    $countries["DK"] = array("code"=>"DK","name"=>"Denmark","d_code"=>"+45");
    $countries["DJ"] = array("code"=>"DJ","name"=>"Djibouti","d_code"=>"+253");
    $countries["DM"] = array("code"=>"DM","name"=>"Dominica","d_code"=>"+1");
    foreach ($countries as $country){
    echo "<pre>";
    echo print_r($country);
    echo "</pre>";
    }
    
    0 讨论(0)
  • 2020-11-22 12:36

    When one wants elements to be added with zero-based element indexing, I guess this will work as well:

    // adding elements to an array with zero-based index
    $matrix= array();
    $matrix[count($matrix)]= 'element 1';
    $matrix[count($matrix)]= 'element 2';
    ...
    $matrix[count($matrix)]= 'element N';
    
    0 讨论(0)
提交回复
热议问题