2

I have send array from view to my controller using ajax, then I want compare it with my model. But I don't know how to get array when use ActiveQuery like this:

$riderAll = Riders::find()->select('user_id')->asArray()->all();
$tableData = array_diff($tableData, $riderAll);

Getting error array to string conversion. Tell me what's wrong in $riderAll? please

1
  • 1
    asArray()->all() will return an array having arrays of result rows. I would prefer you to do var_dump($riderAll); and var_dump($tableData);. And see if both are arrays or not. I think yout $tableData is not an array. verify it or show the var_dump plz so the question would be more clear. Commented Dec 17, 2015 at 18:44

3 Answers 3

5

Instead of using all() in activeQuery .. You have to use column() which will give 1-D array so that you can easily apply array_diff()

Try this code ..

   $riderAll = Riders::find()->select ('user_id')->asArray()->column();

   $tableData = array_diff($tableData,$riderAll);

In above you are using all() which will output 2-D array so array_dff() not be applicable. Ask if other Problem occur..

Sign up to request clarification or add additional context in comments.

Comments

1

Try usinge array for select

$riderAll = Riders::find()->select(['user_id'])->asArray()->all();
 $tableData = array_diff($tableData, $riderAll);

7 Comments

this $tabledata array (size=2) 0 => string '' (length=0) 1 => string '189' (length=3) this $riderAll array (size=4) 0 => array (size=1) 'rider_id' => string '1' (length=1) 1 => array (size=1) 'rider_id' => string '5' (length=1) 2 => array (size=1) 'rider_id' => string '121' (length=3) 3 => array (size=1) 'rider_id' => string '189' (length=3)
You have tried to use the array to the field in the select?.. what's happen
yes it is an array. but i don't know what wrong when use array_diff have error like this
Seems your $tableData don't have the same structure of the result of the query.. shoudl be you must manage the result for compare with the $tableData
You can't with active record but is easy with a simlle foreach scan the content and copy the value in an empty simple array.. .. i hope this is usefule... If my answer is right please mark as accepted and/or rate as useful too.
|
1

I have solution I'm create some function to make same structure:

function getArr($array, $key) {
    $return = array();
    foreach($array as $row) {
        $return[] = $row[$key];
    }
    return $return;
}

how to use: $arr = $this->getArr($riderAll, 'rider_id');

then $tableData3 = array_diff($tableData, $arr);

1 Comment

This is the solution i have suggested.. in the comment... for fair you should rate my answer...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.