Using 'EXPLODE' with Multidimensional Array!!@

Discussion in 'PHP' started by mani2604, Oct 16, 2010.

  1. #1
    Hey there...Well I am using checkboxes to post the multi-dim array; but the thing is When I retrieve & explode it directly ,It shows an error

    Warning: explode() expects parameter 2 to be string, array given in C:\ms4w\Apache\htdocs\test24.php on line 12 NULL

    So If I do use 'for loop' to take one array at a time from the clicked checkboxes & then explode; It works.. but thats the problem, I dont want a single dimension arrays as the output.. I want the output to be the multi dimensional array format; the same way how I assigned them to the checkboxes

    I was hoping sumone might checkout the code below & letme know How can I explode & still retain the multidimensional array?!!

    thanks,

    <?php
    
          session_start();
    
          if(isset($_REQUEST['submit']))//---- form is submitted -----
          {
    
          $tab = $_REQUEST['tab'];
    
    var_dump ($tab);
          
          $extent = explode("::",$tab);      //Direct Explode
    
    var_dump ($extent);
    
        for($i=0;$i<count($tab);$i++)
     
       {
        
    
        $extent = explode("::",$tab[$i]);  //Single arrays
          //echo '<br>'.$tab[$i]." is checked.";
    
    var_dump ($extent);
         }
    
          }
    
          ?>
    
           
    
          <html>
    
          <form method="POST" name="myform2" action="<?php echo $_SERVER['PHP_SELF']; ?>">
     
          <?php $table = array(
     
         
     
          array(1,'XYZ',13.59,94,06.73,92.53),
          array(2,'YZX', 19.87,84.78,12.59,76.45),
          array(3,'XZY',24.72,74.55,20.15,68.35),
          array(4,'YXZ' ,12.75,77.37,08.18,74.86),
          array(5,'ZYX' ,32.46,76.93,29.53,73.87),
     
          );
      
          ?>
      
           
    
          <? for($i=0;$i<count($table);$i++){?>
      
          <br><input name="tab[]" type="checkbox" value="<?=$table[$i][0]?>::<?=$table[$i][1]?>::<?=$table[$i][2]?>::<?=$table[$i][3]?>::<?=$table[$i][4]?>::<?=$table[$i][5]?>">
      
          Id : <?=$table[$i][0]?>, Name : <?=$table[$i][1]?>
     
          <? } ?>
     
          <br><br><input name="submit" value="submit" type="submit">
      
          </form>
      
          </html>
    Code (markup):

     
    mani2604, Oct 16, 2010 IP
  2. axelay

    axelay Peon

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Might not be what you want but why don't you try to use something like this:
    <?php
    
    	session_start();
    
    	$table = array(
    		1 => array('XYZ',13.59,94,06.73,92.53),
    		2 => array('YZX', 19.87,84.78,12.59,76.45),
    		3 => array('XZY',24.72,74.55,20.15,68.35),
    		4 => array('YXZ' ,12.75,77.37,08.18,74.86),
    		5 => array('ZYX' ,32.46,76.93,29.53,73.87),
    	);
    
    	if (isset($_REQUEST['submit']))
    	{
    		$tab = $_REQUEST['tab'];
    
    		$result = array();
    		foreach ($tab as $t)
    			$result[] = $table[$t];
    
    		var_dump ($result);
    	}
    ?>
    <html>
    	<form method="POST" name="myform2" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php
    	foreach ($table as $id=>$ar)
    	{
    ?>
    		<br><input name="tab[]" type="checkbox" value="<?=$id?>">Id : <?=$id?>, Name : <?=$ar[0]?>
    <?php
    	}
    ?>
    		<br><br><input name="submit" value="submit" type="submit">
    	</form>
    </html>
    PHP:
    The idea here is to declare the $table right on the top and you just need to pass the id, rather than the full content of the row.
    When the button is pressed, you have a simple array of the indexes and you just need to find them again from the original array.

    I am not real good @ explaining but fire any questions you may have ;)

    aXe
     
    axelay, Oct 16, 2010 IP
  3. mani2604

    mani2604 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well thanks a lot ... that was almost wat I wanted... but stil I've got a problem

    Well the below function is the one that we used previously to retrieve an array and then transfer it to construct polygons for map overlays (image below).. that was most like a hardcoding.. Now the prob is with the implementation of checkboxes....

    Ur code snippet works like a charm when i 'var_dump' the result but when I insert it into the function ; this time the polygons jus don't appear.. I am not sure wats the prob??! Seems like the function doesn't return the same value($qresult) this time...

    $my_array=   array(
                            //  array('id','state','lat_hi','long_hi','lat_low','long_low'),  //field names
    							array(1,'andaman',13.59,94,06.73,92.53),
    							array(2,'AP',     19.87,84.78,12.59,76.45),
    							array(3,'Gujarat',24.72,74.55,20.15,68.35),
    							array(4,'kerala' ,12.75,77.37,08.18,74.86),
    							array(5,'punjab' ,32.46,76.93,29.53,73.87),
    				
    			);
    			$_SESSION['table'] = $my_array; 
    function GetStoreTable() {
    $arraylen=4; // zero based 
      $i=0; 
      $allValuesPresent=1; 
    
         if (isset($_SESSION['table'])) { 
                   foreach ($_SESSION['table'] as $arr) { 
                        if (! isset($arr[$i])) { 
                             $allValuesPresent = 0; 
    							break; 
    						} 
     // Will get overwritten by query if any are missing 
     
     $qresult[$i]=$arr; 
     $i++; 
    } 
    
        // return array of results
    
    // do your query 
    
       return $qresult;
    
    }
    } // end GetStoreTable
    PHP:
    [​IMG]
     
    mani2604, Oct 18, 2010 IP
  4. axelay

    axelay Peon

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    My table definition was slightly different, but you can use
    $table = array(
    	array(1, 'XYZ',13.59,94,06.73,92.53),
    	array(2, 'YZX', 19.87,84.78,12.59,76.45),
    	array(3, 'XZY',24.72,74.55,20.15,68.35),
    	array(4, 'YXZ' ,12.75,77.37,08.18,74.86),
    	array(5, 'ZYX' ,32.46,76.93,29.53,73.87),
    );
    PHP:
    And it should be fine.

    Also, why do you use $_SESSION to pass your table in the function? You should use GLOBAL

    aXe
     
    axelay, Oct 18, 2010 IP