1

I need to echo the key value of an array.

This is how im outputting my array:

foreach($xml->channel->item as $item) {
  $item->title
}

I've tried adding:

foreach($xml->channel->item as $key => $item)

But the value I echo just comes out as: item

Any ideas?

Var Dump of item results:

var_dump($xml->channel->item);

    object(SimpleXMLElement)#4 (5) { 
["title"]=>  string(33) "BA and union agree to end dispute" 
["description"]=>  string(118) "British Airways and the Unite union reach an agreement which could end the long-running dispute between the two sides." 
["link"]=>  string(61) "http://www.bbc.co.uk/go/rss/int/news/-/news/business-13373638" 
["guid"]=>  string(43) "http://www.bbc.co.uk/news/business-13373638" 
["pubDate"]=>  string(29) "Thu, 12 May 2011 12:33:38 GMT" 
} 
2
  • 2
    have you put echo in front of $item->title? try var_dump($xml->channel->item and debug whether it is already an array or not. Commented May 12, 2011 at 22:59
  • I think it's a SimpleXML Object and won't necessarily dump out as an array Commented May 12, 2011 at 23:00

4 Answers 4

1

Try doing:

$data = $xml->channel->item;

if (is_object($data) === true)
{
    $data = get_object_vars($data);
}

echo '<pre>';
print_r(array_keys($data));
echo '</pre>';
Sign up to request clarification or add additional context in comments.

2 Comments

This gives me the key for each entry under item:
@CLiown: And you don't want that? foreach (array_keys($data) as $key) { echo $key; }.
0

It is not an array but a SimpleXMLElement. So loop through the children nodes and output the name.

foreach ( $xml->channel->item->children() as $child ) {
  echo $child->getName();
}

Comments

0

foreach($xml->channel->item as $key => $item) echo $key; }

ADDED:

Check the answer here for converting it to array Trouble traversing XML Object in foreach($object as $key => $value);

1 Comment

@CLiown that's because it's not a real array. It's an SimpleXMLElement, which mimics being an array without real indexes, just lots of values.
0

Hi maybe you could try to convert XML the into Array

function xml2array ( $xmlObject, $out = array () )
{
        foreach ( (array) $xmlObject as $index => $node )
            $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;

        return $out;
}

and then access it with foreach.

EXAMPLE

$x = new SimpleXMLElement(<<<EOXML
<root>
   <node>Some Text</node>
</root>
EOXML
);

xml2array($x,&$out);
var_dump($out);

OUTPUT

array(1) {
  ["node"]=>
  string(9) "Some Text"
}

3 Comments

It is just simple recursive method with parsers all nodes of XML. Try xml2array($xml, $out) and then foreach($out as $key=>$value){}
Result is NULL when I var_dump($out)
Ouups, mistake. $out by reference, xml2array($xml, &$out)

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.