开发者

PHP for each loop doesn't work if only one result found

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) "valu开发者_开发百科e2"
  ["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.


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.


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)
 {


foreach(results AS $curr_result)

should be:

foreach($results AS $curr_result)


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!


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;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜