SadDemon Posted August 2, 2007 Report Share Posted August 2, 2007 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 More sharing options...
andrisp Posted August 2, 2007 Report Share Posted August 2, 2007 (edited) ksort tur nav pie vainas, bet gan: $content[$category] = $row; Varbūt tu gribēji ?: $content[] = $row; Edited August 2, 2007 by andrisp Link to comment Share on other sites More sharing options...
v3rb0 Posted August 2, 2007 Report Share Posted August 2, 2007 $content[$category] = $row; pazaudē. Link to comment Share on other sites More sharing options...
SadDemon Posted August 2, 2007 Author Report Share Posted August 2, 2007 ..ahā! Tik tiešām.. Bet labi - man būtu nepieciešams sortēt pēc "category" atribūta. Kas būtu jāpamaina, lai sortētu pēc kategorijas? Link to comment Share on other sites More sharing options...
andrisp Posted August 2, 2007 Report Share Posted August 2, 2007 $content[$category][] = $row; Un izvadot: foreach ($content as $cat) { foreach($cat as $product) { echo $product."<br>"; } } Link to comment Share on other sites More sharing options...
v3rb0 Posted August 2, 2007 Report Share Posted August 2, 2007 <?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 More sharing options...
SadDemon Posted August 2, 2007 Author Report Share Posted August 2, 2007 ..paldies, paldies andrisp un v3rb0.. viss kārtībā! Link to comment Share on other sites More sharing options...
Recommended Posts