Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 826309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:23:45+00:00 2026-05-15T03:23:45+00:00

Crux of my problem: I’ve got an XML file that returns 20 results. Within

  • 0

Crux of my problem:
I’ve got an XML file that returns 20 results. Within these results are all the elements I need to get. Now, I need to return them in a random order, and be able to specifically work with item 1, items 2-5, and items 6-17.

Idea 1: Use this script to convert the object to an array, which I can shuffle through. This is close to working, but a few of the elements I need to get are under a different namespace, and I don’t seem to be able to get them. Code:

/*
 * Convert a SimpleXML object into an array (last resort).
 *
 * @access public
 * @param object $xml
 * @param boolean $root - Should we append the root node into the array
 * @return array
 */

function xmlToArray($xml, $root = true) {
if (!$xml->children()) {
    return (string)$xml;
}

$array = array();
foreach ($xml->children() as $element => $node) {
    $totalElement = count($xml->{$element});

    if (!isset($array[$element])) {
        $array[$element] = "";
    }

    // Has attributes
    if ($attributes = $node->attributes()) {
        $data = array(
            'attributes' => array(),
            'value' => (count($node) > 0) ? xmlToArray($node, false) : (string)$node
            // 'value' => (string)$node (old code)
        );

        foreach ($attributes as $attr => $value) {
            $data['attributes'][$attr] = (string)$value;
        }

        if ($totalElement > 1) {
            $array[$element][] = $data;
        } else {
            $array[$element] = $data;
        }

    // Just a value
    } else {
        if ($totalElement > 1) {
            $array[$element][] = xmlToArray($node, false);
        } else {
            $array[$element] = xmlToArray($node, false);
        }
    }
}

if ($root) {
    return array($xml->getName() => $array);
} else {
    return $array;
}
}

$thumbfeed = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?q=skadaddlemedia&max-results=20&orderby=published&prettyprint=true');

$xmlToArray = xmlToArray($thumbfeed);
$thumbArray = $xmlToArray["feed"];
for($n = 0; $n < 18; $n++){
$title = $thumbArray["entry"][$n]["title"]["value"];
$desc = $thumbArray["entry"][0]["content"]["value"];
$videoUrl = $differentNamespace;
$thumbUrl = $differentNamespace;
}

Idea 2: Continue using my working code that is getting the information using a foreach, but store each element in an array, then use shuffle on that. I’m not precisely sure how to write to an array within a foreach loop and not write over one another, though. Working code:

foreach($thumbfeed->entry as $entry){
    $thumbmedia = $entry->children('http://search.yahoo.com/mrss/')
        ->group
    ;
    $thumb = $thumbmedia->thumbnail[0]->attributes()->url;
    $thumburl = $thumbmedia->content[0]->attributes()->url;
    $thumburl1 = explode("http://www.youtube.com/v/", $thumburl[0]);
    $thumbid = explode("?f=videos&app=youtube_gdata", $thumburl1[1]);
    $thumbtitle = $thumbmedia->title;

    $thumbyt = $thumbmedia->children('http://gdata.youtube.com/schemas/2007')
        ->duration
    ;
    $thumblength = $thumbyt->attributes()->seconds;     
}

Ideas on if either of these are good solutions to my problem, and if so, how I can get over my execution humps? Thanks so much for any help you can give.

Update: Sample XML is here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:23:45+00:00Added an answer on May 15, 2026 at 3:23 am
    foreach($thumbfeed->entry as $entry){
        $item = array();
    
        $thumbmedia = $entry->children('http://search.yahoo.com/mrss/')->group;
        $item['thumb'] = $thumbmedia->thumbnail[0]->attributes()->url;
    
        $thumburl = $thumbmedia->content[0]->attributes()->url;
        $thumburl1 = explode("http://www.youtube.com/v/", $thumburl[0]);
        $item['thumbid'] = explode("?f=videos&app=youtube_gdata", $thumburl1[1]);
        $item['thumbtitle'] = $thumbmedia->title;
    
        $thumbyt = $thumbmedia->children('http://gdata.youtube.com/schemas/2007')->duration;
        $item['thumblength'] = $thumbyt->attributes()->seconds;
    
        $items[] = $item;
    }
    

    The result will be an array $items[] which has each element being an associative array with the fields from the loop.

    $x[] = $y; is a shorthand for “append $y as a new element to the array $x“.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So the crux of the problem, is I get the following error when running
So that you don't have to read all of the below, the crux of
I need to create a project that has a web frontend to manage synchronous
I've summarized the crux of problem to be as brief as possible: A simple
I have a question re: performance and design. Crux of the problem is: do
I'm rewriting a Bash script I wrote into Python. The crux of that script
My goal is: first, find a specific key in an XML file. Second, return
The crux of the action is in this file: https://github.com/gdoteof/exodus/blob/42c5ee09f09dcb718fa3bdfd79bfe5182c03faaa/Handler/GamingSession.hs The general idea that
The Problem I've been trying to write a program that logs uncaught exceptions and
See UPDATE below for what I now know is the crux of the problem.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.