Foreach error drives me in to nuts

Hai folks,

$x=1;
$result = $xml->xpath('//wp:state'); 
print_r($result);
foreach ($result as $key => $value){
   $state[$x]=$value;
   echo $state[$x] . " ";
   $x++;
}

print_r result :
Array ( [0] => SimpleXMLElement Object ( [0] => NY ) [1] => SimpleXMLElement Object ( [0] => NY ) [2] => SimpleXMLElement Object ( [0] => NY ) [3] => SimpleXMLElement Object ( [0] => NY ) [4] => SimpleXMLElement Object ( [0] => NY ) [5] => SimpleXMLElement Object ( [0] => NY ) )

echo $state[$x] results

N N N N N N

my question is why the for each not featching the full value ‘NY’ :rolleyes::rolleyes::rolleyes:

My guess is, it’s because $value contains a SimpleXMLElement Object, not the value ‘NY’.

it is technically doing what you have asked it to…


echo $state[$x];

echoes the state variable value, and x=1 so it echoes the first character of the value.

I bet if you changed x to $x=2 it would print out Y Y Y Y Y Y Y
and if you leave it off entirely and just echo $state you will get the value…

as an example:


$str = 'spike';
echo $str[1];

echoes p

folks, before i reply to posts,
look at this.

print_r :
Array ( [0] => SimpleXMLElement Object ( [0] => CA ) [1] => SimpleXMLElement Object ( [0] => CA ) [2] => SimpleXMLElement Object ( [0] => CA ) [3] => SimpleXMLElement Object ( [0] => CA ) [4] => SimpleXMLElement Object ( [0] => CA ) [5] => SimpleXMLElement Object ( [0] => CA ) [6] => SimpleXMLElement Object ( [0] => FL ) [7] => SimpleXMLElement Object ( [0] => FL ) [8] => SimpleXMLElement Object ( [0] => FL ) [9] => SimpleXMLElement Object ( [0] => FL ) [10] => SimpleXMLElement Object ( [0] => FL ) [11] => SimpleXMLElement Object ( [0] => FL ) [12] => SimpleXMLElement Object ( [0] => GA ) [13] => SimpleXMLElement Object ( [0] => GA ) [14] => SimpleXMLElement Object ( [0] => IL ) [15] => SimpleXMLElement Object ( [0] => IL ) [16] => SimpleXMLElement Object ( [0] => IL ) [17] => SimpleXMLElement Object ( [0] => IL ) [18] => SimpleXMLElement Object ( [0] => IL ) [19] => SimpleXMLElement Object ( [0] => MA ) )

echo :
CA CA CA CA CA CA FL FL FL FL FL FL GA GA IL IL IL IL IL MA

same foreach works charm.

what i am doing is i have a people search from. if i specify the us state name as ny or ca it prints only one charactor. if i not specify it prints both charactor correctly.
above out put is, by not specifying state name in the search form.

can this be a $state related problem?

$x=2;
$result = $xml->xpath('//wp:state'); 
print_r($result);
foreach ($result as $key => $value){
   $state[$x]=$value;
   echo $state[$x] . " ";
   $x++;
}

Array ( [0] => SimpleXMLElement Object ( [0] => NY ) [1] => SimpleXMLElement Object ( [0] => NY ) [2] => SimpleXMLElement Object ( [0] => NY ) [3] => SimpleXMLElement Object ( [0] => NY ) [4] => SimpleXMLElement Object ( [0] => NY ) [5] => SimpleXMLElement Object ( [0] => NY ) )

N N N N N N

$x=1;
$result = $xml->xpath('//wp:state'); 
print_r($result);
foreach ($result as $key => $value){
   $state[$x]=$value;
   echo $state . " ";
   $x++;
}

Array ( [0] => SimpleXMLElement Object ( [0] => NY ) [1] => SimpleXMLElement Object ( [0] => NY ) [2] => SimpleXMLElement Object ( [0] => NY ) [3] => SimpleXMLElement Object ( [0] => NY ) [4] => SimpleXMLElement Object ( [0] => NY ) [5] => SimpleXMLElement Object ( [0] => NY ) )

nN nNN nNNN nNNNN nNNNNN nNNNNNN

:slight_smile:

Folks,

i think the problem with that $state.
this works charm.


$x=1;
$result = $xml->xpath('//wp:state'); 
print_r($result);
foreach ($result as $key => $value){
   $st[$x]=$value;
   echo $st[$x] . " ";
   $x++;
}

Array ( [0] => SimpleXMLElement Object ( [0] => NY ) [1] => SimpleXMLElement Object ( [0] => NY ) [2] => SimpleXMLElement Object ( [0] => NY ) [3] => SimpleXMLElement Object ( [0] => NY ) [4] => SimpleXMLElement Object ( [0] => NY ) [5] => SimpleXMLElement Object ( [0] => NY ) )

NY NY NY NY NY NY

amazing :frowning:

So $state already contains a string when you arrive at that loop, and you just add 1 letter to that string.
Try echoing $state right before the while loop and see what it contains (my guess is ‘n’ :)).
Defining $state as an empty array before the loop should resolve the problem:


$x=2;
$result = $xml->xpath('//wp:state'); 
print_r($result);
[COLOR="Red"][B]echo "<br /><br />state: $state<br />";
$state = array();[/B][/COLOR]
foreach ($result as $key => $value){
   $state[$x]=$value;
   echo $state[$x] . " ";
   $x++;
}

From the looks of things, that seems to be the main problem and is a very good reason why it is good practice to define our variables when, where and as what we want them to be.

[ot]Please, please, please use `[noparse]

[/noparse]` tags to keep the formatting of the `print_r()` output.[/ot]

ofcause. Thanks buddy for the support :slight_smile:

Absolutly :slight_smile:
now works charm!!
Thank you!!