user3915050
user3915050

Reputation:

xml to array then sort and display

I have the following xml file (sample)

<xml>
 <product>
  <url>banners</url>
  <name>Banners</name>
 </product>
 <product>
  <url>billboards</url>
  <name>Billboards</name>
 </product>
 <product>
  <url>brochures</url>
  <name>Brochures</name>
 </product>
 <product>
  <url>business-cards</url>
  <name>Business Cards</name>
 </product>
</xml>

and I want to write a PHP script that when parsing the xml file (which changes now and then and not going to be manually sorted) sorts the values alphabetically by the tag, but the code I have found to convert my xml file to an array puts a lot of useless information in the array as well and makes it near impossible to sort and display the information I want displayed. here is the php code i have so far

<?php

$file = file_get_contents("xml/products.xml");
$p = xml_parser_create();
xml_parse_into_struct($p, $file, $vals, $index);
xml_parser_free($p);

asort($vals['value']);

echo "<H1>Vals array</H1><BR>";
print_r($vals);
?>

is there another way to convert my xml file without all the unnecessary information as well and in such a way that I can easily display the information afterwords??

the array I hope to end up with will look like

Array
(
    [product] => Array
        (
            [url] => window-clings
            [name] => Window Clings
        )
    [product] => Array
        (
            [url] => banners
            [name] => Banners
        )
)

Upvotes: 0

Views: 516

Answers (3)

user3915050
user3915050

Reputation:

after several hours of experimenting I came up with this solution which gives the results I want and allows me to manipulate the results accordingly

$file = simplexml_load_file("xml/products.xml");

foreach($file->product as $product)
{
 $products["$product->name"] = "$product->url";
}

asort($products);
foreach($products as $name => $url)
{
 echo"       <A HREF=\"products/$url.php\">$name</A>";
}

Upvotes: 0

Isaac
Isaac

Reputation: 983

That's probably what you lookin for

function cmp($a, $b)
{
    return strcmp($a["name"], $b["name"]); // sort by name field 
}

$xml = simplexml_load_string($xmlString, null, LIBXML_NOCDATA);
if($xml == false){
    // Handle error
}


$json = json_encode($xml);
$array = json_decode($json, TRUE);

usort($array['product'], "cmp");

print_r($array);

// Iterate over your array
foreach ($array['product'] as $key => $value) {
    echo $value['name']. ' : '. $value['url'];
}

You array will look like this

Array
(
    [product] => Array
        (
            [0] => Array
                (
                    [url] => banners
                    [name] => Banners
                )

            [1] => Array
                (
                    [url] => billboards
                    [name] => Billboards
                )

            [2] => Array
                (
                    [url] => brochures
                    [name] => Brochures
                )

            [3] => Array
                (
                    [url] => business-cards
                    [name] => Business Cards
                )

        )

)

Upvotes: 1

Kevin
Kevin

Reputation: 41893

If you want to sort in descending fashion. you could use usort() in this case. Example:

$xml = simplexml_load_file('xml/products.xml');
$data = json_decode(json_encode($xml), true);
usort($data['product'], function($a, $b){
    return strcmp($b['name'], $a['name']);
});
$final = array_map(function($batch){
    return array('product' => $batch);
}, $data['product']);
echo '<pre>';
print_r($final);

Upvotes: 1

Related Questions