Numerically Indexed Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Numerically Indexed Arrays

Post by UniqueIdeaMan »

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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Numerically Indexed Arrays

Post by Celauran »

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

Post by UniqueIdeaMan »

Guys,

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>
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
Last edited by UniqueIdeaMan on Sat May 20, 2017 7:39 am, edited 2 times in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Numerically Indexed Arrays

Post by Celauran »

Code: Select all

 $numbers = array( one, two, three, four, five);
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?
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: Numerically Indexed Arrays

Post by UniqueIdeaMan »

Celauran wrote:

Code: Select all

 $numbers = array( one, two, three, four, five);
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?
Tried to get it to spit values like it did. It did what I wanted but it showed errors.

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);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Numerically Indexed Arrays

Post by Celauran »

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);
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.

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

Post by UniqueIdeaMan »

Associative Array

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>

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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Numerically Indexed Arrays

Post by Celauran »

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

Post by UniqueIdeaMan »

Celauran wrote:
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);
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.

This is probably what you're after

Code: Select all

$numbers = ['one', 'two', 'three', 'four', 'five'];
Thanks Celeraun,

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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Numerically Indexed Arrays

Post by Celauran »

Because integers aren't strings.
Post Reply