Page 1 of 1

Numerically Indexed Arrays

Posted: Sat May 20, 2017 6:23 am
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

Re: Numerically Indexed Arrays

Posted: Sat May 20, 2017 6:48 am
by Celauran
There's one variable, and it's an array, so the answer to both questions is one.

Re: Numerically Indexed Arrays

Posted: Sat May 20, 2017 7:27 am
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

Re: Numerically Indexed Arrays

Posted: Sat May 20, 2017 7:32 am
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?

Re: Numerically Indexed Arrays

Posted: Sat May 20, 2017 7:41 am
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);

Re: Numerically Indexed Arrays

Posted: Sat May 20, 2017 7:59 am
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'];

Associative Arrays

Posted: Sat May 20, 2017 8:05 am
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

Re: Numerically Indexed Arrays

Posted: Sat May 20, 2017 12:24 pm
by Celauran
One uses => because you're specifying both keys and values. Procedural has nothing to do with anything here.

Re: Numerically Indexed Arrays

Posted: Sun May 21, 2017 6:14 pm
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

Re: Numerically Indexed Arrays

Posted: Mon May 22, 2017 5:32 am
by Celauran
Because integers aren't strings.