• 24-11-2016, 14:27:36
    #1
    Kimlik doğrulama veya yönetimden onay bekliyor.
    json.txt
    {
        "result": {
            "title": "İçerik",
    		"posts": [
                {
                    "title": "Başlık 1",
    				"img": "post1.jpg",
    				"link": "1"
                },
                {
                    "title": "Başlık 2",
    				"img": "post2.jpg",
    				"link": "1"
                },
                {
                    "title": "Başlık 3",
                    "img": "post3.jpg",
                    "link": "0"
                },
                {
                    "title": "Başlık 4",
                    "img": "post4.jpg",
                    "link": "0"
                }
            ]
        }
    }

    index.php
    <?php
    // copy file content into a string var
    $json_file = file_get_contents('json.txt');
    // convert the string to a json object
    $jfo = json_decode($json_file);
    // read the title value
    $title = $jfo->result->title;
    // copy the posts array to a php var
    $posts = $jfo->result->posts;
    // listing posts
    foreach ($posts as $post) {
        echo $post->title;
    }
    ?>


    Arkadaşlar yukarıdaki örnekteki gibi yapınca postların tamamını listeleyebiliyorum. Fakat benim istediğim sadece "link": "1" olan postların listelenmesi. Bunu nasıl yapabilirim? If fonksiyonu ile yapmaya çalıştım fakat yapamadım. Şimdiden teşekkürler.
  • 24-11-2016, 16:04:08
    #2
    <?php
    $file = file_get_contents('json.txt');
    $file = json_decode($file,true);
    
    $post = $file['result']['posts'];
    
    if($post){
      foreach($post as $item){
        if($item['link'] == 1){
          # 1'e eşit olanlar
          echo $item['title'].'<br>';
        }
      }
    }
    ?>
    Buyur dostum.

    // Ek bilgi: Gelen json verilerini obje'den ben burada array'a çevirdim. İşini daha kolaylaştırır.
  • 25-11-2016, 01:00:29
    #3
    grafikcoder adlı üyeden alıntı: mesajı görüntüle
    <?php
    $file = file_get_contents('json.txt');
    $file = json_decode($file,true);
    
    $post = $file['result']['posts'];
    
    if($post){
      foreach($post as $item){
        if($item['link'] == 1){
          # 1'e eşit olanlar
          echo $item['title'].'<br>';
        }
      }
    }
    ?>
    Buyur dostum.

    // Ek bilgi: Gelen json verilerini obje'den ben burada array'a çevirdim. İşini daha kolaylaştırır.
    Çok teşekkür ederim hocam. İyi çalışmalar
  • 25-11-2016, 01:53:14
    #4
    Üyeliği durduruldu
    İf koşuluna bağlı olmadan filtrelemeleri bu şekilde yapabilirsiniz arkadaşın belirttiğine ek olarak.

    $json = json_decode($json,true);
    $posts = $json['result']['posts'];
    $posts = array_filter($posts, function($obj)
    {
        return $obj["link"] == "1";
    });
    foreach($posts as $post)
    {
    	echo $post["title"]."<br>";
    }