Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Numerically Indexed Arrays

arrays

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 uniqueideaman

uniqueideaman

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts

Posted 20 May 2017 - 03:23 AM

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


#2 uniqueideaman

uniqueideaman

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts

Posted 20 May 2017 - 04:28 AM

Guys,
 
I'm referring to this tutorial:
 
It shows 2 methods of creating an array:
 
 
<?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:
 
 
<?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 
 
 
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);

Edited by uniqueideaman, 20 May 2017 - 04:49 AM.