A
A
Anonymous Anonymous2020-11-06 16:53:10
PHP
Anonymous Anonymous, 2020-11-06 16:53:10

How to sort XML file by year using PHP?

It is necessary to display the data from the XML file and sort by year, in ascending order. The output of the data is done, but the sorting cannot be done. Tried through the DOM but nothing really comes out. Please help with sorting.
XML example:

<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>

PHP code:
<?php
$xml = simplexml_load_file("cdcatalog1.xml");
echo "<h2>".$xml->attributes()->name." ".$xml->attributes()->Date."</h2><br />";
echo '<table border="1px" cellpadding="1" color="black">
        <thead>
        <tr>
        <div>
            <th> Title </th>
            <th> Artist </th>
            <th> Country </th>
            <th> Company </th>
            <th> Price </th>
            <th> Year </th>
            </div>
        </tr>
        </thead>
        <tbody>';

foreach ($xml->children() as $item)
{
        echo "<tr>
<div>
            <td> $item->title </td>
            <td> $item->artist </td>
            <td> $item->country </td>
            <td> $item->company </td>
            <td> $item->price </td>
            <td> $item->year </td>
            </div>
          </tr>";
}
echo '</tbody></table>';

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anonymous Anonymous, 2020-11-08
@King_Of_Demons

<?php
$cd = array();
$xml = simplexml_load_file('cdcatalog1.xml');
foreach($xml->children() as $item)
{
    $cd[] = array(
        'title'             => (string)$item->title,
        'artist'          => (string)$item->artist,
        'country'           => (string)$item->country,
        'company'         => (string)$item->company,
        'price' => (string)$item->price,
        'year' => (string)$item->year
    );
}


array_sort_by_column($cd, 'year');

var_dump($cd);

function array_sort_by_column(&$array, $column, $direction = SORT_ASC) {
    $reference_array = array();

    foreach($array as $key => $row) {
        $reference_array[$key] = $row[$column];
    }

    array_multisort($reference_array, $direction, $array);
}

A
Adik Izat, 2020-11-06
@JaxAdam

/* Берем контент файла */
        $xmlString = file_get_contents($xmlFilePath);
        // Преобразуем xml в массив
        $xml = json_decode(
            json_encode((array)simplexml_load_string($xmlString)), true
        );

+ Sorting example from another forum:
$data = array(
    array(
        "title" => "Another title",
        "date"  => "Fri, 17 Jun 2011 08:55:57 +0200"
    ),
    array(
        "title" => "My title",
        "date"  => "Mon, 16 Jun 2010 06:55:57 +0200"
    )
);

function sortFunction( $a, $b ) {
    return strtotime($a["date"]) - strtotime($b["date"]);
}
usort($data, "sortFunction");
var_dump($data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question