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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:19:07+00:00 2026-05-21T22:19:07+00:00

I use simplexml to take an xml results page and turn it into an

  • 0

I use simplexml to take an xml results page and turn it into an array. Then I use a foreach loop to go through the array records.

The problem is if there is only one result in the array the foreach loop doesnt happen, doesnt display any information.

I have to detect if there is only one row or more than one row and depending on that either use a foreach loop or not.

Wanted to see if there is an easier way so I dont have so much code and everything fits in a foreach loop.

Here is an example:

    $result = $data->params->results;
    $result_count = intval($data->params->totalcount);

    if($result_count > 1)
    {    
        foreach(results AS $curr_result)
        {
            $result_name = $curr_result->name;
        }
    }
    else if($result_count == 1)
    {
        $result_name = $result->name;
    }

Edit:
I added the results variable, this is example code and in my hast I didnt go over the code to make sure it was correct. If there is only one result the array looks like this:

  ["fld1"]=>
  string(6) "value1"
  ["fld2"]=>
  string(6) "value2"
  ["fld3"]=>
  string(6) "value3"

If there is more than one result it looks like this:

[0]=>
  ["fld1"]=>
  string(6) "value1"
  ["fld2"]=>
  string(6) "value2"
  ["fld3"]=>
  string(6) "value3"
[1]=>
  ["fld1"]=>
  string(6) "value1"
  ["fld2"]=>
  string(6) "value2"
  ["fld3"]=>
  string(6) "value3"

Again just a quick example, Im sure the code above isnt “correct” per say but it should give enough info to understand what Im talking about.

  • 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-21T22:19:08+00:00Added an answer on May 21, 2026 at 10:19 pm

    Well this is what I did to get it to work:

    $result = $data->params->results;
    $result_count = count($result);
    
    if($result_count == 1)
    {    
    $results_array[0] = $result;
    }else
    {
    $results_array = $result;
    }
    
    //loop through $results_array
    

    Sorry if my information wasnt detailed enough and confusing, if you have a better way of doing this please let me know!

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

Sidebar

Related Questions

I use SimpleXML to save a simple POJO into XML file and then read
I've been trying to use SimpleXML , but it doesn't seem to like XML
Is it possible to use PHP's SimpleXML functions to create an XML object from
I have some simple XML which I would like to parse into an array
I use SimpleXml to write XML file to internal storage in Android device. The
I'm trying to use simplexml to load an xml file from a server: $xml
My page is currently updating an existing xml, the problem is that when it
I'm trying to use SimpleXML to read some NCBI BLAST XML output, and I'm
I'm trying to use SimpleXML to parse an XML document (an ItemLookupResponse for a
I know how to use simplexml_load_file to get XML results if the XML format

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.