0

I'm trying to get 1 value from my XML Array.

This is the code:

<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();

    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }

    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}
?>

Result:
<?php
$xmlUrl = "http://radiourl:port/status.xsl"; // XML feed file/URL
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
print_r($arrXml);
?>

The result of print_r($arrXml); is this:

    Array ( 
      [Mount-Point] => /listen.mp3 
      [Stream-Title] => VibboStream
      [Stream-Description] => name 
      [Content-Type] => audio/mpeg
      [Mount-started] => 14/Jun/2016:04:28:49 -0500 
      [Bitrate] => 128
      [Current-Listeners] => 1 
      [Peak-Listeners] => 3 
      [Stream-Genre] => Various
      [Stream-URL] => http://url [ice-bitrate] => 128
      [icy-info] => ice-samplerate=44100;ice-bitrate=128;ice-channels=2
      [Current-Song] => Artist - Title
    )

So what I'm trying to get is the part [Current-Song] => Artist - Title. When I echo it I'd like to only see Artist - Title

Can someone help me with this?

Jesse
  • 47
  • 12

1 Answers1

2

You already have the xml object. Just use it.

$artistTitle = $xmlObj->{'Current-Song'};

And just use it from there.

More info on that curly brace syntax here

Community
  • 1
  • 1
  • Thanks, never used xml Array's before, my bad! – Jesse Jun 14 '16 at 12:25
  • Don't worry about. Just always read the docs. If you ever find yourself thinking "there must be an easier way to do this" there probably is. –  Jun 14 '16 at 13:03