I have an associative array like this, i want to generate a table with these data, like breakfast, snacks, lunch, supper, dinner in row the following is the code I've tried, but I am stuck where to break the table row because when the array contains more than one item.i wants to Print associative array to table
*--------------------------------------------------*
| **Breakfast Snacks Lunch Supper Dinner** |
| test test test testfrom
|testfrom
*--------------------------------------------------*
array output is as follow
Array
(
[meal_plan_id] => 17
[calorie_limit] => 1
[total_calorie] => 0
[date] => 2017-12-29
[meal_plan] => Array
(
[0] => Array
(
[meal_type] => bf
[label] => Breakfast
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 107
[label] => test
[quantity] => 10
[unit] => g
[status] => bf
)
[1] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
)
)
[1] => Array
(
[meal_type] => sn
[label] => Snacks
[calorie_limit] => 10
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 108
[label] => test
[quantity] => 121
[unit] => g
)
)
)
[2] => Array
(
[meal_type] => lu
[label] => Lunch
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[status] => su
)
)
)
[3] => Array
(
[meal_type] => su
[label] => Supper
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[status] => sn
)
[1] => Array
(
[id] => 116
[label] => test
[quantity] => 200
[unit] => oz
)
)
)
[4] => Array
(
[meal_type] => dn
[label] => Dinner
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 113
[label] => test500
[quantity] => 20
[unit] => oz
[status] => dn
)
)
)
)
)
below is the code I've tried
// $daily_meals = show_daily_meals() // contains the array
if(!empty($daily_meals['meal_plan'])){
echo '<tr>';
foreach($daily_meals['meal_plan'] as $meal_plan){
foreach ($meal_plan['data'] as $data){
if(!empty($data['id']))
echo '<td class="'.$meal_type.'" data-meals-id="'.$data['id'].'"><span class="left">'.$data['label'].'</span> <span class="right">'.$data['quantity'].' <a class="delete_row"><i class="fa fa-trash" aria-hidden="true"></i></a></span><div class="row_loader"></div></td>';
else echo '<td></td>';
}
$i++;
if($i%5 == 0) echo '</tr>';
}
}
Here is your solution
Input
<?php
$array = array(
array(
'meal_type' => 'bf',
'label' => 'Breakfast',
'calorie_limit' => 30,
'total_calorie' => 0,
'data' => array(
array(
'id' => 107,
'label' => 'test',
'quantity' => 10,
'unit' => 'g',
'status' => 'bf'
),
array(
'id' => 109,
'label' => 'testfrom',
'quantity' => 12,
'unit' => 'g'
)
)
),
array(
'meal_type' => 'sn',
'label' => 'Snacks',
'calorie_limit' => 10,
'total_calorie' => 0,
'data' => array(
array(
'id' => 108,
'label' => 'test',
'quantity' => 121,
'unit' => 'g'
)
)
),
array(
'meal_type' => 'lu',
'label' => 'Lunch',
'calorie_limit' => 20,
'total_calorie' => 0,
'data' => array(array('status' => 'su'))
),
array(
'meal_type' => 'su',
'label' => 'Supper',
'calorie_limit' => 30,
'total_calorie' => 0,
'data' => array(
array('status' => 'sn'),
array(
'id' => 116,
'label' => 'test',
'quantity' => 200,
'unit' => 'oz'
),
)
),
array(
'meal_type' => 'dn',
'label' => 'Dinner',
'calorie_limit' => 20,
'total_calorie' => 0,
'data' => array(
array(
'id' => 113,
'label' => 'test500',
'quantity' => 20,
'unit' => 'oz',
'status' => 'dn'
)
)
)
);
Solution
1st foreach for parent array. Then second for its child..
foreach($array as $r){
$$r['label'] = '<table width="100%">';
foreach($r['data'] as $s){
if(isset($s['label']))$$r['label'] .= '<tr><td align="center">'.$s['label'].'</td></tr>'; // The if condition check weather the label is exist or not if yes then add that in particular label's table
}
$$r['label'] .= '</table>';
}
echo '
<table width="100%" border="1">
<thead>
<tr>
<th>Breakfast</th>
<th>Snacks</th>
<th>Lunch</th>
<th>Supper</th>
<th>Dinner</th>
</tr>
</thead>
Here all labels canverted into variable by $$r['label']. And all have there own table Now we add all these tables into the master table to get the output.
<tbody>
<tr>
<td>'.$Breakfast.'</td>
<td>'.$Snacks.'</td>
<td>'.$Lunch.'</td>
<td>'.$Supper.'</td>
<td>'.$Dinner.'</td>
</tr>
</tbody>
</table>';
//?// echo "<pre>";print_r($array);
?>
Output
Try storing all your column names in an array and create a table with it:
foreach($array as $row) {
$column[$i]['label'] = $row['label'];
$column[$i]['data'] = $this->getdata($row['data']);
$i++;
}
function getdata($array) {
$data = '';
foreach($array as $row) {
$data. = $row['label'].',';
}
return $data;
}
Create a table with the column names and try inserting data using a loop with respect to insert into
Related
I have an array like below. There are id, label, cost, and cid in an array. We want to sum the cost based on the cid like for cid 22 cost should be 196.5 and for cid 11 cost should be 44.4. In our out put array we want to keep the label, cost (sum), and cid, Please see expected output array.
Array
(
[0] => Array
(
[id] => 1331
[label] => PM1
[cost] => 98.25
[cid] => 22
[product_id] => 133
)
[1] => Array
(
[id] => 1332
[label] => PM3
[cost] => 22.20
[cid] => 11
[product_id] => 133
)
[2] => Array
(
[id] => 1341
[label] => PM1
[cost] => 98.25
[cid] => 22
[product_id] => 134
)
[3] => Array
(
[id] => 1342
[label] => PM3
[cost] => 22.20
[cid] => 11
[product_id] => 134
)
)
Tried below
foreach ($array $key => $value) {
$final[$value['cid']] += $value['cost'];
}
print_r ($final);
Getting below as an output
Array
(
[22] => 196.5
[11] => 44.4
)
Want expected output like below.
Array
(
[22] => Array
(
[label] => PM1
[cost] => 196.5
[cid] => 22
)
[11] => Array
(
[label] => PM3
[cost] => 44.4
[cid] => 11
)
)
Basically want to sum cost based on cid and want to keep the label, cost (sum), and cid.
Any help will be greatly appreciated.
I believe this should do the trick
$CIDs_identified = array();
$final_array = array();
foreach($original_array as $small_array){
if(!in_array($small_array['cid'], $CIDs_identified)){
$CIDs_identified[] = $small_array['cid'];
$final_array[$small_array['cid']] = array(
'label' => $small_array['label'],
'cost' => $small_array['cost'],
'cid' => $small_array['cid'],
);
}else{
$final_array[$small_array['cid']]['cost'] += $small_array['cost'];
}
}
On this site, if you can provide your source array in usable format, that is really helpful so that we don't have to retype it. One way to do that is using var_export.
Below is a simple version. Create an array indexed by cid. If that item doesn't exist, populate it with the basic data and a zero cost. Then on each loop, including the initial, add the row's cost.
<?php
$data = [
[
'id' => 1331,
'label' => 'PMI',
'cost' => 98.25,
'cid' => 22,
'product_id' => 133,
],
[
'id' => 1341,
'label' => 'PMI',
'cost' => 98.25,
'cid' => 22,
'product_id' => 134,
],
];
$output = [];
foreach ($data as $item) {
if (!isset($output[$item['cid']])) {
$output[$item['cid']] = [
'label' => $item['label'],
'cost' => 0,
'cid' => $item['cid'],
];
}
$output[$item['cid']]['cost'] += $item['cost'];
}
print_r($output);
Demo here: https://3v4l.org/MY6Xu
$list = [
[ 'id' => 1331, 'label' => 'PM1', 'cost' => 98.25, 'cid' => 22, 'product_id' => 133 ],
[ 'id' => 1332, 'label' => 'PM3', 'cost' => 22.20, 'cid' => 11, 'product_id' => 133 ],
[ 'id' => 1341, 'label' => 'PM1', 'cost' => 98.25, 'cid' => 22, 'product_id' => 134 ],
[ 'id' => 1342, 'label' => 'PM3', 'cost' => 22.20, 'cid' => 11, 'product_id' => 134 ]
];
$result = [];
array_walk($list, function ($item) use (&$result) {
if (isset($result[$item['cid']])) {
$result[$item['cid']]['cost'] = $item['cost'] + $result[$item['cid']]['cost'];
} else {
$result[$item['cid']] = [ 'label' => $item['label'], 'cost' => $item['cost'], 'cid' => $item['cid'] ];
}
});
print_r($result);
Output:
Array
(
[22] => Array
(
[label] => PM1
[cost] => 196.5
[cid] => 22
)
[11] => Array
(
[label] => PM3
[cost] => 44.4
[cid] => 11
)
)
I'm pulling information from 3 different tables in MSSQL 2008 and I'd like to get the SUM of CC_qty as well as each Location condensed into one field per id. If this can be done in the query itself that would be fantastic - listagg and GROUP_CONCAT are not cutting it. Otherwise I've been working with array_reduce, array_merge, array_diff to no avail.
Here is my query and the original array:
SELECT a.id, a.qty, b.locationID, b.CC_qty, c.Location FROM (
SELECT left(id, 10) as id, MAX(qty) as qty
FROM db1
WHERE id like 'abc-abc%'
GROUP BY left(id, 10)
) as a
JOIN (
SELECT locationID, left(SKU, 10) as SKU, CC_qty FROM db2
WHERE CC_qty > 25
) as b on a.abc-abc = b.SKU
JOIN (
SELECT locationID, Location FROM db3
) as c on b.locationID = c.locationID
Array
(
[0] => Array
(
[id] => abc-abc-12
[qty] => 0
[locationID] => 276
[CC_qty] => 250
[Location] => NOP11
)
[1] => Array
(
[id] => abc-abc-12
[qty] => 0
[locationID] => 310
[CC_qty] => 1385
[Location] => NOP01
)
[2] => Array
(
[id] => abc-abc-23
[qty] => 0
[locationID] => 84
[CC_qty] => 116
[Location] => NOP06
)
[3] => Array
(
[id] => abc-abc-23
[qty] => 0
[locationID] => 254
[CC_qty] => 432
[Location] => NOP08
)
[4] => Array
(
[id] => abc-abc-23
[qty] => 0
[locationID] => 228
[CC_qty] => 101
[Location] => NOP04
)
[5] => Array
(
[id] => abc-abc-34
[qty] => 0
[locationID] => 254
[CC_qty] => 436
[Location] => NOP08
)
[6] => Array
(
[id] => abc-abc-34
[qty] => 0
[locationID] => 254
[CC_qty] => 62
[Location] => NOP08
)
[7] => Array
(
[id] => abc-abc-45
[qty] => 0
[locationID] => 75
[CC_qty] => 89
[Location] => NOP05
)
[8] => Array
(
[id] => abc-abc-45
[qty] => 0
[locationID] => 202
[CC_qty] => 372
[Location] => NOP07
)
)
This is my desired output, for simplicity of knowing what information I absolutely require I've removed qty and locationID but those don't have to be removed:
Array
(
[0] => Array
(
[id] => abc-abc-12
[CC_qty] => 1635
[Location] => NOP11, NOP01
)
[1] => Array
(
[id] => abc-abc-23
[CC_qty] => 649
[Location] => NOP06, NOP08, NOP04
)
[2] => Array
(
[id] => abc-abc-34
[CC_qty] => 495
[Location] => NOP08
[3] => Array
(
[id] => abc-abc-45
[CC_qty] => 461
[Location] => NOP05, NOP07
)
)
Thanks for looking!
Being that I left an answer for MySQL, it wasn't going to work for this. I don't know MSSQL well enough to use it, so here's a way to do it with PHP so I don't leave you completely without an answer.
$arr = array
(
array
(
'id' => 'abc-abc-12',
'qty' => 0,
'locationID' => 276,
'CC_qty' => 250,
'Location' => 'NOP11'
),
array
(
'id' => 'abc-abc-12',
'qty' => 0,
'locationID' => 310,
'CC_qty' => 1385,
'Location' => 'NOP01'
),
array
(
'id' => 'abc-abc-23',
'qty' => 0,
'locationID' => 84,
'CC_qty' => 116,
'Location' => 'NOP06'
)
);
$combinedArr = array();
foreach ($arr as $a)
{
$found = false;
foreach ($combinedArr as $i => $b)
{
if ($b['id'] == $a['id'])
{
$found = true;
$locs = explode(',', $a['Location']);
$combinedArr[$i]['CC_qty'] += $a['CC_qty'];
if (!in_array($b['Location'], $locs))
{
$locs[] = $b['Location'];
$combinedArr[$i]['Location'] = implode(', ', $locs);
}
}
}
if (!$found)
$combinedArr[] = $a;
}
print_r($combinedArr);
/*
Array
(
[0] => Array
(
[id] => abc-abc-12
[qty] => 0
[locationID] => 276
[CC_qty] => 1635
[Location] => NOP01, NOP11
)
[1] => Array
(
[id] => abc-abc-23
[qty] => 0
[locationID] => 84
[CC_qty] => 116
[Location] => NOP06
)
)
*/
I don't have any experience with MSSQL, but I feel rather confident that it provides the necessary functionality to merge, sum, and concatenate. Anyhow, I am compelled to post an answer because I find the answer from Thomas to be unrefined.
Essentially, you should use the id values as temporary keys to determine if you are processing the first occurrence of the group or a subsequent occurrence. On the first encounter, just save the whole row to the output array. For all future rows belonging to the same group, just sum and concatenate the desired values.
To remove the temporary keys in the result array, just call array_values($result).
Code: (Demo)
$array = [
['id' => 'abc-abc-12', 'qty' => 0, 'locationID' => 276, 'CC_qty' => 250, 'Location' => 'NOP11'],
['id' => 'abc-abc-12', 'qty' => 0, 'locationID' => 310, 'CC_qty' => 1385, 'Location' => 'NOP01'],
['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 84, 'CC_qty' => 116, 'Location' => 'NOP06'],
['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 432, 'Location' => 'NOP08'],
['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 228, 'CC_qty' => 101, 'Location' => 'NOP04'],
['id' => 'abc-abc-34', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 436, 'Location' => 'NOP08'],
['id' => 'abc-abc-34', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 62, 'Location' => 'NOP08'],
['id' => 'abc-abc-45', 'qty' => 0, 'locationID' => 75, 'CC_qty' => 89, 'Location' => 'NOP05'],
['id' => 'abc-abc-45', 'qty' => 0, 'locationID' => 202, 'CC_qty' => 372, 'Location' => 'NOP07'],
];
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['id']])) {
$result[$row['id']] = $row;
} else {
$result[$row['id']]['qty'] += $row['qty']; // SUM
$result[$row['id']]['locationID'] .= ", " . $row['locationID']; // CONCAT
$result[$row['id']]['CC_qty'] += $row['CC_qty']; // SUM
$result[$row['id']]['Location'] .= ", " . $row['Location']; // CONCAT
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'id' => 'abc-abc-12',
'qty' => 0,
'locationID' => '276, 310',
'CC_qty' => 1635,
'Location' => 'NOP11, NOP01',
),
1 =>
array (
'id' => 'abc-abc-23',
'qty' => 0,
'locationID' => '84, 254, 228',
'CC_qty' => 649,
'Location' => 'NOP06, NOP08, NOP04',
),
2 =>
array (
'id' => 'abc-abc-34',
'qty' => 0,
'locationID' => '254, 254',
'CC_qty' => 498,
'Location' => 'NOP08, NOP08',
),
3 =>
array (
'id' => 'abc-abc-45',
'qty' => 0,
'locationID' => '75, 202',
'CC_qty' => 461,
'Location' => 'NOP05, NOP07',
),
)
The following is my result array
Array (
[0] => Array
(
[ProductID] => 220
[TextID] => 477
[ProductName] => Hugo Woman
[Price] => 43.91
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/874229/300_0/Hugo_Boss-Hugo_Woman-EdP_30ml_GRATIS_Nail_Polish_4ml.jpg
[CategoryID] => 1
[SubCategoryID] => 1
[View] => 0
)
[1] => Array
(
[ProductID] => 616
[TextID] => 959
[ProductName] => Hugo XY
[Price] => 44.95
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/333660/300_0/Hugo_Boss-Hugo_XY.jpg
[CategoryID] => 2
[SubCategoryID] => 2
[View] => 0
)
[2] => Array
(
[ProductID] => 650
[TextID] => 991
[ProductName] => Hugo Just Different
[Price] => 45.76
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/617162/300_0/Hugo_Boss-Hugo_Just_Different.jpg
[CategoryID] => 2
[SubCategoryID] => 2
[View] => 0
)
)
I have a second array with subcategories, in which the key is referencing to the SubCategoryID:
Array
(
[1] => Array
(
[EN] => Ladies
[NL] => Dames
)
[2] => Array
(
[EN] => Men
[NL] => Heren
)
)
I want to loop through the result array and remove the keys who don't have a SubCategoryID listed in the second array. I looked at http://php.net/manual/en/function.array-filter.php, but can't figure out the best way to do this.
Thank you!
There are two solutions to the above problem, one with using simple for loop and one with using array_walk() function.
Here, $result_array is your result array and $subcategory_array is your subcategory array.
Solution(1):
$subcategory_ids = array_keys($subcategory_array);
$arrLength = count($result_array);
for($i = 0; $i < $arrLength; ++$i){
if(!in_array($result_array[$i]['SubCategoryID'], $subcategory_ids)){
unset($result_array[$i]);
}
}
// display $result_array
echo "<pre>"; print_r($result_array);
Solution(2):
$subcategory_ids = array_keys($subcategory_array);
function filter_arr($item, $key){
global $result_array, $subcategory_ids;
if(!in_array($item['SubCategoryID'], $subcategory_ids)){
unset($result_array[$key]);
}
}
array_walk($result_array, "filter_arr");
// display $result_array
echo "<pre>"; print_r($result_array);
Please try i thnik this help to you..
$array = array (
0 => array
(
'ProductID' => 220,
'TextID' => 477,
'ProductName' => 'Hugo Woman',
'Price' => 43.91,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/874229/300_0/Hugo_Boss-Hugo_Woman-EdP_30ml_GRATIS_Nail_Polish_4ml.jpg',
'CategoryID' => 1,
'SubCategoryID' => 1,
'View' => 0
),
1 => array
(
'ProductID' => 616,
'TextID' => 959,
'ProductName' => 'Hugo XY',
'Price' => 44.95,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/333660/300_0/Hugo_Boss-Hugo_XY.jpg',
'CategoryID' => 1,
'SubCategoryID' => 2,
'View' => 0
),
'2' => array
(
'ProductID' => 650,
'TextID' => 991,
'ProductName' => 'Hugo Just Different',
'Price' => 45.76,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/617162/300_0/Hugo_Boss-Hugo_Just_Different.jpg',
'CategoryID' => 2,
'SubCategoryID' => 1,
'View' => 0
),);
$array1 = array (
1 => array
(
'EN' => 'Ladies',
'NL' => 'Dames'
),
2 => array
(
'EN' => 'Men',
'NL' => 'Heren'
),);
foreach($array as $newArray){
if (array_key_exists($newArray['SubCategoryID'], $array1)) {
echo '<pre>';
print_r($newArray);
echo '</pre>';}}
I don't have any idea how to loop through this array and display it to an HTML table. Child data are dynamic and can be unlimited. Anyone can give me a clue? Thanks!
(
[0] => Array
(
[Discount] => Array
(
[id] => 8
[parent_id] => 0
[lft] => 1
[rght] => 6
[name] => Discount 1
[value] => 25
)
[children] => Array
(
[0] => Array
(
[Discount] => Array
(
[id] => 10
[parent_id] => 8
[lft] => 2
[rght] => 5
[name] => Child of D1
[value] => 32
)
[children] => Array
(
[0] => Array
(
[Discount] => Array
(
[id] => 11
[parent_id] => 10
[lft] => 3
[rght] => 4
[name] => The 1.1.1
[value] => 65
)
[children] => Array
(
)
)
)
)
)
)
)
I know I can't just do:
foreach($discounts as $discount){
echo "<div>{$discount['Discount']['name']}</div>";
}
Try this
foreach ($Discount as $children => $value) {
echo "<div>{$discount['Discount']['$children']['name']}</div>";
}
You will need recursion to properly generate the HTML. Here is an example that I hope is helpful. This isn't exactly HOW you'll want to generate the final HTML but this should get you started on the right track:
$data = array(
0 => array(
'discount' => array(
'id' => 8,
'parent_id' => 0,
'lft' => 1,
'rght' => 6,
'name' => 'Discount 1',
'value' => 25,
),
'children' => array(
0 => array(
'discount' => array(
'id' => 10,
'parent_id' => 8,
'lft' => 2,
'rght' => 5,
'name' => 'Child of D1',
'value' => 32,
),
'children' => array(
0 => array(
'discount' => array(
'id' => 11,
'parent_id' => 10,
'lft' => 3,
'rght' => 4,
'name' => 'The 1.1.1',
'value' => 65,
),
'children' => array(),
)
)
)
)
)
);
$html = getHtml($data);
/**
* This function is called recursively to generate the necessary HTML
* #param array $data
*/
function getHtml(array $data)
{
// This is your base conditon to end the recursion. It stops once it hits an empty children array
if (empty($data)) {
return;
}
$data = $data[0]; // will have to tweak this if you have more than 1 element per array
$html = sprintf('<div id="%d" name="%s">', $data['discount']['id'], $data['discount']['name']);
$html .= getHtml($data['children']); // this recursive call generates inner HTML
$html .= '</div>';
return $html;
}
// See the result
var_dump($html);
My array is like that:
Array
(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
)
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
[2] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[3] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
I want to change that array like that depending on key['type']. If array key['type'] are same, I want to change array like that:
Array
(
[car] => Array(
[0]=>Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
),
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
),
[train]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[cruise]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
)
)
what I mean is that if key['type'] is car, I want to create car array or if the type is train I want to create train array or if the type is cruise I want to create cruise array. I don't know how to loop the array. Anyone please help me. Thanks a lot!
Here's a simple way to do it: loop over the data, and just append to the subarray matching the type value:
// starting data
$starting_array = array (
0 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 1,
'tran_name' => 'private',
'tran_image' => '1251961905A1.jpg',
'type' => 'car',
'troute_id' => 10
),
1 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 2,
'tran_name' => 'express',
'tran_image' => 'bus3.jpg',
'type' => 'car',
'troute_id' => 13
),
2 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 3,
'tran_name' => 'MyanmarTrain',
'tran_image' => 'Burma-Gorteikviaduct.jpg',
'type' => 'train',
'troute_id' => 16
),
3 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 4,
'tran_name' => 'Ayeyarwaddy Cruise',
'tran_image' => 'boat-ChutzpahToo1.jpg',
'type' => 'cruise',
'troute_id' => 22
)
);
// initialize the result array
$result = array();
// loop over the starting array
foreach($starting_array as $entry) {
// make sure the result array has a key matching this item's type
if(!array_key_exists($entry['type'], $result)) {
$result[ $entry['type'] ] = array();
}
// add this item to the result array
$result[ $entry['type'] ][] = $entry;
}
// this is just for testing, so you can verify the output matches your desired result
echo "<pre>";
var_dump($result);
echo "</pre>";
Try this:
<?php
$tempArr = Array
(
Array(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 1,
"tran_name" => "private",
"tran_image" => "1251961905A1.jpg",
"type" => "car",
"troute_id" => 10
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 2,
"tran_name" => "express",
"tran_image" => "bus3.jpg",
"type" => "car",
"troute_id" => 13
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 3,
"tran_name" => "MyanmarTrain",
"tran_image" => "Burma-Gorteikviaduct.jpg",
"type" => "train",
"troute_id" => 16
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 4,
"tran_name" => "Ayeyarwaddy Cruise",
"tran_image" => "boat-ChutzpahToo1.jpg",
"type" => "cruise",
"troute_id" => 22
)
);
$resultArr = array();
foreach($tempArr as $tempKey=>$temp)
{
if(!array_key_exists($temp['type'], $resultArr))
{
$resultArr[$temp['type']] = array();
}
$resultArr[$temp['type']][] = $temp;
}
echo '<pre>';
print_r($resultArr);
?>
This is working fine .....