SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    linking 2 arrays

    Hi, I am having an issue with arrays.

    I have a bunch of factors with a series of information that corresponds to them but the seconday series is not appearing correctly.

    EXAMPLE:
    PHP Code:
    $factor[1] = 'Colors';
    $choice[1] = 'red';
    $choice[2] = 'black';
    $choice[3] = 'green';

    $factor[2] = 'Fruit';
    $choice[1] = 'apple';
    $choice[2] = 'peaches';
    $choice[3] = 'bannanna';

    $i=0;
    foreach (
    $factor as $f){
    $i++;
    echo 
    "$factor[$i]";
    echo 
    "Choices are as follows:<br />";
    echo 
    $choices[$i];

    I've tried a couple of different variable names for the array such as:

    PHP Code:
    $choice[1][1] ='apple';
    $choice[1][2] ='peaches';
    $choice[1][3] ='bannanna';

    //and tried calling back like the following:
    $choice1[1] ='apple';
    $choice1[2] ='peaches';
    $choice1[3] ='bannanna';

    foreach(){
    $i++;
    echo 
    $factor[$i]
    echo 
    "Choices are as follows:<br />";
    echo 
    $choices$i[$i];


    but i just keep getting Array displayed on screen. Any ideas?


    Thanks...

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's because you're tryng to echo an array, which you can't do. An array inside an array is still an array.

    Either use var_dump() or print_r() instead of echo to output the whole array, or select a key to echo.

    e.g.
    $choices[0][1];
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    That's because you're tryng to echo an array, which you can't do. An array inside an array is still an array.

    Either use var_dump() or print_r() instead of echo to output the whole array, or select a key to echo.

    e.g.
    $choices[0][1];
    thank you.

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, didnt work, now i just get the word Array once for each variable in the array.

    ArrayArrayArray

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok, what's the exact code you're using at the moment?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $choice[1][1] ='apple';
    $choice[1][2] ='peaches';
    $choice[1][3] ='bannanna';

    foreach( 
    $choice as $index ){
      foreach( 
    $index as $key=>$value){
      echo 
    $value;
      }


  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    PHP Code:
    $choice[1][1] ='apple';
    $choice[1][2] ='peaches';
    $choice[1][3] ='bannanna';

    foreach( 
    $choice as $index ){
      foreach( 
    $index as $key=>$value){
      echo 
    $value;
      }

    Perfect works great, thanks..


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
  •