How to traverse a multiple dimensional array in foreach loop to get the table of 4 columns?
How to traverse a multiple dimensional array in foreach loop to get the table of 4 columns?
I have an array of this format:
Array
(
[2] =开发者_如何学Go> Array
(
[0] => Array
(
[0] => 1938
[prod_id] => 1938
[1] => Nokia N96 16GB Sim Free Unlocked Mobile Phone
[prod_name] => Nokia N96 16GB Sim Free Unlocked Mobile Phone
[2] => 1938_1280387838.jpeg
[prod_image] => 1938_1280387838.jpeg
[3] =>
[prod_model] =>
[4] => 0.00
[prod_purchase_price] => 0.00
[5] => 286.95
[prod_retail_price] => 286.95
[6] => 295.51
[prod_sp_offer_price] => 295.51
[7] => 0
[prod_stock_qty] => 0
[8] => 0
[network_id] => 0
[9] =>
[prod_preorder_text] =>
[10] => 2
[cat_type_id] => 2
[11] => Sim Free Mobiles
[cat_type_name] => Sim Free Mobiles
[discount] => 0
)
}
}
Now i need a table which must divide the last child array data in 4 columns of data. using foreach. IN PHP
Not sure I got this right, you need to display last 4 values of each array element? If so, something like this could be done:
Based on additional information provided, you need to use something like this:
echo '<table><tr>';
foreach ( $array as $i => $data )
{
echo '<td>HereBe $data</td>';
if ( 3 === $i % 4 )
{
echo '</tr><tr>';
}
}
echo '</tr></table>';
Please note, above code is just an example and is not optimized at all, it is only meant as an idea to help you started. Yes, the result will be correct, but HTML output will not validate in some cases. Can you guess which ones? :)
sy for e.g your main array is named as $arrMainArr
echo '<table border="1">';
foreach($arrMainArr[2] as $arrMains){
echo '<tr>';
foreach($arrMains as $key => $value){
if(!preg_match("/\d/",$key))
echo '<td>'.$key.'</td>';
}
echo '</tr>';
}
echo '</table>';
OR Other way is
$arrMains = array_keys($arrMainArr[2][0]);
echo '<table border="1"><tr>';
foreach($arrMains as $key => $value){
if(!preg_match("/\d/",$key))
echo '<td>'.$key.'</td>';
}
echo '</tr></table>';
your array seems to be the result of a database query. if you have control of that query, it will be easier to get the data you want if you modify / amend that query, instead of doing what is essentially a sql query in php. please post the query, and i might help more detailled.
精彩评论