Search code examples
phpxmlxml-parsingunset

How to unset xml elements in php?


I am working on a php code as shown below in which I to want unset xml elements in php.

I have applied the logic in the following way:

When $a is 4, then it should display 4th item from the top from xml.
When $a is 3, then it should display 3rd item from the top from xml.
When $a is 2, then it should display 2nd item from the top from xml.
When $a is 1. then it should display 1st item from the top from xml.

At this moment, I have set value of a to 4.

$a=4;
if ($a == 1) {  // it would not unset item[0] and it should display item[0]   (April 5)
for($i = count($xml->channel->item); $i >= 1; $i--){
unset($xml->channel->item[$i]);                     
}
} else if ($a == 2) { // it would not unset item[1]  and it should display item[1] (April 4)
for($i = count($xml->channel->item); $i >= 2; $i--){
unset($xml->channel->item[$i]);
}
unset($xml->channel->item[0]);                 
} else if ($a == 3) { // it would not unset item[2] and it should display item[2]  (April 3)
for($i = count($xml->channel->item); $i >= 3; $i--){
unset($xml->channel->item[$i]);
}
unset($xml->channel->item[0]);
unset($xml->channel->item[1]);
} else if ($a == 4) { // it would not unset item[3] and it should display item[3]  (April 2)
for($i = count($xml->channel->item); $i >= 4; $i--){
unset($xml->channel->item[$i]);
}
unset($xml->channel->item[0]);
unset($xml->channel->item[1]);
unset($xml->channel->item[2]);
} else if ($a == 5) {  // it would not unset item[4] and it should display item[4]   (April 1)
unset($xml->channel->item[0]);
unset($xml->channel->item[1]);
unset($xml->channel->item[2]);
unset($xml->channel->item[3]);
}

The above code is not working properly. All the content is being pulled from this xml http://www.cpac.ca/tip-podcast/jwplayer.xml

I have attached the screenshot with the list of items.

enter image description here


Solution

  • If you want to display the information from a particular element, you can just access it directly by its index, without deleting the other entries. This code will work on the XML downloaded from the URL in your question. Note that xml element arrays are 0-indexed so a value of 2 will get the third entry in the array, so we use $a-1 so that a value of 3 corresponds to the third entry. Also note the slight complexity due to some of the children having a different namespace...

    $xml = simplexml_load_string($xmlstr);
    $a = 3;
    $item = $xml->channel->item[$a-1];
    echo "Title: " . $item->title . "\n";
    echo "Description: " . $item->description . "\n";
    $jw = $item->children('jwplayer', true);
    echo "Image: " . $jw->image . "\n";
    echo "Source: " . $jw->source->attributes()->file . "\n";
    

    Output:

    Title: April 3, 2019 
    Description: Jody Wilson-Raybould and Jane Philpott are removed from the Liberal Caucus. Gerald Butts submits text messages, and other evidence, to the justice committee. The Environment Commissioner says Canada isn't doing enough to fight climate change. 
    Image: http://media.cpac.ca/_app_images/tip_player_poster.png 
    Source: http://www.cpac.ca/tip-podcast/1554286033.mp3
    

    Demo on 3v4l.org