Forum Moderators: coopster

Message Too Old, No Replies

Using 'EXPLODE' with Multidimensional Array!@

         

mani2604

8:00 am on Oct 16, 2010 (gmt 0)



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>

enigma1

1:13 pm on Oct 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't need to complicate things and mess the forms. Use an sd array like you do but move the $table array var to the top of your script

Then upon submission you take the tab key and you use it as an index inside your table array. Basically the name="tab[]" will contain an index into your $table array.

<?php
if( isset($_POST['tab']) && is_array($_POST['tab']) ) {
foreach($_POST['tab'] as $key => $value) {
if( isset($table[$key]) ) {
// Valid index in array do something
$input_array = $table[$key];
}
}
}