1

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.

4
  • 1
    I'm not sure what the question is here, but I can attest to foreach working when there is only one element in the array. Commented May 5, 2011 at 17:50
  • Not sure if it's just a mis-type, but you're looping over results which isn't a valid variable name, should be $results. Commented May 5, 2011 at 17:50
  • i would say that this script doesn't work if there is more than one element in array. Commented May 5, 2011 at 17:51
  • Yeah, on further inspection, you're looping over results, which isn't a valid variable, and then in the else if block you're referring to $result, and I can't even tell where that's coming from. Very confusing John. Commented May 5, 2011 at 17:53

6 Answers 6

3

You could try:

$results_array = (array)$result;

Which should type cast it to an array, even if it's a single result that was returned.

Sign up to request clarification or add additional context in comments.

Comments

1

This line

if($result_count > 1)
{

Prevents your foreach loop from running when there is only one item in your array. You want

 if($result_count > 0)
 {

Comments

1
foreach(results AS $curr_result)

should be:

foreach($results AS $curr_result)

Comments

1

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!

Comments

0

Your code has a typo in it, and it's not using the array.

$result_name = $results[0]->name;

Just as an FYI, you do realize that the loop in the if statement will simply set $result_name to the last value. It would be easier to do

$result_name = $results[$result_count - 1]->name;

Comments

0

Here's what I just did as a clean solution;

I looked for the sub Array first for the multiple elements;

if (isset($data['ComplexArray']['This'][0])) $UseMe = $data['ComplexArray']['This']; else $UseMe[0] = $data['ComplexArray']['This'][0]; foreach ($UseMe as $EachElement) { . . . }

in XML process - by adding the array element, you're not breaking the existing "Foreach" code so you don't have to have "IF (isset($data['ComplexArray']['This'][0])) envelope around the whole "FOREACH" code with redundant code, merely using the existing array logic so it picks up the "one" set element.

1 Comment

Welcome to Stack Overflow! "Here, use this code" answers aren't generally well received - please edit your answer to explain how does the code in it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.