Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Enum arrays

  1. #1
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Enum arrays

    PHP Code:
    <?php
    $myarray 
    = array(
     [
    0] => 'string a',
     [
    2] => 'string b',
     [
    4] => 'string c',
    [
    8] => 'string d',
    ); 
    $arraycount count$myarray );
    for( 
    $i 0$i $arraycount$i++ ) {
      echo  
    'key is: ' $i ' - value is ' $myarray[$i] . '<br />';
    }
    ?>
    Why is the above code showing error as follows:
    Parse error: syntax error, unexpected '[', expecting ')' in ....\xampp\htdocs\....\1.php on line 3

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Code:
    [8] => 'string d',
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    But that is already in the code
    Code:
    [8] => 'string d',
    Where is the error?

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The last item in an array (or any other kind of comma separated list) must NOT have a comma, or it will cause an error.

    By the way you're going to get a lot of warnings about undefined indices if you do it like that. You'd need to count by two in the for loop, or you could use a foreach loop, which is specifically designed for arrays.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Ya i intentionally put those indices like that. ;-) :-P But i removed the last comma still it i giving error

  6. #6
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    I put it like this according to your suggestion@djr33,but still i get the same error!
    Code:
    <?php
    $myarray = array(
     [0] => 'string a',
     [2] => 'string b',
     [4] => 'string c',
     [8]=> 'string d'); 
    $arraycount = count( $myarray );
    for( $i = 0; $i < $arraycount; $i++ ) {
      echo  'key is: ' . $i . '  value is ' . $myarray[$i] . '<br />';
    }
    ?>

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oh, you don't put [] around array keys. That's just when it's being displayed to you (such as with the print_r() function), or when you are accessing an individual item from the array with $array[0] or $array['key'].

    PHP Code:
    <?php
    $myarray 
    = array(
     
    => 'string a',
     
    => 'string b',
     
    => 'string c',
     
    8=> 'string d'); 
    $arraycount count$myarray );
    for( 
    $i 0$i $arraycount$i++ ) {
      echo  
    'key is: ' $i '  value is ' $myarray[$i] . '<br />';
    }
    ?>
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Ohh Thanks I overlooked that!!
    :-P

  9. #9
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Thanks for helping..I overlooked that

  10. #10
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Please try the below code

    <?php
    $myarray = array(
    0 => 'string a',
    2 => 'string b',
    4 => 'string c',
    8 => 'string d',
    );

    foreach($myarray as $x=>$x_val)
    {
    echo 'key is: ' . $x . ' - value is ' . $x_val . '<br />';
    }
    ?>

Similar Threads

  1. how to insert values into enum fields in mysql
    By janunme in forum MySQL and other databases
    Replies: 7
    Last Post: 09-18-2011, 06:56 AM
  2. Arrays
    By lindsaycb in forum PHP
    Replies: 0
    Last Post: 02-12-2009, 06:09 PM
  3. Resolved These two arrays look the same - JS says they aren't.
    By jlizarraga in forum JavaScript
    Replies: 2
    Last Post: 11-03-2008, 08:55 PM
  4. Resolved ENUM Datatype error
    By vineet in forum MySQL and other databases
    Replies: 1
    Last Post: 10-23-2008, 01:59 PM
  5. can one change enum w/o losing content?
    By quint in forum MySQL and other databases
    Replies: 0
    Last Post: 06-04-2007, 07:07 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •