Monday, September 26, 2011

PHP explode function on array

Today while working on the project of PHP-Reportica , I felt need of  applying php explode function on some php variable. Though usage of explode function was bit easy and I already used it, but this time I need to use it in much complicated way.

I was having a field with value of  selected_table_name.select_field_name. Now I had easily separated these two using function as:-

$explode_data= explode(".",  $tab_field);

Two arrays are formed now viz.  $explode_data[0] and $explode_data[1]. But issue was that first array might have redundant entries of same table names. Now, I stuck at this point, How to do it???

I find solution after about one hour spending one this thing. I came to know about a PHP function called unique array which removes redundant entries from an array.  Here below is what I needed:-
 foreach ($_POST['selfield'] as $mystring) {
$rs=explode(",",$mystring);
$first[]= $rs[0];
}
$uniqtab= array_unique($first);
$count= count($uniqtab);
$i=0;
foreach ($uniqtab as $table){
    echo $table;
     if ($i < ($count - 1)) {
      echo ', ';
   }
   $i=$i+1;
}

No comments:

Post a Comment