0

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>";
      }
    }

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks but count remains one like this Array ( [2] => Array ( [0] => 1515840892 [1] => 1515843242 ) [4] => Array ( [0] => 1515841997 ) [10860] => Array ( [0] => 1515842530 ) ) Array ( [2] => 2 [4] => 1 [10860] => 1 )
please show me the content of $res->field_topic_tid ( a sample ) looking to your data should be eg: 'world' or Europa ..
sorry for confusion update results sample in question, its ID and I am fetching its name by function. Also I need to list title, language in next line to created. + 1 thank you for helping.
Europa and World are fetched by using function which use field_topic_tid which is actually coming in results data
Ok .. answer updated .. with a check on setted value ..but the counter should not contain the created value .. in my coden i don't assign this value to counter and overall i don't append the values in counter array .. check better
|
1

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
}

1 Comment

Thanks but I need to list all rows one by one. For example: $value['created'] then $value['title'] and $value['language']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.