Add variable to array without using array push

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Idiot88
New php-forum User
New php-forum User
Posts: 19
Joined: Thu Jan 25, 2018 2:56 pm

Fri Feb 09, 2018 3:25 pm

Hi

I want to add to a variable to the end of an array without the overhead of calling array_push. This is my code:

$sklabels = array ();
for( $count = 0 ; $count < 44; $count++)
{
$j = "sk" . $count;
array_push( $sklabels, $j );
}

Please will you give me example code that doesn’t use array_push?

Thanks in advance.

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 54
Joined: Mon Oct 16, 2017 1:06 pm

Fri Feb 09, 2018 6:25 pm

Code: Select all

<?php
$sklabels   = [];
$sklabels[] = 'var1';
$sklabels[] = 'var2';
$sklabels[] = 'var3';

print_r($sklabels);
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

Post Reply