Im receiving the following variable sent as a json array from javascript: $_REQUEST['fb_friend_uid']
I then decode the array and then do a var_dump
$string = json_encode($_REQUEST['fb_friend_uid']);
$result = json_decode($string, true);
var_dump($result);
The var_dump shows me the following
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..
How would I go about saving all values from 'id' into one new $var_string ? - formatted like: "263901486, 502533736, 506526230"
Objective is to use the string in an sql query like so:
SELECT * FROM vote WHERE
vote_fb_uid IN ($var_string)
Try this :
$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);
If you want all the items to be combined to one long string you can try imploding the array http://php.net/manual/en/function.implode.php
$varString = implode(',', $_REQUEST['fb_friend_uid'])
I don't really know PHP, but looking at the docs, you should be able to write something along the lines of:
$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);
(Adapted from Prasanth Bendra's answer.)
Try this
$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)";