Goedemorgen,

Ik probeer een drie dimensionaal array te sorteren op een waarde uit de laatste array.

Dit is de structuur:

map_data
{
levels
{
locations
{
[title] => room 1
[categories] => cat_3
},
{
[title] => room 2
[categories] => cat_1
},
{
[title] => room 3
[categories] => cat_2
}

},
{
locations
{
[title] => room 4
[categories] => cat_1
},
{
[title] => room 5
[categories] => cat_3
},
{
[title] => room 6
[categories] => cat_2
}

}
}

Hier mijn poging:

$map_data = json_decode($db_record['data'], true);
uasort($map_data['levels'], function($a, $b) {
return strnatcmp($a['category'], $b['category']);
});

foreach ($map_data['levels'] as $level) {
foreach ($level['locations'] as $location) {
$output .= '<li>';
$output .= '<h2>' . $location['category'] . '</h2>';
$output .= '<h2>' . $location['title'] . '</h2>';
$output .= '</li>';
}
}
$output .= '</div>';
return $output;

Het resultaat moet gesorteerd worden op $locations['category'];

Iemand een idee :)?

Met vriendelijke groet,
Chris
Komt dit uit een database?
Laat dan het sorteren over aan de database.
Jazeker dat is dit stukje:

global $wpdb;
$wpdb->show_errors();
$table = $wpdb->prefix . 'custommaps';
$db_record = $wpdb->get_row("SELECT * FROM $table WHERE id =". $id . "", 'ARRAY_A');

Zou alleen niet weten hoe ik hier ORDERBY of iets dergelijks kan toepassen..
<?php

$db_record = $wpdb->get_row("SELECT * FROM $table WHERE id =". $id . " ORDER BY kolomnaam1 ASC, kolomnaam2 DESC", 'ARRAY_A');


?>

[size=xsmall]Toevoeging op 22/07/2015 13:45:15:[/size]

kolomnaam1 en kolomnaam2 natuurlijk even vervangen voor jouw kolomnamen en ASC voor oplopend en DESC voor aflopend gebruiken. Eventueel mag ASC ook weggelaten worden.
Mogelijk sorteer je alfabetisch, terwijl je numeriek wilt sorteren? Waarom sla je ook "cat_2" op onder categories, in plaats van simpelweg "2"?

Kijk eens naar de types van je waarden, en/of neem de datastructuur eens onder de loep.

Ook mis je het tussenliggende niveau 'locations'?
Ik heb me wat meer in de datastructuur verdiept en kom er achter dat het iets ingewikkelder is dan ik eerst dacht, dus vandaar dat ik mijn vraag opnieuw stel :)
Het is een bestaande plattegrond plugin wat werkt met Json structuur hieruit probeer ik informatie uit te halen. Deze JSON structuur wordt opgeslagen in de tabel "custommaps" in de kolom "data"
Er zitten drie niveau's in de JSON: map-data, levels en locations.
Het niveau locations heb ik nodig deze bevat alle kamers van de plattegrond.

Nu is het gelukt om een lijst van de kamers te krijgen


<?php
global $wpdb;
$wpdb->show_errors(); 
$table = $wpdb->prefix . 'custommaps';
$db_record = $wpdb->get_row("SELECT * FROM $table WHERE id =". $id . "", 'ARRAY_A');
$map_data = json_decode($db_record['data'], true);
		
	foreach ($map_data['levels'] as $level) {
		foreach ($level['locations'] as $location) {
			$output .= '<li>';
			$output .= '' . $location['category'] . '';
			$output .= '<div class="grid-description">' . $location['description'] . '</div>';
			$output .= '</li>';
			}
		}
	$output .= '</ul>';
	return $output;
?>


als we alleen "category" er bij pakken wordt de output dit:

cat_1
cat_2
cat_9
cat_9
cat_10
cat_10
cat_1

Ik zou graag de lijst willen sorteren op category.

Om mijzelf maar te quoten:
Thomas van den Heuvel op 22/07/2015 14:16:59
Waarom sla je ook "cat_2" op onder categories, in plaats van simpelweg "2"?

Bouw je zelf deze data op, of is deze afkomstig van een externe partij?
Dat kan ik zelf beheren

[size=xsmall]Toevoeging op 04/08/2015 10:23:32:[/size]

Na wat knutselen iets gekregen wat resultaat geeft!


<?php
        global $wpdb;
	$table = $wpdb->prefix . 'custommaps';
	$db_record = $wpdb->get_row("SELECT * FROM $table WHERE id = $id", 'ARRAY_A');
	$map_data = json_decode($db_record['data'], true);
	
	foreach ($map_data['levels'] as $level) {
		foreach ($level['locations'] as $location) {
			$title = $location['title'];
			$category = $location['category'];
			$link = $location['link'];
			$image = $location['image'];
			$description = $location['description'];
			
                        // zou dubbele items moeten verkomen...
			 $exists = $wpdb->get_var( $wpdb->prepare("SELECT * FROM 'persons' WHERE title='$title'"));
			
			  if ( ! $exists ) {
				$wpdb->insert("persons", array(
				   "title" => $title,
				   "category" => $category,
				   "link" => $link,
				   "image" => $image,
				   "description" => $description,
				));
			  } else { 
			    echo 'already exists';
			 }
		}
	}
			
	$db_result = $wpdb->get_results("SELECT * FROM persons ORDER BY CAST(`category` AS SIGNED)");
	
	foreach ( $db_result as $row ) {
			echo ''.$row->title.'</br>';
	}
?>


Werkt nog niet hoe het moet.. de check op dubbele items werkt niet en als output geeft het telkens maar een paar resultaten.
Opgelost :D


<?php
	global $wpdb;
	$table = $wpdb->prefix . 'custommaps';
	$db_record = $wpdb->get_row("SELECT * FROM $table WHERE id = $id", 'ARRAY_A');
	$map_data = json_decode($db_record['data'], true);
	
	foreach ($map_data['levels'] as $level) {
		foreach ($level['locations'] as $location) {
			$item = array(
				'link' => $location['link'], 
				'title' => $location['title'], 
				'category' => $location['category'],
				'image' => $location['image'],
				'desc' => $location['description']
			);
			$items []= $item;
		}
	}

	
	function cmp($a, $b)
	{
		return (floatval($b['category']) < floatval($a['category']));
	}
	
	usort($items, "cmp");

	$result .= '<ul class="map-grid">';
	
	foreach ( $items as $output ) {
		$result .= '<li>';
		if ($target != false) $result .= '<a href="' . $target . '?location=' . $location['id'] . '">'; 
			else $result .= '<a href="' . $output['link'] . '">';
		$result .= '<img src="' . $output['image'] . '">';
		$result .= '<h2>' . $output['title'] . '</h2>';
		$result .= '' . $output['category'] . '';
		$result .= '<div class="grid-description">Function:' . $output['description'] . '</div>';
		$result .= '</a></li>';
	}
	$result .= '</ul>';
	return $result;
?>


Bedankt voor het meedenken.

Reageren