D
D
Doston2018-12-24 09:58:46
Yii
Doston, 2018-12-24 09:58:46

Json getting data from array in Yii2?

Hi all. I am storing the value in the db as json. How to use in_array to select it in Yii2?
So, if the first entry, the category column in the news table is [1, 2, 5], and the second entry, the category column in the news table is [3, 7, 9], and the third entry, the category column in the news table is [2,5, 8], then do I need to have 2 number in the array in the column category;
How can I do that?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Doston, 2018-12-25
@Do-on

It happened,

public static function getTest($id,$lim){
    $m = static::find()->all();
    $massiv = [];
    foreach ($m as $key) {
        if(in_array($id,json_decode(json_encode($key['category']),true))){
        	$y = $key['id'];
        	array_push($massiv, $y);
        }
    }
    return static::find()->where(['IN', 'id', $massiv])->orderBy(["id"=>SORT_DESC])->limit($lim)->all();
}

B
Boris Korobkov, 2018-12-24
@BorisKorobkov

json. How to use in_array

php.net/manual/en/function.json-decode.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question