简体   繁体   中英

Getting values in model use asArray Yii2

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

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..

Try usinge array for select

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM