Welcome Guest, Not a member yet? Register   Sign In
Array to string conversion error when sorting array
#1

Hello! Suppose I have a database with a column called "weight" and I have this function in my model:
Code:
public function test()
    {
        return $this->select('weight')
                    ->distinct()
                    ->findAll();
    }
 
Then I call this function in my controller:
Code:
$model = new MyModel();
$test = $model->test();

Then I put the $test variable inside $data and pass it to my view file. Now in the view file I print the array:
Code:
<?php print_r($test); ?>

Here is the result I get:

Array ( [0] => Array ( [weight] => 90 kg ) [1] => Array ( [weight] => 92 kg ) [2] => Array ( [weight] => 100 kg ) [3] => Array ( [weight] => 94 kg ) [4] => Array ( [weight] => 98 kg ) [5] => Array ( [weight] => 125 kg ) )

If I try to sort $test using asort($test), it works. But if I use natsort($test), I get error "Array to string conversion." The same happens if I use array_multisort($test, SORT_NATURAL) or sort($test, SORT_NATURAL). I have no idea why it happens. Could you please help me?
Reply


Messages In This Thread
Array to string conversion error when sorting array - by IvanBell - 11-17-2020, 01:54 PM



Theme © iAndrew 2016 - Forum software by © MyBB