Jump to content
php.lv forumi

Problēma ar ksort ()..


SadDemon

Recommended Posts

Problēma - kārtojot masīvu pazūd dublikāta dati.. lūdzu kādu skaidrojumu, kāpēc tas tā?

 

*.php faila saturs:

<?php

$doc = new DOMDocument;
$doc -> load('test_input.xml');

$content = array();
$products = $doc -> getElementsByTagName('product');


foreach ($products as $product) {
$id = $product -> getAttribute('id');
$category = $product -> getAttribute('category');

$row = "product id: $id - category: $category";

$content[$category] = $row;
}

ksort ($content);


foreach ($content as $output) {
echo $output."<br>";
}

?>

 

 

un *.xml faila saturs:

<?xml version="1.0" encoding="utf-8"?>
<root>
<product id="1" category="a" />
<product id="2" category="g" />
<product id="3" category="d" />
<product id="4" category="b" />
<product id="5" category="a" />
</root>

 

 

Output:

product id: 5 - category: a
product id: 4 - category: b
product id: 3 - category: d
product id: 2 - category: g

 

..sekojoši, kur pazūd - <product id="1" category="a" /> - kā to novērst?

Link to comment
Share on other sites

<?php
$content[] = array('category' => 'c', 'name' => 1);
$content[] = array('category' => 'a', 'name' => 2);
$content[] = array('category' => 'b', 'name' => 3);
$content[] = array('category' => 'a', 'name' => 4);
$content[] = array('category' => 'c', 'name' => 5);
$content[] = array('category' => 'b', 'name' => 6);
$content[] = array('category' => 'a', 'name' => 7);

function cmp($a, $b) {
return strcasecmp($a['category'], $b['category']);
}
echo 'pirms:';
print_r($content);
usort($content, "cmp");

echo 'peec:';
print_r($content);
?>

Link to comment
Share on other sites

×
×
  • Create New...