0

我从 javascript 接收以下作为 json 数组发送的变量:$_REQUEST['fb_friend_uid']

然后我解码数组,然后做一个 var_dump

$string = json_encode($_REQUEST['fb_friend_uid']);
$result = json_decode($string, true);

var_dump($result);

var_dump 向我展示了以下内容

array (size=360)
  0 => 
    array (size=1)
      'id' => string '263901486' (length=9)
  1 => 
    array (size=1)
      'id' => string '502533736' (length=9)
  2 => 
    array (size=1)
      'id' => string '506526230' (length=9)
  3 => 
    array (size=1)
      'id' => string '507245473' (length=9)
    etc..

我将如何将 'id' 中的所有值保存到一个新的 $var_string 中?- 格式如下:“263901486, 502533736, 506526230”

目标是在 sql 查询中使用字符串,如下所示:

SELECT * FROM vote WHERE
vote_fb_uid IN ($var_string)
4

4 回答 4

1

尝试这个 :

$string = json_encode($_REQUEST['fb_friend_uid']);
$result = json_decode($string, true);

$res_array   = array();
foreach($result as $val){
   $res_array[]  = $val['id'];
}

$res_string = implode(",",$res_array);
于 2013-05-29T10:05:49.147 回答
0

如果您希望将所有项目组合成一个长字符串,您可以尝试将数组内爆 http://php.net/manual/en/function.implode.php

$varString = implode(',', $_REQUEST['fb_friend_uid'])

于 2013-05-29T10:02:55.790 回答
0

我不太了解 PHP,但是查看文档,您应该能够编写以下内容:

$string = json_encode($_REQUEST['fb_friend_uid']);
$result = json_decode($string, true);

$res_array = array_map(function ($val) { return $val['id']; });

$res_string = join(",", $res_array);

(改编自 Prasanth Bendra 的回答。)

于 2013-05-29T10:20:44.117 回答
0

尝试这个

$string = json_encode($ar);
$result = json_decode($string, true);

$res_array   = array();
foreach($result as $val){
   $res_array[]  = $val['id'];
}

$res_string = "'".implode("','", $res_array)."'";

$qry = "SELECT * FROM vote WHERE vote_fb_uid IN ($res_string)";
于 2013-05-29T10:37:11.140 回答