CodeIgniter Forums
Unable to add to array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Unable to add to array (/showthread.php?tid=82133)



Unable to add to array - Sincere - 06-15-2022

I've done this many times before: adding an array to an yet already existing array, but for some reason this isn't working now, and I'm out of clues:
PHP Code:
$influx_server_data = [];
$stats = [
                    'health'        =>  $health,
                    'total_live'    =>  $server->live_total,
//etc etc/
                    'mem_used'      =>  $server->system_stats->memory->used,
                    'mem_total'      =>  $server->system_stats->memory->total,
                ];
                $servers_model->update($live_server->id,$stats);

                //Influx Magic

                // Server influx data
                foreach($stats as $measurement => $value){
                    $influx_server_point = [$measurement//name of measurement
                        $value//measurement value
                        ['host' => $live_server->hostname'region' => $live_server->region], // optional tags
                        $now
                    
];
                    print_r($influx_server_point);
                    $influx_server_data $influx_server_data + [$influx_server_point];
                }
                print_r($influx_server_data); 

But for some reason $influx_server_data will only have the array from the first run (health in this case). The first print_r confirms that the foreach works fine and shows the desired array for each run.
I'd be grateful for any insights!


RE: Unable to add to array - Sincere - 06-15-2022

@kenjis helped solve this:
I had to do the following:
PHP Code:
$influx_server_data[] = [$influx_server_point]; 



RE: Unable to add to array - InsiteFX - 06-16-2022

PHP Code:
$result array_merge($array1$array2);