SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    multidimensional array and foreach

    hey, please check this code:

    PHP Code:
    $poll = array ( "options" => array ( "option1""option2","option3"),
                      
    "votes" => array ( 40,20,10)                  
                    );
    foreach (
    $poll as $value) {
        echo 
    $value[0];

    this results in output "option140"

    now if I want to display it like (for instance):

    <input type=''text" name="option" value="option1"><input type="text" name="votes" value="40">

    where as the next foreach would result in option2 and votes "20"

    how would I manage this? thanks in advance

  2. #2
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you should re-arrange you array:
    PHP Code:
    $poll = array(
       
    => array(
          
    name => option1,
          
    value => 40
       
    )
       
    => array(
          
    name => option2,
          
    value => 20
       
    )
       
    => array(
          
    name => option3,
          
    value => 10
       
    )
    ); 
    Now your foreach is easier:
    PHP Code:
    foreach($poll as $array) {
        
    $name $array['name'];
        
    $value $array['value'];
        echo 
    "<input type=\''text\" name=\"option\" value=\"".$name."\"><input type=\"text\" name=\"votes\" value=\"".$value."\">";


  3. #3
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    	$poll = array (
    		"options" => array ( "option1", "option2","option3"),
    		"votes" => array ( 40,20,10)                  
    	);
    
    	$option_num = 0;
    	foreach ($poll['options'] as $option) {
    		echo "<input type='text' name='option[]' value='$option'><input type='text' name='votes[]' value='" . $poll['votes'][$option_num] . "'><br/>";
    		$option_num++;
    	}

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thnx guys.. I try to look on both solutions for implementing in my script.

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, the option of gavwvin works for me the best, since that is the way I get result from my query..

    now how can I turn around the foreach...?

    when I have two arrays

    PHP Code:
    $array1 = array ('option1','option2','option3');
    $array2 = array (40,20,10); 
    how to get one array like
    PHP Code:
     $poll = array(
       
    => array(
          
    name => option1,
          
    value => 40
       
    )
       
    => array(
          
    name => option2,
          
    value => 20
       
    )
       
    => array(
          
    name => option3,
          
    value => 10
       
    )
    ); 

  6. #6
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    for($i=0$i<count($array1); $i++){
     
    $new_array[$i]['name'] = $array1[$i];
     
    $new_array[$i]['value'] = $array2[$i];


  7. #7
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tthnx!! that works..

    well as I am building up my script here, I have another question on multi dimensional arrays.

    if I have an array that consists of 5 other arrays... how would I remove one of these arrays if it was empty?


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
  •