I've been looking for a few hours for an answer to this, and a lot of what I found either doesn't work or is just off what I need. But here is my situation:
I have an array, as follows:
Array (
'0' => Array (
'city' => Therizo
'city_id' => 10722
'nation_id' => 1459
'nation' => Synkomdi
'infra' => 2500.00
)
'1' => Array (
'city' => Anoixi
'city_id' => 11822
'nation_id' => 1459
'nation' => Synkomdi
'infra' => 2500.00
)
'2' => Array (
'city' => Alitheia
'city_id' => 14256
'nation_id' => 1459
'nation' => Synkomdi
'infra' => 2500.00
)
'3' => Array (
'city' => Vlastisi
'city_id' => 15117
'nation_id' => 1459
'nation' => Synkomdi
'infra' => 2500.00
)
'4' => Array (
'city' => Gyri
'city_id' => 20507
'nation_id' => 1459
'nation' => Synkomdi
'infra' => 2500.00
)
'5' => Array (
'city' => Prasino Pyrgoi
'city_id' => 21486
'nation_id' => 1459
'nation' => Synkomdi
'infra' => 2500.00
) ....
)
As you can see, it's a nested array (so an array of arrays essentially). I need to sort the arrays by the "infra" value.
I already tried the sort method, which I read more about on W3 schools here. I then tried the multisort method using the PHP documentation here. Both sort and multisort returned either "true" or 1.
My code for each: sort:
<?php
[...]
echo sort($checked);
# checked is the array I linked above
?>
echo sort() returned 1; The second method I tried, which I was more sure about:
$infra = array_column($checked, 'infra');
echo array_multisort($infra, SORT_DESC, $checked);
Returned "true", I then resorted to the pre-PHP 5 method of using multisort:
foreach ($checked as $key => $row) {
$infra[$key] = $row['infra'];
}
$checked = array_multisort($infra, SORT_DESC, $checked);
I have no idea how to proceed since none of these worked and I've never sorted by a specific key-value only, in nested arrays. Is it even possible? And if so, how?
Edit: Someone asked if I had control over how the array is built, the answer is yes, I am making the array from individual database queries.
Edit Part 2: And here is the full code, for better reference.
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
# Step 1: Get the nation from GET_nation
$stmt = $conn->prepare("SELECT * FROM `hour_nations_v2` WHERE nation_id=$natid OR nation='$natid'");
$stmt->execute();
$nation = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$nation = $stmt->fetchAll();
if(empty($nation)) {
echo json_encode(array("success" => false, "data" => "Nation not found"));
}
else {
$min = $nation[0]['score'] * 0.25;
$max = $nation[0]['score'] * 1.25;
# Step 2: Get all nations
$stmt = $conn->prepare("SELECT * FROM `hour_nations_v2` WHERE alliance_id='$id' OR alliance='$id'");
$stmt->execute();
$targets = $stmt->fetchAll();
foreach($targets as $thisTarget) {
if($thisTarget['score'] < $max && $thisTarget['score'] > $min && $thisTarget['defensive_wars'] < 3) {
$stmt = $conn->prepare("SELECT * FROM `hour_cities` WHERE nation_id=" . $thisTarget['nation_id']);
$stmt->execute();
$cities = $stmt->fetchAll();
foreach($cities as $thisCity) {
$checked [] = array("city" => $thisCity['city_name'], "city_id" => $thisCity['city_id'], "nation_id" => $thisTarget['nation_id'], "nation" => $thisTarget['nation'], "infra" => $thisCity["infra"]);
}
}
}
$infra = array_column($checked, 'infra');
foreach ($checked as $key => $row) {
$infra[$key] = $row['infra'];
}
$checked = array_multisort($infra, SORT_DESC, $checked);
echo json_encode(array("success" => true, "data" => $checked));
}