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 7514243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:26:21+00:00 2026-05-30T00:26:21+00:00

Salutations, Elders of code, I am on a quest to master the spells of

  • 0

Salutations, Elders of code,

I am on a quest to master the spells of PHP, and now need your assistance in slaying a mighty beast.

I’m making a REST API in PHP. One of the functions is a GET that returns a list of pngs in a dir. But instead of returning one array, it returns multiple arrays (one for each iteration?).

I want:

["1.png","2.png","3.png"]

But I’m getting:

["1.png"]["1.png","2.png"]["1.png","2.png","3.png"]

I present my pitiful function for scorn and humiliation:

function getPics() {
$pic_array = Array(); 
$handle =    opendir('/srv/dir/pics'); 
while (false !== ($file = readdir($handle))) { 
    if ($file!= "." && $file!= ".." &&!is_dir($file)) { 
    $namearr = explode('.',$file); 
    if ($namearr[count($namearr)-1] == 'png') $pic_array[] = $file; 
    } 
echo json_encode($pic_array);
} 
closedir($handle);
}
  • 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-05-30T00:26:23+00:00Added an answer on May 30, 2026 at 12:26 am

    You should do some proper indenting and it will be very clear what was wrong. You put the echo json_encode() in the loop. This is a corrected version:

    function getPics()
    {
        $pic_array = Array(); 
        $handle = opendir('/srv/dir/pics'); 
        while ( false !== ($file = readdir($handle)) )
        {
            if ( $file=="." || $file==".." || is_dir($file) ) continue; 
            $namearr = explode('.',$file);
            if ($namearr[count($namearr)-1] == 'png') $pic_array[] = $file; 
        } 
        echo json_encode($pic_array);
        closedir($handle);
    }
    

    Note that this way of checking the extension fails has a minor flaw, in that a file named “png” (with no extension) will match. There are several ways to fix this, e.g. by using pathinfo() to analyse the filename.

    ps. also not that this:

    if ( $file=="." || $file==".." || is_dir($file) ) continue; 
    

    can be written as

    if ( is_dir($file) ) continue; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

File1: hello - dictionary definitions: hi hello hallo greetings salutations no more hello for
I have pattern like below hi hello hallo greetings salutations no more hello for
Salutations, I am having problems allowing user's(admin) the ability to upload files/images to the
I am trying to code an emailer that emails customers of a company. What
I'm looking for something will accept names, salutations, and numbers. Things like Mr., O'Henry,
Greeting and Salutations! I work in an environment where I have a development server,
jQuery validation plugin doesn't seem to work with my code. The text fields are
Greetings and Salutations, My web application utilizes the EvalMath class found at PHPClasses. Unfortunately,
greetings & salutations, this is my first run with the google maps API, i'm
We use a customized installation of SugarCRM and I need to make a change

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.