I have the situation to swap array with condition.My array is plotted bellow.
pid => like primary key in mysql so it may loose it order
type=> type indicates, product type.array having 3 type of products.
Name=> Simply indicates product name
First I tried solution in mysql, But I didn't got any good sign.Some of them suggest me array swapping is better one. But I don't know How to get this one.
My problem
I am having list of items.When i listing products, type 3(mater) should not come at 5,10,15
(i.e modules of 5 ) positions.If it does my design getting collapse.
Screen Shot explanation
Perfect placement
Collapsed design
So i want to make sure type 3(master) never comes at mod of 5 position.How can i do this
Help me
My previous try in mysql here
Array
(
[0] => Array
(
[pid] => 1
[type] => 1
[name] => A
)
[1] => Array
(
[pid] => 2
[type] => 1
[name] => B
)
[2] => Array
(
[pid] => 3
[type] => 2
[name] => D
)
[3] => Array
(
[pid] => 4
[type] => 3
[name] => E(master)
)
[4] => Array
(
[pid] => 5
[type] => 3
[name] => f(sub)
)
[5] => Array
(
[pid] => 6
[type] => 1
[name] => A1
)
[6] => Array
(
[pid] => 7
[type] => 2
[name] => B1
)
[7] => Array
(
[pid] => 8
[type] => 1
[name] => C1
)
[8] => Array
(
[pid] => 9
[type] => 2
[name] => D1
)
[9] => Array
(
[pid] => 10
[type] => 3
[name] => E1(master)
)
[10] => Array
(
[pid] => 11
[type] => 3
[name] => A2(sub)
)
[11] => Array
(
[pid] => 12
[type] => 2
[name] => B2
)
[12] => Array
(
[pid] => 13
[type] => 1
[name] => C2
)
[13] => Array
(
[pid] => 14
[type] => 2
[name] => D2
)
[14] => Array
(
[pid] => 15
[type] => 1
[name] => E2
)
)
Thanks in advance
I gave it a try, the following code should do the trick. What I recommend is that you split up a block in the several subparts that define a block. Furthermore, it's not very DRY code, but it will give you an idea:
<?php
$grid = array(); $blocks = array();
$blocks[] = array(array("pid" => 1, "type" => 1, "name" => "A"));
$blocks[] = array(array("pid" => 2, "type" => 1, "name" => "B"));
$blocks[] = array(array("pid" => 3, "type" => 2, "name" => "D"));
$blocks[] = array(array("pid" => 4, "type" => 3, "name" => "E(master)"), array("pid" => 5, "type" => 3, "name" => "F (sub)"));
$blocks[] = array(array("pid" => 6, "type" => 1, "name" => "A1"));
$blocks[] = array(array("pid" => 7, "type" => 2, "name" => "B1"));
$blocks[] = array(array("pid" => 8, "type" => 1, "name" => "C1"));
$blocks[] = array(array("pid" => 9, "type" => 2, "name" => "D1"));
$blocks[] = array(array("pid" => 10, "type" => 3, "name" => "E1 (master)"), array("pid" => 11, "type" => 3, "name" => "A2 (sub)"));
$blocks[] = array(array("pid" => 12, "type" => 2, "name" => "B2"));
$blocks[] = array(array("pid" => 13, "type" => 1, "name" => "C2"));
$blocks[] = array(array("pid" => 14, "type" => 2, "name" => "D2"));
$blocks[] = array(array("pid" => 14, "type" => 1, "name" => "E2"));
$current_row = 0;
for ($n=0;$n < count($blocks);$n++) {
//Check if current row exists in grid
if (!isset($grid[$current_row]))
{
//Create new empty row in grid
$grid[$current_row] = array();
}
//check if current block fits
if (count($grid[$current_row]) + count($blocks[$n]) > 5)
{
// Block doesn't fit: Search for block that fits
for ($i=$n;$i < count($blocks);$i++)
{
if (count($grid[$current_row]) + count($blocks[$i]) <= 5)
{
//place parts in block on current row
foreach ($blocks[$i] as $part)
{
$grid[$current_row][] = $part;
}
//unset block from queue
unset($blocks[$i]);
}
}
//place current block on new row
$current_row++;
}
//place parts in block in grid
foreach ($blocks[$n] as $part)
{
$grid[$current_row][] = $part;
}
}
print_r($grid);
?>
Related
I have the following array:
[0] => Array
(
[id] => 1
[uid] => 50
[sum1] => 1
[sum2] => 2
)
[1] => Array
(
[id] => 2
[uid] => 50
[sum1] => 2
[sum2] => 4
)
[2] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
As you can see, on some of those indexes, [uid] is the same. The length and data of the array is dynamic.
what i need to do is merge the indexes that have the same value for[uid] and sum the values for the other keys:
[0] => Array
(
[id] => 2
[uid] => 50
[sum1] => 3
[sum2] => 6
)
[1] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
But for the life of me, i can't figure out how to do that.
Any help appreciated!
You can do it like this way,
<?php
$mainArray = [
0 => Array
(
'id' => 1,
'uid' => 50,
'sum1' => 1,
'sum2' => 2
),
1 => Array
(
'id' => 2,
'uid' => 50,
'sum1' => 2,
'sum2' => 4
),
2 => Array
(
'id' => 3,
'uid' => 51,
'sum1' => 3,
'sum2' => 5
)
];
$newArray=[];
foreach ($mainArray as $value) {
if(!array_key_exists($value['uid'], $newArray)){
// Store first time
$newArray[$value['uid']] = $value;
}else{
// Already exist, then sum and replace it
$newArray[$value['uid']]['id'] = $value['id'];
$newArray[$value['uid']]['sum1'] += $value['sum1'];// Sum with previous value
$newArray[$value['uid']]['sum2'] += $value['sum2'];// Sum with previous value
}
}
$newArray = array_values($newArray);// Reset indexes, Start the array index with zero
print_r($newArray);// To see the output
Output:
Array
(
[0] => Array
(
[id] => 2
[uid] => 50
[sum1] => 3
[sum2] => 6
)
[1] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
)
I think you are correct. Actually, i solved the problem using something similar:
$sumArray = Array();
foreach ($array1 as $item){
if(isset($sumArray[$item['uid']])){
$sumArray[$item['idPartener']]['sum1'] += $item['sum1'];
$sumArray[$item['idPartener']]['sum2'] += $item['sum2'];
}else{
$sumArray[$item['uid']] = Array(
'id' => $item['id'],
'sum1' => $item['sum1'],
'sum2' => $item['sum2'] ,
);
}
}
Thanks all for your help!
I have an array containing sport results that looks like this:
Array
(
[0] => Array
(
[name] => Alexander
[distance] => 60
[value] => 9
)
[1] => Array
(
[name] => Alexander
[distance] => 100
[value] => 10
)
[2] => Array
(
[name] => Alexander
[distance] => 200
[value] => 11
)
[3] => Array
(
[name] => Alexander
[distance] => 400
[value] => 12
)
[4] => Array
(
[name] => Dmitriy
[distance] => 60
[value] => 5
)
[5] => Array
(
[name] => Dmitriy
[distance] => 100
[value] => 6
)
[6] => Array
(
[name] => Dmitriy
[distance] => 200
[value] => 7
)
[7] => Array
(
[name] => Dmitriy
[distance] => 400
[value] => 8
)
[8] => Array
(
[name] => Sergei
[distance] => 60
[value] => 1
)
[9] => Array
(
[name] => Sergei
[distance] => 100
[value] => 2
)
[10] => Array
(
[name] => Sergei
[distance] => 200
[value] => 3
)
[11] => Array
(
[name] => Sergei
[distance] => 400
[value] => 4
)
)
I need to display the array data in a specific way in a html table, that should look like this. Top header should contain all unique values from "distance" key and left header should contain all unique values from "name" key:
| 60 | 100 | 200 | 400
===========================================
Alexnader | 9 | 10 | 11 | 12
===========================================
Dmitriy | 5 | 6 | 7 | 8
===========================================
Sergei | 1 | 2 | 3 | 4
How can I do it the fastest way, with the least number of loops or using php array methods? Thank you.
Get unique distances and names for the table.
$names = array_unique(array_map(fn($a) => $a['name'], $data));
$dists = array_unique(array_map(fn($a) => $a['distance'], $data));
Make a function to find by name and distance for table values.
function findValue($dist, $name, $data): string {
foreach($data as $d) {
if($d['name'] != $name || $d['distance'] != $dist ) continue;
return (string) $d['value'];
}
return '';
}
Now out output the table beginning with the head and distances
echo "<table>\n";
echo "<tr><th></th>", implode('', array_map(fn($col) => "<th>$col</th>", $dists)), "</tr>\n";
Put out the data by row for each name using our find function
foreach($names as $name) {
$row = [];
foreach($dists as $dist) {
$row[] = '<td>' . findValue($dist, $name, $data) . '</td>';
}
$rowText = join('', $row);
echo "<tr><td>$name</td>$rowText</tr>\n";
}
Finally close the table.
echo "</table>\n";
The result is
<table>
<tr><th></th><th>60</th><th>100</th><th>200</th><th>400</th></tr>
<tr><td>Alexander</td><td>9</td><td>10</td><td>11</td><td>12</td></tr>
<tr><td>Dmitriy</td><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr><td>Sergei</td><td>1</td><td>2</td><td>3</td><td>4</td></tr>
</table>
We have to restructure the array first
$array = Array
(
"0" => Array
(
"name" => "Alexander",
"distance" => 60,
"value" => 9
),
"1" => Array
(
"name" => "Alexander",
"distance" => 100,
"value" => 10
),
"2" => Array
(
"name" => "Alexander",
"distance" => 200,
"value" => 11
),
"3" => Array
(
"name" => "Alexander",
"distance" => 400,
"value" => 12
),
"4" => Array
(
"name" => "Dmitriy",
"distance" => 60,
"value" => 5
),
"5" => Array
(
"name" => "Dmitriy",
"distance" => 100,
"value" => 6
),
"6" => Array
(
"name" => "Dmitriy",
"distance" => 200,
"value" => 7
),
"7" => Array
(
"name" => "Dmitriy",
"distance" => 400,
"value" => 8
),
"8" => Array
(
"name" => "Sergei",
"distance" => 60,
"value" => 1
),
"9" => Array
(
"name" => "Sergei",
"distance" => 100,
"value" => 2
),
"10" => Array
(
"name" => "Sergei",
"distance" => 200,
"value" => 3
),
"11" => Array
(
"name" => "Sergei",
"distance" => 400,
"value" => 4
)
);
$table_heading = array_unique(array_column($array, "distance"));
$restructured_array = [];
foreach($array as $row){
if(!isset($restructured_array[$row["name"]])){
$restructured_array[$row["name"]] = [];
}
$restructured_array[$row["name"]][$row["distance"]] = $row["value"];
}
Now the array can be looped to print the table.
echo "<table><thead><tr>";
echo "<th></th>";
foreach($table_heading as $heading){
echo "<th>".$heading."</th>";
}
echo "</tr></thead>";
echo "<tbody>";
foreach ($restructured_array as $key=>$rows){
echo "<tr><th>".$key."</th>";
foreach ($rows as $k=>$row){
echo "<td>".$row."</td>";
}
}
echo "</tbody></table>";
The output will be
60 100 200 400
Alexander 9 10 11 12
Dmitriy 5 6 7 8
Sergei 1 2 3 4
I have two array Array1 and Array2 and I need to remove Array2 value from Array1 I show my both the Array here.
In Array1 I have utype_id is 11 and 14 and I need to remove this id record from Array2 so how can I do it can you please help me?
Array1(
[0] => stdClass Object
(
[id] => 22
[accessid] => 2
[utype_id] => 11
[discount] => 3434
[published] => 1
)
[1] => stdClass Object
(
[id] => 23
[accessid] => 2
[utype_id] => 14
[discount] => 2
[published] => 1
)
)
Array2
(
[0] => stdClass Object
(
[id] => 9
[type_name] => Admin
[description] => admin
[published] => 0
)
[1] => stdClass Object
(
[id] => 10
[type_name] => Senior sales
[description] => senior sales
[published] => 0
)
[2] => stdClass Object
(
[id] => 11
[type_name] => junior sales
[description] => junior
[published] => 1
)
[3] => stdClass Object
(
[id] => 14
[type_name] => dealer
[description] => dealer
[published] => 0
)
[4] => stdClass Object
(
[id] => 15
[type_name] => fgdg
[description] => dfg
[published] => 1
)
[5] => stdClass Object
(
[id] => 16
[type_name] => fgdfg
[description] => fgdfg
[published] => 0
)
)
I didn't get any solution for this. I need only 9,10,15,16 Record id from Array2.
Just for entertainment purposes (and I was feeling a bit left out :( ). Index both arrays by the ID (need php 7+ for array_column() to support objects as input) and then array_diff_key() to remove any from the second array...
print_r(array_diff_key(array_column($array2, null, "id"),
array_column($array1, null, "utype_id")));
I would like to say that a foreach() solution is faster than this, just wanted to join in and post some original content.
First, extract utype_ids from first array, make them keys to speed up search:
$utype_ids = [];
foreach ($array1 as $item) {
$utype_ids[$item->utype_id] = 1;
}
Then, filter second array using $utype_ids:
$filtered_array = array_filter(
$array2,
function($v) use ($utype_ids) {
return !isset($utype_ids[$v->id]);
}
);
Demo: https://3v4l.org/i2heV
Use nested loops to perform the qualifying checks. Use a break as a matter of best practice to avoid unnecessary iterations.
Code: (Demo)
$blacklist = [
(object)["id" => 22,"accessid" => 2, "utype_id" => 11, "discount" => 3434, "published" => 1],
(object)["id" => 23,"accessid" => 2, "utype_id" => 14, "discount" => 2, "published" => 1]
];
$rows = [
(object)["id" => 9, "type_name" => "Admin", "description" => "admin", "published" => 0],
(object)["id" => 10, "type_name" => "Senior sales", "description" => "senior sales", "published" => 0],
(object)["id" => 11, "type_name" => "junior sales", "description" => "junior sales", "published" => 1],
(object)["id" => 14, "type_name" => "dealer", "description" => "dealer", "published" => 0],
(object)["id" => 15, "type_name" => "fgdg", "description" => "dfg", "published" => 1],
(object)["id" => 16, "type_name" => "fgdfg", "description" => "fgdfg", "published" => 0]
];
foreach ($blacklist as $disqualifier) { // iterate the blacklist
foreach ($rows as $index => $row) { // iterate the list to be checked
if ($row->id === $disqualifier->utype_id) { // if row should be disqualified
unset($rows[$index]); // remove the row
break; // stop checking the $rows for this $disqualifier
}
}
}
var_export($rows);
...if you need the output to be reindexed, you can call array_values($rows).
If these arrays of objects are coming from a database table, you should be improving your query to do this filtration process in advance.
You can use..
$arr1ids = array();
foreach($array1 as $val1){
$arr1ids[] = $val1->utype_id;
}
$resArr = array();
foreach($array2 as $val2){
if(!in_array($val2->utype_id,$arr1ids)){
$resArr[] = $val2;
}
}
print_r($resArr);
I want to merge two array's using key(product_id) and adding that values(usage).
Array 1
Array
(
[0] => Array
(
[name] => Reschedule A Service
[usage] => 1
[product_id] => 8
)
[1] => Array
(
[name] => Adding An Image
[usage] => 1
[product_id] => 5
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 1
[product_id] => 14
)
)
Array 2
Array
(
[0] => Array
(
[name] => Adding An Image
[usage] => 1
[product_id] => 5
)
[1] => Array
(
[name] => Schedule A Service
[usage] => 3
[product_id] => 11
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 2
[product_id] => 14
)
[3] => Array
(
[name] => Sales Performance Dashboard
[usage] => 2
[product_id] => 30
)
[4] => Array
(
[name] => Quote
[usage] => 1
[product_id] => 32
)
)
I need an out put like this merging and adding usage values.
Array
(
[0] => Array
(
[name] => Adding An Image
[usage] => 2
[product_id] => 5
)
[1] => Array
(
[name] => Schedule A Service
[usage] => 3
[product_id] => 11
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 3
[product_id] => 14
)
[3] => Array
(
[name] => Sales Performance Dashboard
[usage] => 2
[product_id] => 30
)
[4] => Array
(
[name] => Quote
[usage] => 1
[product_id] => 32
)
[5] => Array
(
[name] => Reschedule A Service
[usage] => 1
[product_id] => 8
)
)
This is my code for creating arrays
foreach($query->rows as $product){
$top_products[]=array(
'name'=>$product['name'],
'usage'=>$product['pusage'],
'product_id'=>$product['product_id']
);
}
foreach($query_2->rows as $product){
$top_point_products[]=array(
'name'=>$product['name'],
'usage'=>$product['p_usage'],
'product_id'=>$product['product_id']
);
}
$first =array(
array(
"name" => "Reschedule A Service",
"usage" => 1,
"product_id" => 8
),
array(
"name" => "Adding An Image",
"usage" => 1,
"product_id" => 5
),
array(
"name" => "Each Calendar Event",
"usage" => 1,
"product_id" => 14
)
);
$second =array(
array(
"name" => "Adding An Image",
"usage" => 1,
"product_id" => 5
),
array(
"name" => "Schedule A Service",
"usage" => 3,
"product_id" => 11
),
array(
"name" => "Each Calendar Event",
"usage" => 2,
"product_id" => 14
),
array(
"name" => "Sales Performance Dashboard",
"usage" => 2,
"product_id" => 30
),
array(
"name" => "Quote",
"usage" => 1,
"product_id" => 32
)
);
$result = array_unique(array_merge($first,$second), SORT_REGULAR);
Use array_unique & array_merge
Use the array_merge function, like this:
$C = array_merge($A, $B);
print_r($C);
Read manual Array merge
try this code
<?php
$array1=array
(
0 => array(
'name' => "Reschedule A Service",
'usage' => 1,
'product_id' => 8
),
1 => Array
(
'name' => "Adding An Image",
'usage' => 1,
'product_id' => 5
),
2 => Array
(
'name' => "Each Calendar Event",
'usage' => 2,
'product_id' => 14
)
);
$array2=array
(
0 => Array
(
'name' => "Adding An Image",
'usage' => 1,
'product_id' => 5
),
1 => Array
(
'name' => "Schedule A Service",
'usage' => 3,
'product_id' => 11
),
2 => Array
(
'name' => "Each Calendar Event",
'usage' => 5,
'product_id' => 14
),
3 => Array
(
'name' => "Sales Performance Dashboard",
'usage' => 2,
'product_id' => 30
),
4 => Array
(
'name' => "Quote",
'usage' => 1,
'product_id' => 32
)
);
$product_id1=array_column($array1, 'product_id');
$product_id2=array_column($array2, 'product_id');
$new=array_intersect($product_id1,$product_id2);
foreach ($new as $key => $value) {
if(in_array($new[$key],$product_id2)){
$array2[array_search($new[$key],$product_id2)]['usage']+=$array1[$key]['usage'];
}
}
$new1=array_diff($product_id1,$product_id2);
foreach ($new1 as $key => $value) {
$array2[]=$array1[$key];
}
foreach ($array2 as $key => $value) {
echo "[".$key."]=><br>";
foreach ($value as $key1 => $value1) {
echo "      ";
echo "[".$key1."]=>".$value1."<br>";
}
echo "<br>";
}
?>
output
[0]=>
[name]=>Adding An Image
[usage]=>2
[product_id]=>5
[1]=>
[name]=>Schedule A Service
[usage]=>3
[product_id]=>11
[2]=>
[name]=>Each Calendar Event
[usage]=>7
[product_id]=>14
[3]=>
[name]=>Sales Performance Dashboard
[usage]=>2
[product_id]=>30
[4]=>
[name]=>Quote
[usage]=>1
[product_id]=>32
[5]=>
[name]=>Reschedule A Service
[usage]=>1
[product_id]=>8
Use array_merge and a simple foreach loop to check your condition and update the usagevalues.
See below
$result = array_merge($arrArray1, $arrArray2);
$result2 = array();
foreach($result as $key => $value){
if(array_key_exists($value['product_id'], $result2)){
$result2[$value['product_id']]['usage'] += $value['usage'];
} else{
$result2[$value['product_id']] = $value;
}
}
print_r($result2);
If you want to reset your resultant array indexes use array_merge again like this
$result2 = array_merge($result2);
Hope this will help
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 .....