1

I know this easy but I still can't solve it, I have this piece of code :

$names = [
    '1' => 'name1',
    '2' => 'name2',
    '3' => 'name3',
];  

It's simple for small number array, but how if i get many data and always change?, I plan to use for loop

$totaldata = 5    

for($z=1; $z<=$totaldata; $z++) {
    $yz = name.$z;

    $names = [
        $z => $yz,
    ];
}

but somehow it doesn't work, any solution?

1
  • how to loop that 1 => name1 ... and so on inside array, say if there are 100 data and change every minutes it really are impossible to change it and add new data manually every minute. Commented Apr 2, 2016 at 5:17

1 Answer 1

1

The problem is that you do not add new data to $names array, you just overwrite it all the time.

So change this code:

$names = [
        $z => $yz,
    ];

to this:

$names[$z] = $yz;

It is also a good point to initialize $names before loop. So the result should be like this:

$totaldata = 5    
$names = [];

for($z=1; $z<=$totaldata; $z++) {
    $yz = name.$z;

    $names[$z] = $yz;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.