PHP Array

creative-2008

Weaksauce
Joined
Dec 23, 2008
Messages
100
I'm running a while loop in PHP which I would like to add variables to an array. I've googled this and searched the PHP manual extensively, and either haven't found it or haven't realised that I've found it!

This is an exert from the code that I'm running:

PHP:
While ($current_question <= $num_question):

$multiplicand = array($current_question => rand(1,12),);

print_r($multiplicand[$current_question]);

print_r($multiplier[$current_question]);

$current_question = ($current_question + 1);

endwhile;

print_r($multiplicand['1']);

However, I've tried printing the whole array later and I don't think that I'm actually storing the data in the array correctly. I think that it is instead just storing the last data inputted into the array. The final line of code only prints a value when $num_question is 1.

Can anybody point out to me what I've got wrong? Or where I can find information about arrays that I'm more likely to understand than the PHP handbook.
 
I think I may have found the (or an) answer in an unlikely place, in the form of the count function in the PHP handbook.

I'm now adding to the array with this line of code.

PHP:
$multiplier[$current_question] = rand(1,12);

It seems to be working. Is it the correct way of adding to an array in this way?
 
Couple tips:

1. You can add one to a variable by doing $variable++; instead of $variable = $variable + 1;.
2. You can add an element to the end of an array by using $array[] = $value;.
3. The 2nd comma in the 2nd line is unnecessary.

It seems as if your whole code block could be simplified to something such as:

Code:
$multiplicand = [url=http://php.net/manual/en/function.array-fill.php]array_fill[/url](0, $num_questions, rand(1,12));
or (if you need each value to be unique)
Code:
$multiplicand = [url=http://php.net/manual/en/function.range.php]range[/url](1, 12);
[url=http://php.net/manual/en/function.shuffle.php]shuffle[/url]($multiplicand);

It might also be worthwhile for you to look through PHP's language reference.
 
Last edited:
Thanks very much for your tips. I'll look into the link shortly.

1 and 3 make sense right away.

2) The first time I use $array[] = $value; will it set it to position 0 in the array? If so is there any way to avoid this and have it set it to position 1?
 
The first time you use $array[] = $value, it'll create the $array variable if it doesn't exist and add $value at positon 0.

The only way to start at position (key) 1 is to declare $array beforehand with key 0, then at the end, remove the first element.

Some trickery, such as this, should work (probably unnecessary for your exercise):
Code:
$array = Array(null); //sets 0 => NULL
while(...) {
     $array[] = $value;
}
array_shift($array); //removes the first element (NULL)

The PHP manual has a very thorough look at arrays.


Edit: Ignore the quote, array_shift() resets keys: "All numerical array keys will be modified to start counting from zero while literal keys won't be touched."

Why do you want to start at key 1 instead of 0?


Edit 2: Found a way to keep the array starting at 1:
Code:
$array = Array(null); //sets the first element to NULL
while(...) {
    $array[] = $value;
}
$array = array_reverse($array,true); //reverse the array, NULL is now the last element (keys are preserved)
array_pop($array); //removes the last element, NULL
$array = array_reverse($array,true); //reverse the array back to its original ornentation, preserving keys

However, the question still remains.
 
Last edited:
Thanks again for all of your posts. I'll look into your suggestions.

The reason that I want the array to start counting at 1 is because it is used to store numbers that relate to questions. So I'd prefer that question number 1 is stored in value 1 in the array, to prevent me getting confused later in the script. Am I making it more complicated than it should be?
 
I went back and edit my last post quote a few times so it might be worth another look.

I think it'd be best to get used to using 0 as the key for the first element for numerous reasons.

1. That's how PHP was designed. It's assumed everywhere that the first element is key 0.
2. It takes a bit of code to get your array to start at key 1 unless you define key 1's value beforehand.
3. As you become more experienced you might start to wonder why your code doesn't work properly, only to later find out it's because of that.
4. I made these up.

I'm sure someone else with a greater knowledge of PHP can give you better reasons.
 
I agree with Snowknight26.

The argument for changing the index logic of an array is based solely on the current presentation layout, which is simply not strong enough for overriding the language's default behavior IMO. Keep your display logic separate from your enumeration logic.
 
The reason that I want the array to start counting at 1 is because it is used to store numbers that relate to questions. So I'd prefer that question number 1 is stored in value 1 in the array, to prevent me getting confused later in the script. Am I making it more complicated than it should be?
Short answer: yes. There are certain assumptions regarding arrays in PHP that can and will be made by other developers that you're looking to violate by doing one-based indexing (having the beginning of the array start at index/key 1). If no one else is going to interact with this code, it's not a big deal, but zero-based arrays are the norm in programming languages, and it's useful to wrap this construct around your mind early.

Think of it as holding to language conventions: doing so, regardless of language, is rarely a mistake.
 
Back
Top