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?

1 Answer 1

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

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

2 Comments

Thanks, never used xml Array's before, my bad!
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.

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.