PHP Append an array to a sub array

后端 未结 2 1543
攒了一身酷
攒了一身酷 2021-01-28 10:01

I need to make an array of locations arrays, it must look like this

$relevanceKeys = array(
    \'locations\' => array(

        array(
            \'longitud         


        
相关标签:
2条回答
  • 2021-01-28 10:32

    Since you're only needing to push elements to the array, you can simply use the [] struct as follows:

    while ($row = $result->fetch_object()) 
    {
        $relevanceKeys['locations'][] = array (
            'longitude' => $row->longitude,
            'latitude' => $row->latitude
        );
    } 
    
    0 讨论(0)
  • 2021-01-28 10:45

    Just append the array like this:

    $relevanceKeys['locations'][] = $array;
                             //^^ See here
    

    You don't have to merge them all the time!

    Also I think you want to change this:

    $array = array(
                 array (
                     'longitude' => $row->longitude,
                     'latitude' => $row->latitude
                 )
           );
    

    to this to get your expected structure:

    $array = array(
                 'longitude' => $row->longitude,
                 'latitude' => $row->latitude
           );
    

    For more information about array see the manual: http://php.net/manual/en/language.types.array.php

    0 讨论(0)
提交回复
热议问题