Group an array on the basis of array keys in php -


i have array structure this,which coming loop:

$a = array('9-aug','$50','room1'); $b = array('10-aug','$60','room1'); $c = array('9-aug','$70','room2'); $d = array('10-aug','$80','room2'); 

i need data this(i.e) group data on basis of date:

['9-aug-2014']=>{                 [0]=>{'$50','room1'},                 [1]=>{'$70','room2'}                  }  ['10-aug-2014']=>{             [0]=>{'$60','room1'},             [1]=>{'$80','room2'}              } 

is there function ? have tried using array_map, can't desired o/p. here have tried:

$e = array_map(null, $a, $b, $c,$d); print_r($e); 

and got:

array (     [0] => array         (             [0] => 9-aug             [1] => 10-aug             [2] => 9-aug             [3] => 10-aug         )     [1] => array         (             [0] => $50             [1] => $60             [2] => $70             [3] => $80         )      [2] => array         (             [0] => room1             [1] => room1             [2] => room2             [3] => room2         )  ) 

please help.i can't format $a,$b,$c,$d arrays, way data fetched.

how format data can visible in format.

<table class="price-breakdown">                     <thead>                     <!--<tr><th></th><th style="font-weight:normal" colspan="3">price per room (inc vat &amp; taxes)</th></tr>-->                     <tr>                         <th class="date-col">date</th>                         <th class="date-col">room</th>                             <th valign="top">                                     premier saver                                 <br>                                 <span class="pbthsubtitle">                                     price per room<br>(inc vat &amp; taxes)                                 </span>                             </th>                       </tr></thead>                      <tbody>                                 <tr>                                     <td>                                             <nobr>wed 17 sep 14</nobr>                                     </td>                                     <td align="left">1</td>                                         <td>     £<span>71.00</span>                                         </td>                                   </tr>                                 <tr>                                     <td>                                     </td>                                     <td align="left">2</td>                                         <td>     £<span>71.00</span>                                         </td>                                   </tr>                                 <tr>                                     <td>                                     </td>                                     <td align="left">3</td>                                         <td>     £<span>71.00</span>                                         </td>                                   </tr>                                 <tr>                                     <td>                                             <nobr>thu 18 sep 14</nobr>                                     </td>                                     <td align="left">1</td>                                         <td>     £<span>67.00</span>                                         </td>                                   </tr>                                 <tr>                                     <td>                                     </td>                                     <td align="left">2</td>                                         <td>     £<span>67.00</span>                                         </td>                                   </tr>                                 <tr>                                     <td>                                     </td>                                     <td align="left">3</td>                                         <td>     £<span>67.00</span>                                         </td>                                   </tr>                                 <tr>                                     <td>                                             <nobr>fri 19 sep 14</nobr>                                     </td>                                     <td align="left">1</td>                                         <td>     £<span>83.00</span>                                         </td>                                   </tr>                                 <tr>                                     <td>                                     </td>                                     <td align="left">2</td>                                         <td>     £<span>83.00</span>                                         </td>                                   </tr>                                 <tr>                                     <td>                                     </td>                                     <td align="left">3</td>                                         <td>     £<span>83.00</span>                                         </td>                                   </tr>                     </tbody>                      <tfoot>                         <tr class="yellow-top">                             <td colspan="5"></td>                         </tr>                         <tr>                             <td class="date-col">                                 <nobr>total 3 nights </nobr>                             </td>                             <td>3 rooms</td>                              <td>    £<span>663.00</span> </td>                                <td>    £<span>744.00</span> </td>                                <td>    £<span>822.75</span> </td>                       </tr><tr class="yellow-btm"><td colspan="5"></td></tr></tfoot>                      </table>  

this can done following code. , working script available @ following url: http://sugunan.net/demo/array4.php

$a = array('9-aug','$50','room1'); $b = array('10-aug','$60','room1'); $c = array('9-aug','$70','room2'); $d = array('10-aug','$80','room2');  $arr = array($a,$b,$c,$d); $arr_merge = array();  foreach($arr $key_dim1=>$val_dim1) {     $arr_merge[$val_dim1[0]][] = array($val_dim1[1],$val_dim1[2]); }  print_r($arr_merge); 

the table creation part array

<table class="price-breakdown">     <thead>         <tr>             <th class="date-col">date</th>             <th class="date-col">room</th>             <th valign="top">premier saver<br>             <span class="pbthsubtitle">price per room<br>(inc vat &amp; taxes)</span>             </th>         </tr>     </thead>      <tbody>         <?php         foreach($arr_merge $key1=>$val1){             foreach($val1 $key2=>$val2){         ?>             <tr>                 <td><nobr><?php if($key2==0){ echo $key1; } ?></nobr></td>                 <td align="left"><?php echo $val2[1]; ?></td>                 <td><?php echo $val2[0]; ?></td>             </tr>         <?php }} ?>     </tbody> </table>  

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -