PHP/SQL - Put query result into multidimensional array, then sort the multidimensional array

Опубликовано: 06 October 2024
на канале: Digital Dig
3,843
34

I take the result of an SQL query and put it into a multidimensional array in PHP. Then I sort the array. Code follows (but YouTube wouldn't let me include angle brackets so it's just the PHP - replace > with the correct angle bracket)...

include( '../functions/config.php' );

$sql = "SELECT * FROM test_sort";
$get_fruit = mysqli_query( $con_wp, $sql );

while ( $row = mysqli_fetch_array( $get_fruit ) ) {
$items[$row['id']] = array('fruit' => $row['fruit'], 'color' => $row['color'], 'price' => $row['price']);
}

array_multisort( array_column( $items, 'price' ), $items );

print_r($items);