Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8206253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:36:11+00:00 2026-06-07T08:36:11+00:00

My Question: How would I echo a slide only if the json object contains

  • 0

My Question:

How would I echo a slide only if the json object contains an image?

My Issue:

I’m not sure how I should treat a for loop of a json object to echo a slide only if it contains an image.

Objective:

I want to be able to echo a div only if there is an image in the json object, and then echo a link around the image so I can link to the story.

Additional:

How could I not echo a slide div inside “#slides” when there is no image?

Is there something in PHP that would let me echo the slide if it contains an image without breaking the foreach loop?

Or am I going to have to break the foreach loop,store the information for only the slides holding an image and re-loop through them? If so what would be the best way to do so?

I’m lost because if I make an if statement for $key==”img” its only going to echo the image portion, not sure how I should handle it.

Code:

NEWS.JSON

{
    "1": {
        "id": "1",
        "img":"./images/newspost/07-05-12.jpg",
        "link":"http://www.cnn.com/2012/07/05/world/europe/france-air-crash-report/index.html",
        "title": "Example",
        "date":"02/08/12",
        "content": "Example"
    },
    "2": {
        "id": "2",
        "img":"",
        "link":"http://online.wsj.com/article/SB10001424052702304141204577508500189367804.html?mod=googlenews_wsj",
        "title": "Example",
        "date":"09/03/10",
        "content": "Example"
    }
}

HOME.PHP

/* Error Report on */
error_reporting(E_ALL);

/* Open Json file */
$json = file_get_contents("./content/news.json");

/* Setup iterator to go through file */
$jsonIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($json, TRUE)),RecursiveIteratorIterator::SELF_FIRST);

/* Create SLIDES for SLIDESHOW */
echo "<div id='slides'>";

At this point I DON’T want to echo anything if no image exists.

foreach($jsonIterator as $key => $val)
{

if(is_array($val))
{
echo "<div>";
}

if($key=="link")
{
echo "<a href='$val'>";
}

if($key=="img"&&$val!="")
{
echo "<img alt='' src='$val'></img>";

}



if(!is_array($val)&&$key=="content")
{
echo "</a>";
echo "</div>";
}

}

End loop/what needs to show only if image exists.

echo "</div>";
/* End SLIDES creation */
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:36:12+00:00Added an answer on June 7, 2026 at 8:36 am

    I show you the way how I would do it:

    $json = file_get_contents("./content/news.json");
    $jsonArray = json_decode($json, true);
    
    // Start slideshow...
    echo "<div id='slideshow'>";
    
    foreach ($jsonArray as $entry) {
      if ($entry['img'] == '') {
        continue; // Just don't do anything with this entry, go to next one
      }
    
      // News begins.
      echo "<div>";
    
      // For example:
      echo "<a href='" . $entry['link'] . "'>";
      // Etc.
    
      // News ends.
      echo "</div>";
    }
    

    I think it’s much more simpler than your way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: How would one write a function to check and return whether or not
I can not really find a satisfying answer to the following question: What would
Sorry, I'm not sure how to really word my question. Here it goes. If
Simple question I guess, but a fundamental one and I'm not sure of the
I was having a question about making a 2D JSON string Now I would
I thought this question would have already existed on SO, but then I couldn't
A simple answer to this super simple question would be great! Here is the
Question What would be a good (ideally, technical) reason to ever program some non-trivial
I would like to ask for more an opinion than a question: What would
Before I ask this question I would to apologise because of the fact that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.