I am trying create an array in a foreach loop, and then sort it by a key.
The for each loop creating the array looks like this:
public function index(){
$query=$this->My_model->get_data();
foreach ($query as $row)
{
$data=array(
Array('Points'=>$points,'Name'=>$row['Name'], 'Phone'=>$row['phone']),
);
function cmp ($a, $b) {
return $a['Points'] < $b['Points'] ? 1 : -1;
}
usort($data, "cmp");
print_r($data);
}
}
But this only returns the first the first item in the array.
However when I input some array items directly such as the below, it works fine and sorts all the array items.
public function index(){
$query=$this->My_model->get_data();
foreach ($query as $row)
{
$data = array (
Array ( 'Points' => 500, 'Name' => 'James Lion' ) ,
Array ( 'Points' => 1200, 'Name' => 'John Smith' ),
Array ( 'Points' => 700, 'Name' => 'Jason Smithsonian' ) );
function cmp ($a, $b) {
return $a['Points'] < $b['Points'] ? 1 : -1;
}
usort($data, "cmp");
print_r($data);
}
}
How do I fix this so that the code in the first snippet, so that works as it does in the second snippet?