2017-05-16 7 views
0

Ich habe einige decodierte JSON-Daten, die ich in einer bestimmten Reihenfolge basierend auf einem Kindwert sortieren möchte. Die entschlüsselten json-Datei wie folgt strukturiert:Array nach Kind sortieren

Array 
(
    [location] => Array 
     (
      [id] => 10215235726 
      [title] => demo title 
      [media] => Array 
       (
        [nodes] => Array 
         (
          [0] => Array 
           (
            [id] => 15129696952092 
            [thumbnail_src] => thumb_15131.jpg 
            [is_video] => 1 
            [code] => BTg35sbvdfc 
            [date] => 1494577207 
            [display_src] => image_15131.jpg 
            [video_views] => 318 
            [caption] => Batman 
            [comments] => Array 
             (
              [count] => 2 
             ) 
            [likes] => Array 
             (
              [count] => 87 
             ) 
           ) 

          [1] => Array 
           (
            [comments_disabled] => 
            [id] => 47484867964790738 
            [thumbnail_src] => thumb_11536.jpg 
            [is_video] => 
            [code] => BTmSAQghufS 
            [date] => 1493745672 
            [display_src] => image_11536.jpg 
            [caption] => Aquaman 
            [comments] => Array 
             (
              [count] => 2 
             ) 
            [likes] => Array 
             (
              [count] => 73 
             ) 
           ) 
etc... 

Ich Werte ohne Problem mit der folgenden Ausgabe von:

$json = json_decode(file_get_contents("http://www.linktojson.com"), true); 

foreach($json['location']['media']['nodes'] as $value) { 
    echo $value['display_src']; 
} 

Aber jetzt würde Ich mag die Ausgabe auf Vorlieben sortieren ([count]) . Ich habe schon ein paar Methoden ausprobiert, aber ich kann ihre Lösungen nicht so anwenden, dass sie für mich funktionieren. Im Moment bin ich bei dieser Suche Sortierung:

function custom_sort($a, $b) { 
    return $a['count'] - $b['count']; 
} 

usort($json, 'custom_sort'); 

Dies wirft usort() erwartet Parameter 1 werden Array, null gegeben, auch gibt es zwei count Kinder (Kommentare und mag) ohnehin so ist es wahrscheinlich würde deswegen nicht funktionieren.

Ich bin ziemlich neu in der Arbeit mit diesen Arten von Arrays, so würde jede Hilfe sehr geschätzt werden. Lösungen

+0

Was ist also unklar? '$ data_array' ist __nicht__ ein Array. –

+0

@u_mulder Sorry, kopieren und fügen Sie den Fehler meinerseits. – Lenny

Antwort

1

:

$json = json_decode(file_get_contents("http://www.linktojson.com"), true); 

// sorting by comments count 
usort($json['location']['media']['nodes'], 'sort_by_comments'); 

// OR sorting by likes count 
usort($json['location']['media']['nodes'], 'sort_by_likes'); 

// sorting functions: 
function sort_by_comments($a, $b) { 
    return $a['comments']['count'] - $b['comments']['count']; 
} 

function sort_by_likes($a, $b) { 
    return $a['likes']['count'] - $b['likes']['count']; 
} 
+0

Aah! Prost für diesen Kumpel, so offensichtlich jetzt, dass ich die Lösung (wie immer) habe. – Lenny

+0

Was ist mit den Parametern $ a und $ b? Ich konnte den Parameter nicht verstehen. Erklären Sie es bitte. – lalithkumar

+0

@lalithkumar '$ a' und' $ b' sind Namen von Parametern, die an die Implementierung einer Sortierfunktion übergeben werden. Werte von $ a und $ b sind Elemente des Arrays, das gerade sortiert wird. –