0

So as the question states, im trying to create an array using a for loop, this seems as though its a simple question, but i cant find the asnwer on SO or googling. Heres what im doing:

$twelve=array("user","day");

for($i=0; $i<$value; $i++)
{
    $total=$anarray[$i][value]; //get a value

    $twelve[$i]=($i,$total);  //insert values into array
}

this doesn't work, how should i go about getting this to work?

3 Answers 3

1

You may end up in a never-ending loop if $total=$anarray[$i][value]; is an increasing value. Regardless of the loop, you'll want to do as the other answerer mentioned, namely: $twelve[$i] = array($i, $total);

Sign up to request clarification or add additional context in comments.

1 Comment

yeah i saw that, it was a typo, however, using the initial answer is not working and giving me a blank answer
1

I think it should be $twelve[$i] = array($i, $total);

Also, on this line;

$total=$anarray[$i][value]; //get a value

Unless value is defined as a constant, I think you want to do $anarray[$i][$value];. PHP might not recognize value as a set variable or a constant, therefore crashes and never sets $twelve to any value.

Comments

0

I have found a solution that does work

in each loop simply do:

$twelve[$i]["user"]=$i;
$twelve[$i]["day"]=$total;

It would be nice if there is a way to do that in one line, but that is working.

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.