从单维值/密钥JSON中检索多个值

时间:2014-05-27 03:18:43

标签: php json

我正试图解决这个问题。

"ContentBlock1":["2","22"]

我一直在尝试将2和22放入逗号sepertaed字符串中,以便我可以在MySQL IN(2,22)查询中使用它。

我目前尝试了几种方法,但似乎没有一种方法适用于我。

$ContentBlock = my json data;

$cid = json_decode($ContentBlock,true);     

foreach ($cid as $key){ 

    $jsoncid = "$key ,";

}

然后:

SELECT * FROM content 
WHERE featured=1 AND state=1 AND catid IN($jsoncid) 
ORDER BY ordering ASC LIMIT 4");

1 个答案:

答案 0 :(得分:0)

你可以像下面这样做

$c='{"ContentBlock1":["2","22"]}';

$cid = json_decode($c);  

// RecursiveIterator will search even sub arrays

$new = new RecursiveIteratorIterator(new RecursiveArrayIterator($cid));
foreach($new as $v) {
  $r[]= $v;
}

echo "SELECT * FROM content WHERE featured=1 AND state=1 AND catid IN('".implode("','",$jsoncid)."') ORDER BY ordering ASC LIMIT 4";

<强> 输出

SELECT * FROM content WHERE featured=1 AND state=1 AND catid IN('2','22') ORDER BY ordering ASC LIMIT 4
相关问题