Reputation: 177
I have results like below and want them to list by grouping based on field_topic_tid and also want to display total against each field_topic_tid plus list all the elements having same tid :
Array
(
[0] => stdClass Object
(
[nid] => 229774
[created] => 1515840892
[title] => Statliga Sveaskog tillbakavisar arkeologers oro över avverkning och tallplantering i järnålderns odlingsmarker i Böda ekopark på Öland, uppger sig varken hittat interna rapporter om skador på fornlämningar eller synpunkter från myndigheter
[language] => sv
[field_topic_tid] => 2
)
[1] => stdClass Object
(
[nid] => 229775
[created] => 1515841997
[title] => Indonesiens centralbank varnar för att kryptovalutor riskerar såväl allmänhetens som finanssystemets välmående, förklarar sådana investeringar riskfyllda då valutan inte backas av fysiska tillgångar samt saknar myndighetsansvar
[language] => sv
[field_topic_tid] => 4
)
[2] => stdClass Object
(
[nid] => 229776
[created] => 1515842530
[title] => Chiles president Bachelet ber befolkningen välkomna och respektera påve Franciskus sedan tre katolska kyrkor brandbombats och påven hotats inför första påvebesöket i landet sedan 1987, attackerna obetydliga och endast smärre skador på byggnaderna enligt myndigheter
[language] => sv
[field_topic_tid] => 10860
)
[3] => stdClass Object
(
[nid] => 229777
[created] => 1515843242
[title] => Tiotals personer från Nordiska motståndsrörelsen delar ut flygblad i Göteborg, polisen på plats då utvecklade fanor innebär allmän sammankomst, lugnt på samtliga platser men polishelikopter på plats för att få översiktsbilder och övervaka eventuella ytterligare tillställningar, anmälan om brott mot ordningslagen upprättad
[language] => sv
[field_topic_tid] => 2
)
[4] => stdClass Object
(
[nid] => 229778
[created] => 1515843973
[title] => Tjänstemän tenderar tolka data i linje med sina politiska värderingar, enligt experimentstudie på över 2 700 anställda på Världsbanken och brittisk biståndsmyndighet
[language] => sv
[field_topic_tid] => 6
)
[5] => stdClass Object
(
[nid] => 229779
[created] => 1515844445
[title] => Flertal sydafrikanska H&M-butiker stormade och vandaliserade i protest mot annons med svart pojke i "coolest monkey in the jungle"-tröja
[language] => sv
[field_topic_tid] => 10862
)
Final Results required:
---------------------Europa (12)--------------------- // Europa is fetched using field_topic_tid
1515858065
1515876116
1515879824
1515879962
1515884386
1515946227
1515952420
1515966754
1516003619
1516007065
1516009339
1516109628
---------------------World (11)--------------------- // World is fetched using field_topic_tid
1515840892
1515843242
1515879880
1515923083
1515963824
1516003867
1516008238
1516008877
1516097354
1516109578
1516109605
---------------------Politik (9)--------------------- // Politik is fetched using field_topic_tid
1515881114
1515881981
1515882293
1515887667
I am able to achieve above by using below code but I have to apply 3 foreachloops to do that, please suggest if there is alternative way.
echo "<pre>"; print_r($results);
$grouped = array();
foreach($results as $res){
$grouped[$res->field_topic_tid][] = (array) $res;
}
arsort($grouped);
foreach($grouped as $key=>$val){
$term = taxonomy_term_load($key);
$section_title = taxonomy_term_title($term);
echo "---------------------".$section_title." (".count($val).")---------------------</br>";
foreach($val as $value){
echo $value['created']."</br>";
}
}
Upvotes: 0
Views: 67
Reputation: 133370
You could do all with a single loop using a second array for count
$grouped = array();
$counter = array();
foreach($results as $res){
$grouped[$res->field_topic_tid][] = $res->created;
$counter[$res->field_topic_tid] = (isset($counter[$res->field_topic_tid]) ? $counter[$res->field_topic_tid]+1 : 1;
}
In grouped you should get the valur for created and in counter the related values for count
Upvotes: 1
Reputation: 1981
You can reduce it by one loop by using implode()
$grouped = array();
foreach($results as $res){
$grouped[$res->field_topic_tid][] = $res->created; // Directly push value of created
}
arsort($grouped);
foreach($grouped as $key => $val){
echo "---------------------".$key." (".count($val).")---------------------</br>";
echo implode('<br>', $val); // Implode with <br> delimiter
}
Upvotes: 1