Numerically Indexed Arrays
Moderator: General Moderators
-
UniqueIdeaMan
- Forum Contributor
- Posts: 197
- Joined: Wed Jan 18, 2017 3:43 pm
Numerically Indexed Arrays
Fellow Php Buds,
Do you mind telling me, according to you:
1. How many variables are listed below ? And,
2. How many numerically Indexed Arrays ?
$employee_array[0] = "Bob";
$employee_array[1] = "Sally";
$employee_array[2] = "Charlie";
$employee_array[3] = "Clare";
Thank You
Do you mind telling me, according to you:
1. How many variables are listed below ? And,
2. How many numerically Indexed Arrays ?
$employee_array[0] = "Bob";
$employee_array[1] = "Sally";
$employee_array[2] = "Charlie";
$employee_array[3] = "Clare";
Thank You
Re: Numerically Indexed Arrays
There's one variable, and it's an array, so the answer to both questions is one.
-
UniqueIdeaMan
- Forum Contributor
- Posts: 197
- Joined: Wed Jan 18, 2017 3:43 pm
Re: Numerically Indexed Arrays
Guys,
I'm referring to this tutorial:
https://www.tutorialspoint.com/php/php_arrays.htm
It shows 2 methods of creating an array:
On the first method, I just switched the numerical numbers to numbers in words but I get error! It should've worked no matter what the values of the arrays are on (note the first method below).
I changed it to:
This is the result I get:
Notice: Use of undefined constant one - assumed 'one' in C:\xampp\htdocs\test\test.php on line 41
Notice: Use of undefined constant two - assumed 'two' in C:\xampp\htdocs\test\test.php on line 41
Notice: Use of undefined constant three - assumed 'three' in C:\xampp\htdocs\test\test.php on line 41
Notice: Use of undefined constant four - assumed 'four' in C:\xampp\htdocs\test\test.php on line 41
Notice: Use of undefined constant five - assumed 'five' in C:\xampp\htdocs\test\test.php on line 41
Value is one
Value is two
Value is three
Value is four
Value is five
Value is one
Value is two
Value is three
Value is four
Value is five
I'm referring to this tutorial:
https://www.tutorialspoint.com/php/php_arrays.htm
It shows 2 methods of creating an array:
Code: Select all
<?php
/* First method to create array. */
$numbers = array( one, two, three, four, five);
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
/* Second method to create array. */
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
?>
</body>
</html>On the first method, I just switched the numerical numbers to numbers in words but I get error! It should've worked no matter what the values of the arrays are on (note the first method below).
I changed it to:
Code: Select all
<?php
//2 examples On How To Create Numeric Arrays:
?>
<html>
<body>
<?php
/* First method to create array. */
$numbers = array( one, two, three, four, five);
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
/* Second method to create array. */
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
?>
</body>
</html>Notice: Use of undefined constant one - assumed 'one' in C:\xampp\htdocs\test\test.php on line 41
Notice: Use of undefined constant two - assumed 'two' in C:\xampp\htdocs\test\test.php on line 41
Notice: Use of undefined constant three - assumed 'three' in C:\xampp\htdocs\test\test.php on line 41
Notice: Use of undefined constant four - assumed 'four' in C:\xampp\htdocs\test\test.php on line 41
Notice: Use of undefined constant five - assumed 'five' in C:\xampp\htdocs\test\test.php on line 41
Value is one
Value is two
Value is three
Value is four
Value is five
Value is one
Value is two
Value is three
Value is four
Value is five
Last edited by UniqueIdeaMan on Sat May 20, 2017 7:39 am, edited 2 times in total.
Re: Numerically Indexed Arrays
That's not going to work. Strings need to be wrapped in quotes, otherwise PHP is going to think they're constants, hence the warning you got. Otherwise, what are you trying to do here?Code: Select all
$numbers = array( one, two, three, four, five);
-
UniqueIdeaMan
- Forum Contributor
- Posts: 197
- Joined: Wed Jan 18, 2017 3:43 pm
Re: Numerically Indexed Arrays
Tried to get it to spit values like it did. It did what I wanted but it showed errors.Celauran wrote:That's not going to work. Strings need to be wrapped in quotes, otherwise PHP is going to think they're constants, hence the warning you got. Otherwise, what are you trying to do here?Code: Select all
$numbers = array( one, two, three, four, five);
My point:
If following works without any errors then the further following too should work:
/* First method to create array. Example, according to tutorial. This sows no errors. */
$numbers = array( 1, 2, 3, 4, 5);
/* First method to create array. Example, according to my editing. This shows errors. */
$numbers = array( one, two, three, four, five);
Re: Numerically Indexed Arrays
No. Strings need to be wrapped in quotes. The above would only work if you had previously defined one, two, three, four, and five as constants.UniqueIdeaMan wrote:If following works without any errors then the further following too should work:
/* First method to create array. Example, according to tutorial. This sows no errors. */
$numbers = array( 1, 2, 3, 4, 5);
/* First method to create array. Example, according to my editing. This shows errors. */
$numbers = array( one, two, three, four, five);
This is probably what you're after
Code: Select all
$numbers = ['one', 'two', 'three', 'four', 'five'];-
UniqueIdeaMan
- Forum Contributor
- Posts: 197
- Joined: Wed Jan 18, 2017 3:43 pm
Associative Arrays
Associative Array
Numerical Array
Look at both code's bold parts on how each different type of arrays create an array.
The Associative Array:
/* First method to associate create array. */
$salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
The Numerical Array:
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
Why one uses "=>" and one not ? Are they not both procedural style ?
Look:
https://www.tutorialspoint.com/php/php_arrays.htm
Code: Select all
<html>
<body>
<?php
/* First method to associate create array. */
[b] $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);[/b]
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
/* Second method to create array. */
$salaries['mohammad'] = "high";
$salaries['qadir'] = "medium";
$salaries['zara'] = "low";
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
?>
</body>
</html>
Numerical Array
Code: Select all
<?php
/* First method to create array. */
$numbers = array( one, two, three, four, five);
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
/* Second method to create array. */
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
?>
</body>
</html>
The Associative Array:
/* First method to associate create array. */
$salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
The Numerical Array:
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
Why one uses "=>" and one not ? Are they not both procedural style ?
Look:
https://www.tutorialspoint.com/php/php_arrays.htm
Re: Numerically Indexed Arrays
One uses => because you're specifying both keys and values. Procedural has nothing to do with anything here.
-
UniqueIdeaMan
- Forum Contributor
- Posts: 197
- Joined: Wed Jan 18, 2017 3:43 pm
Re: Numerically Indexed Arrays
Thanks Celeraun,Celauran wrote:No. Strings need to be wrapped in quotes. The above would only work if you had previously defined one, two, three, four, and five as constants.UniqueIdeaMan wrote:If following works without any errors then the further following too should work:
/* First method to create array. Example, according to tutorial. This sows no errors. */
$numbers = array( 1, 2, 3, 4, 5);
/* First method to create array. Example, according to my editing. This shows errors. */
$numbers = array( one, two, three, four, five);
This is probably what you're afterCode: Select all
$numbers = ['one', 'two', 'three', 'four', 'five'];
So, many programmers have told me that the worded numbers are strings and I should single quote them.
Wrong way:
$numbers = array( one, two, three, four, five);
Right way:
$numbers = array( 'one', 'two', 'three', 'four', 'five');
But how come the following is valid then without the single quotes:
$numbers = array( 1, 2, 3, 4, 5);
Why need quotes on one and not the other ?
I was re-learning from:
https://www.tutorialspoint.com/php/php_arrays.htm
Re: Numerically Indexed Arrays
Because integers aren't strings.