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']);
})
Related
How to get all the pricing values from the JSON array? I need to split two segments tld's (com,in,in.net,etc...) and the corresponding tld pricing value I tried to get the values from the array but com, in etc those are key values.
My JSON Array
Array
(
[result] => success
[pricing] => Array
(
[com] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 637.70
)
[transfer] => Array
(
[1] => 637.70
)
[renew] => Array
(
[1] => 637.70
)
)
[in] => Array
(
[categories] => Array
(
[0] => ccTLD
[1] => Geography
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 1014.67
)
[transfer] => Array
(
[1] => 1014.67
)
[renew] => Array
(
[1] => 1014.67
)
)
[info] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 200.00
)
[transfer] => Array
(
[1] => 200.00
)
[renew] => Array
(
[1] => 200.00
)
)
[net] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 829.76
)
[transfer] => Array
(
[1] => 829.76
)
[renew] => Array
(
[1] => 829.76
)
)
[biz] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 878.33
)
[transfer] => Array
(
[1] => 878.33
)
[renew] => Array
(
[1] => 878.33
)
)
[org] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 939.45
)
[transfer] => Array
(
[1] => 939.45
)
[renew] => Array
(
[1] => 939.45
)
)
[asia] => Array
(
[categories] => Array
(
[0] => gTLD
[1] => ccTLD
[2] => Geography
[3] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 1527.88
)
[transfer] => Array
(
[1] => 1527.88
)
[renew] => Array
(
[1] => 1527.88
)
)
[co.uk] => Array
(
[categories] => Array
(
[0] => ccTLD
[1] => Geography
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 602.53
)
[transfer] => Array
(
[1] => 602.53
)
[renew] => Array
(
[1] => 602.53
)
)
[in.net] => Array
(
[categories] => Array
(
[0] => Other
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 100.00
)
[transfer] => Array
(
[1] => 100.00
)
[renew] => Array
(
[1] => 100.00
)
)
[uk] => Array
(
[categories] => Array
(
[0] => ccTLD
[1] => Geographic
[2] => Popular
)
[addons] => Array
(
[dns] => 1
[email] => 1
[idprotect] => 1
)
[group] => sale
[register] => Array
(
[1] => 200.00
)
[transfer] => Array
(
[1] => 300.00
)
[renew] => Array
(
[1] => 400.00
)
)
)
)
How to split the array and get the transfer values from the above JSON array?
If I don't misunderstood your question then this is what you need with simple foreach() loop to get tlds with their corresponding prices
$expected = [];
foreach($result['pricing'] as $tld=>$array){
// this is the register value, you can change it for transfer or renew if you wish
$expected[$tld] = $array['register'][1];
}
print_r($expected);
I have a multidimensional array in PHP which looks like the following code:
Array
(
[0] => Array
(
[ID] => 6898
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8214
[ParentID] => 6898
)
[1] => Array
(
[ID] => 5595
[ParentID] => 6898
[children] => Array
(
[0] => Array
(
[ID] => 7238
[ParentID] => 5595
)
)
)
)
)
[1] => Array
(
[ID] => 8216
[ParentID] =>
)
[2] => Array
(
[ID] => 7046
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5555
[ParentID] => 7046
[children] => Array
(
[0] => Array
(
[ID] => 7294
[ParentID] => 5555
)
)
)
[1] => Array
(
[ID] => 5706
[ParentID] => 7046
)
)
)
[3] => Array
(
[ID] => 5884
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7431
[ParentID] => 5884
[children] => Array
(
[0] => Array
(
[ID] => 5618
[ParentID] => 7431
)
)
)
[1] => Array
(
[ID] => 6993
[ParentID] => 5884
)
)
)
[4] => Array
(
[ID] => 6002
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5801
[ParentID] => 6002
)
[1] => Array
(
[ID] => 6615
[ParentID] => 6002
[children] => Array
(
[0] => Array
(
[ID] => 6595
[ParentID] => 6615
)
)
)
)
)
[5] => Array
(
[ID] => 8119
[ParentID] =>
)
[6] => Array
(
[ID] => 8116
[ParentID] =>
)
[7] => Array
(
[ID] => 5992
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7614
[ParentID] => 5992
[children] => Array
(
[0] => Array
(
[ID] => 7647
[ParentID] => 7614
)
)
)
[1] => Array
(
[ID] => 8215
[ParentID] => 5992
)
)
)
[8] => Array
(
[ID] => 5690
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8188
[ParentID] => 5690
)
[1] => Array
(
[ID] => 5678
[ParentID] => 5690
)
)
)
[9] => Array
(
[ID] => 5825
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 6842
[ParentID] => 5825
)
[1] => Array
(
[ID] => 5653
[ParentID] => 5825
[children] => Array
(
[0] => Array
(
[ID] => 6490
[ParentID] => 5653
[children] => Array
(
[0] => Array
(
[ID] => 7737
[ParentID] => 6490
)
)
)
)
)
)
)
[10] => Array
(
[ID] => 8114
[ParentID] =>
)
[11] => Array
(
[ID] => 8113
[ParentID] =>
)
[12] => Array
(
[ID] => 7826
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7871
[ParentID] => 7826
[children] => Array
(
[0] => Array
(
[ID] => 5891
[ParentID] => 7871
)
)
)
[1] => Array
(
[ID] => 5983
[ParentID] => 7826
)
)
)
)
Array
(
[0] => Array
(
[ID] => 6898
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8214
[ParentID] => 6898
)
[1] => Array
(
[ID] => 5595
[ParentID] => 6898
[children] => Array
(
[0] => Array
(
[ID] => 7238
[ParentID] => 5595
)
)
)
)
)
[1] => Array
(
[ID] => 8216
[ParentID] =>
)
[2] => Array
(
[ID] => 7046
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5555
[ParentID] => 7046
[children] => Array
(
[0] => Array
(
[ID] => 7294
[ParentID] => 5555
)
)
)
[1] => Array
(
[ID] => 5706
[ParentID] => 7046
)
)
)
[3] => Array
(
[ID] => 5884
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7431
[ParentID] => 5884
[children] => Array
(
[0] => Array
(
[ID] => 5618
[ParentID] => 7431
)
)
)
[1] => Array
(
[ID] => 6993
[ParentID] => 5884
)
)
)
[4] => Array
(
[ID] => 6002
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 5801
[ParentID] => 6002
)
[1] => Array
(
[ID] => 6615
[ParentID] => 6002
[children] => Array
(
[0] => Array
(
[ID] => 6595
[ParentID] => 6615
)
)
)
)
)
[5] => Array
(
[ID] => 8119
[ParentID] =>
)
[6] => Array
(
[ID] => 8116
[ParentID] =>
)
[7] => Array
(
[ID] => 5992
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7614
[ParentID] => 5992
[children] => Array
(
[0] => Array
(
[ID] => 7647
[ParentID] => 7614
)
)
)
[1] => Array
(
[ID] => 8215
[ParentID] => 5992
)
)
)
[8] => Array
(
[ID] => 5690
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 8188
[ParentID] => 5690
)
[1] => Array
(
[ID] => 5678
[ParentID] => 5690
)
)
)
[9] => Array
(
[ID] => 5825
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 6842
[ParentID] => 5825
)
[1] => Array
(
[ID] => 5653
[ParentID] => 5825
[children] => Array
(
[0] => Array
(
[ID] => 6490
[ParentID] => 5653
[children] => Array
(
[0] => Array
(
[ID] => 7737
[ParentID] => 6490
)
)
)
)
)
)
)
[10] => Array
(
[ID] => 8114
[ParentID] =>
)
[11] => Array
(
[ID] => 8113
[ParentID] =>
)
[12] => Array
(
[ID] => 7826
[ParentID] =>
[children] => Array
(
[0] => Array
(
[ID] => 7871
[ParentID] => 7826
[children] => Array
(
[0] => Array
(
[ID] => 5891
[ParentID] => 7871
)
)
)
[1] => Array
(
[ID] => 5983
[ParentID] => 7826
)
)
)
)
This is the hierarchical array in which I want to pass two node ID's something like this -> getLevelDifference(7826, 5891) and it should give 2 as result or a function in which I can get that level child array like -> getLevelChild(7826, 2) and it should return 5891 as the result or other childs of the same level ?
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 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']