I am generating menu from the database, it is working perfectly but only problem is, my parent node is ROOT which I have to remove from the array.
Array ( [0] => Array ( [label] => ROOT [url] => Array ( [0] => category/view [id] => 1 )
[items] => Array (
[0] => Array ( [label] => DESIGNERS [url] => Array ( [0] => category/view [id] => 2 ) )
[1] => Array ( [label] => WOMEN [url] => Array ( [0] => category/view [id] => 3 ) [items] => Array ( [0] => Array ( [label] => CURRENT SALES [url] => Array ( [0] => category/view [id] => 8 ) [items] => Array ( [0] => Array ( [label] => SUIT SELECTIONS [url] => Array ( [0] => category/view [id] => 10 ) ) ) ) [1] => Array ( [label] => ENDING SOON [url] => Array ( [0] => category/view [id] => 9 ) ) ) )
[2] => Array ( [label] => MEN [url] => Array ( [0] => category/view [id] => 4 ) )
[3] => Array ( [label] => MAKE IT YOUR OWN [url] => Array ( [0] => category/view [id] => 5 ) )
[4] => Array ( [label] => CLEARANCE [url] => Array ( [0] => category/view [id] => 6 ) )
[5] => Array ( [label] => OUT OF THE BOX [url] => Array ( [0] => category/view [id] => 7 ) ) ) ) )
I want the array to start with [items] => Array(), how do I remove the parent node.
The desired solution must be:
Array (
[0] => Array ( [label] => DESIGNERS [url] => Array ( [0] => category/view [id] => 2 ) )
[1] => Array ( [label] => WOMEN [url] => Array ( [0] => category/view [id] => 3 ) [items] => Array ( [0] => Array ( [label] => CURRENT SALES [url] => Array ( [0] => category/view [id] => 8 ) [items] => Array ( [0] => Array ( [label] => SUIT SELECTIONS [url] => Array ( [0] => category/view [id] => 10 ) ) ) ) [1] => Array ( [label] => ENDING SOON [url] => Array ( [0] => category/view [id] => 9 ) ) ) )
[2] => Array ( [label] => MEN [url] => Array ( [0] => category/view [id] => 4 ) )
[3] => Array ( [label] => MAKE IT YOUR OWN [url] => Array ( [0] => category/view [id] => 5 ) )
[4] => Array ( [label] => CLEARANCE [url] => Array ( [0] => category/view [id] => 6 ) )
[5] => Array ( [label] => OUT OF THE BOX [url] => Array ( [0] => category/view [id] => 7 ) ) ) ) )
I am not sure I understood well, is that what you want?
$desired_array=$old_array[0]['items']
Related
I want to generate a nested associative array from the result of SQL query from the table which should look like this:
[0] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Sciuridae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Marmota
[children] => Array
(
[0] => Array
(
[name] => Marmota himalayan
[children] => Himalayan Marmot
)
)
)
)
),
[1] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
),
[2] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
),
[3] => Array
(
[name] => Cricetidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Alticola
[children] => Array
(
[0] => Array
(
[name] => Alticola roylei
[children] => Royle's Mountain V
)
)
)
)
)
)
)
Problem: I am unable to figure it out how to nest array if there are same subsequent particular column values, for example, Rodentia has 3 different families (according to the table) it should nest inside children array and similarly for other fields as shown in the above array. My code is as follows:
$sql = "SELECT * FROM `mamallia_table`";
$result = $conn->query($sql);
$genus = array();
if ($result->num_rows > 0) {
foreach($result as $row){
$data[] = classification_order($row);
}
echo "<pre>";
print_r($data);
} else {
echo "0 results";
}
}
function classification_order($row){
$species[] = array("name"=>$row['species'],"children"=>$row['common_name']);
$genus[] = array("name"=>$row['genus'],"children"=>$species);
$family_count[] = array("name"=>$row['family'],"children_no"=>$row['no_of_species'],"children"=>$genus);
$data = ["name"=>$row['order'],"children_no"=>$row['no_of_family'],"children"=>$family_count];
return $data;
}
Output: Which gives me the resulting array:
Array
(
[0] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Sciuridae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Marmota
[children] => Array
(
[0] => Array
(
[name] => Marmota himalayan
[children] => Himalayan Marmot
)
)
)
)
)
)
)
[1] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
)
)
)
[2] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Cricetidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Alticola
[children] => Array
(
[0] => Array
(
[name] => Alticola roylei
[children] => Royle's Mountain V
)
)
)
)
)
)
)
)
I get this array in response from webservice, so How do I read it in a foreach cycle? or some easy way to read it. The [group] they are more than 12 [id]
there is.
Array
( [response] => Array (
[single] => Array ( [parameters] => Array ( [0] => Array ( [name] => msgCode [value] => 0101 )
[1] => Array ( [name] => msgDesc [value] => OK )
[2] => Array ( [name] => status [value] => 1)
[3] => Array ( [name] => message [value] => Normal) ) )
[group] => Array ( [id] => N4BD767 [parameters] => Array ( [0] => Array ( [name] => idFee [value] => 000 ) echo
[1] => Array ( [name] => typeFee [value] => Cuota)
[2] => Array ( [name] => entryDate [value] => 2014-12-17T14:06:47-03:00 )
[3] => Array ( [name] => expirationDate [value] => 2015-12-05T00:00:00-03:00)
[4] => Array ( [name] => amountOrigin [value] => 221980)
[5] => Array ( [name] => surcharges [value] => 1856)
[6] => Array ( [name] => entity [value] => ONLINE)
[7] => Array ( [name] => feeStatus [value] => inicial )
[8] => Array ( [name] => tranNumber [value] => 27) ) ) ) )
Okay guys, this is what I did and it work fine. With no foreach.
$output = array();
array_walk_recursive($result, function($item,$key) use (&$output){
array_push($output,$key,$item);
});
echo var_dump($output);
The array is received from the facebook api and i am not able to extract the likes array from the array,Please help me
[data] => Array (
[0] => Array (
[message] => Hello
[id] => 729659027165160_729651713832558
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 729659027165160
)
)
[paging] => Array (
[cursors] => Array (
[after] => NzI5NjU5MDI3MTY1MTYw
[before] => NzI5NjU5MDI3MTY1MTYw
)
)
)
)
[1] => Array (
[id] => 729659027165160_718306454967084
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1719747118259908
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTcxOTc0NzExODI1OTkwOA==
[before] => MTcxOTc0NzExODI1OTkwOA==
)
)
)
)
[2] => Array (
[id] => 729659027165160_541135166017548
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[3] => Array (
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[id] => 729659027165160_520677651396633
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
[1] => Array (
[id] => 806391372817118
)
[2] => Array (
[id] => 928633297192567
)
[3] => Array (
[id] => 824812004311172
)
[4] => Array (
[id] => 10207344532684729
)
[5] => Array (
[id] => 1188171664544003
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE4ODE3MTY2NDU0NDAwMw==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[4] => Array (
[id] => 729659027165160_110578795739856
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyO
)
)
)
)
)
I am able to extract the id from the above array,but unable to extract the count of likes and message.
Try this code, it iterates in your array and stores all ids and stores the message, the likes Array and the number of likes only if they exist (Supposing that your array is named $myarray):
$result = array();
foreach($myarray['data'] as $data){
$item = array();
$item['id'] = $data['id'];
if( isset($data['message']) || isset($data['likes']) ){
if(isset($data['message'])) $item['message'] = $data['message'];
if(isset($data['likes'])) {
$item['likes'] = array();
foreach($data['likes']['data'] as $like){
$item['likes'][] = $like['id'];
}
$item['countlikes'] = count( $data['likes']['data'] );
}
}
$result[] = $item;
}
print_r($result);
With your example Array the result will be:
Array
(
[0] => Array
(
[id] => 729659027165160_729651713832558
[message] => Hello
[likes] => Array
(
[0] => 729659027165160
)
[countlikes] => 1
)
[1] => Array
(
[id] => 729659027165160_718306454967084
[likes] => Array
(
[0] => 1719747118259908
)
[countlikes] => 1
)
[2] => Array
(
[id] => 729659027165160_541135166017548
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
[3] => Array
(
[id] => 729659027165160_520677651396633
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[likes] => Array
(
[0] => 1162428970453842
[1] => 806391372817118
[2] => 928633297192567
[3] => 824812004311172
[4] => 10207344532684729
[5] => 1188171664544003
)
[countlikes] => 6
)
[4] => Array
(
[id] => 729659027165160_110578795739856
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
)
I want a recursive function to group all child categories under root category. I've an array like this;
Array
(
[0] => Array
(
[uid] => 1
[title] => Car
[Child] => Array
(
[0] => Array
(
[uid] => 3
[title] => Color
[Child] => Array
(
[0] => Array
(
[uid] => 5
[title] => Red
)
[1] => Array
(
[uid] => 6
[title] => Blue
)
)
)
[1] => Array
(
[uid] => 4
[title] => Door
)
)
)
[1] => Array
(
[uid] => 2
[title] => Two Wheeler
[Child] => Array
(
[0] => Array
(
[uid] => 7
[title] => Type
[Child] => Array
(
[0] => Array
(
[uid] => 9
[title] => Scooter
)
[1] => Array
(
[uid] => 10
[title] => Bike
)
)
)
[1] => Array
(
[uid] => 8
[title] => Company
)
)
)
)
My requirement is to bring all the subchild values to the main category child value. I mean I need the following structure;
Array
(
[0] => Array
(
[uid] => 1
[title] => Car
[Child] => Array
(
[0] => Array
(
[uid] => 3
[title] => Color
)
[1] => Array
(
[uid] => 4
[title] => Door
)
[2] => Array
(
[uid] => 5
[title] => Red
)
[3] => Array
(
[uid] => 6
[title] => Blue
)
)
)
[1] => Array
(
[uid] => 2
[title] => Two Wheeler
[Child] => Array
(
[0] => Array
(
[uid] => 7
[title] => Type
)
[1] => Array
(
[uid] => 8
[title] => Company
)
[2] => Array
(
[uid] => 9
[title] => Scooter
)
[3] => Array
(
[uid] => 10
[title] => Bike
)
)
)
)
)
How can I achieve this using a PHP function?
I have an array as below:
Array
(
[0] => Array
(
[id] => 2
[type] => comment
[batch] => Array
(
[0] => Array
(
[id] => T001
[datetime] => 2010-05-15 11:29:00
)
[1] => Array
(
[id] => T003
[datetime] => 2010-05-15 11:33:00
)
)
)
[1] => Array
(
[id] => 3
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T002
[datetime] => 2010-05-15 11:31:00
)
[1] => Array
(
[id] => T004
[datetime] => 2010-05-15 12:32:00
)
[2] => Array
(
[id] => T006
[datetime] => 2010-05-15 12:33:00
)
)
)
[2] => Array
(
[id] => 4
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T005
[datetime] => 2010-05-15 12:34:00
)
[1] => Array
(
[id] => T007
[datetime] => 2010-05-15 13:35:00
)
[2] => Array
(
[id] => T008
[datetime] => 2010-05-15 10:36:00
)
)
)
)
I would like to re-order the array by only the last datetime inside the sub array [batch]
the result should be like this:
Array
(
[0] => Array
(
[id] => 4
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T005
[datetime] => 2010-05-15 12:34:00
)
[1] => Array
(
[id] => T007
[datetime] => 2010-05-15 13:35:00
)
[2] => Array
(
[id] => T008
[datetime] => 2010-05-15 10:36:00
)
)
)
[1] => Array
(
[id] => 2
[type] => comment
[batch] => Array
(
[0] => Array
(
[id] => T001
[datetime] => 2010-05-15 11:29:00
)
[1] => Array
(
[id] => T003
[datetime] => 2010-05-15 11:33:00
)
)
)
[2] => Array
(
[id] => 3
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T002
[datetime] => 2010-05-15 11:31:00
)
[1] => Array
(
[id] => T004
[datetime] => 2010-05-15 12:32:00
)
[2] => Array
(
[id] => T006
[datetime] => 2010-05-15 12:33:00
)
)
)
)
Can I do it with "array_multisort"? Please suggest.
Thank you very much!
Use usort which allows you to sort an array using a user-defined comparison function. (PHP Docs)
usort($array, function($a, $b) {
$lastTimestampA = end($a['batch']);
$lastTimestampB = end($b['batch']);
return strtotime($lastTimestampA['datetime']) > strtotime($lastTimestampB['datetime']);
})