Support » Developing with WordPress » Sorting the data called with WordPress Acf, API.

  • I use the advanced custom fields pro plugin on my wordpress site. I want to sort numerically from bigger to smaller, data which I got from API. I am receiving the data without any problems. But I could not sort.

     <?php if( have_rows('g_pool' , 'options') ): ?>
            <?php while( have_rows('g_pool' , 'options') ) : the_row(); ?>  
            <?php 
              $name = get_sub_field('pool_id'); 
        
        $o1 = file_get_contents('JSONADDRESS' . get_sub_field('pool_name')  . '?api_key=' . $api); 
            $pl1 = json_decode($o1);
            $d1 = $pl1->id;
            $a1 = file_get_contents('JSONADDRESS'. $d1 . '?api_key=' . $api);
            $player1 = json_decode($a1);
            $rank1 = $player1['0']->rank;
            $point1 = $player1['0']->leaguePoints;
            $tier = $player1['0']->tier;   
        
        
         $ary = ARRAY($point1);
            RSORT($ary );
            
          FOREACH ($ary AS $_point){
            ECHO $_point.'<br>';
          } 
    

    The sample json content I got from api is as follows:https://prnt.sc/vylsf6

    As you can see there is a value called leaguePoints. In my codes above, I transfer the leaguePoints value I got from json to the point1 variable.

    The screenshot you see below is my repeater content: https://prnt.sc/vylui6 (admin) Let me explain the pool name field. Users are registered with these names in the system where I receive data with API. That’s why I write the user name they registered in the system into this pool_name field. I get the user data by assigning the value entered in the field to the api url in the codes you see above.

    And the result is as in the screenshot I shared below:https://prnt.sc/vyls9o (output)

    I get results as I wrote from the admin panel. All I want is to sort the numbers in the variable point1 in descending order. Of course, this data is demo. Maybe hundreds of lines will be entered, so I want to use repeaters. Actually I can print without sorting. But the important thing for me is to print in order.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Ideally you’d want the API to return sorted data for you. Every API is different, so maybe it’s not possible. You can always use PHP’s usort() to order the data as you require. After the JSON is decoded into an array, run it through usort() to get a reordered array.

    The magic of usort() is you define your own custom callback to make ordering comparisons. PHP passes two elements and your callback returns an integer indicating if the elements are less than, equal, or greater than the other. This return is used to sort the entire array accordingly. I believe PHP uses a bubble sort algorithm to accomplish this, so it’s not particularly speedy, but it will get the job done.

    Thread Starter atakanyildirim

    (@atakanyildirim)

    @bcworkz First of all, thank you very much for your answer. How can I use usort() in my code above?

    Moderator bcworkz

    (@bcworkz)

    How does the league points data correlate with the player data? They come from two different API requests. Does the data need to be correlated before sorting? The two data sets could be assembled into one third data array if need be. Once you have all the data you want sorted into an array (lets call it $unsorted), you basically just do something like $sorted = usort( $unsorted, 'my_sorting_callback');. $sorted will contain the data sorted as you desire.

    All the magic is in how you write my_sorting_callback(). PHP passes to your callback two items from $unsorted. Your callback compares the two and returns a positive, negative, or zero integer value depending on how the two items compare. Your code can make any sort of comparison based on any element values in the passed items. Check out the examples in the following linked docs page:
    https://www.php.net/manual/en/function.usort.php

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.