如何仅将数字从数组保存到新数组 [英] how to save numbers only from an array into a new array

查看:84
本文介绍了如何仅将数字从数组保存到新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有var: $ _ REQUEST ['fb_friend_uid'] ,它为我提供以下输出:

  Array { returned_val:[ 47483647, 47483647, 47483647, 665414807, 263901486, 665414807, 665414807, 665414807]} $我想将这里的数据保存到一个新的数组中,只包含格式为的数字;  47483647,47483647等 



目标是像这样在sql查询中使用它:

 选择*从投票位置
voice_fb_uid IN($ myNumbers)

将其保存到我认为的新阵列中可以做到:

  foreach($ _REQUEST ['fb_friend_uid'] as $ uid){
$ uids [] = $ uid ['id'];
}

$ ids = join(’,’,$ uids);

但是我的问题仍然存在,如何清除第一个变量以仅包含数字。
建议?

解决方案

我无法为您提供 exact 解决方案,因为我'不确定 $ _ REQUEST ['fb_friend_uid'] 返回的值是否是使用 json_encode(),或者该值实际上是一个json字符串。



在任何一种情况下,都有一个使用这两种情况的示例,因此请在您的情况下使用任何一种有意义的方法:



如果使用PHP数组:



假定PHP Array的格式类似于:



array('returned_val'=> array('47483647','47483647','47483647','665414807','263901486','665414807',' 665414807','665414807'));

  <?php 
$ original_arr = $ _REQUEST ['fb_friend_uid' ] ['returned_val'];

如果JSON字符串:



假定JSON字符串的格式类似于:



{ returned_val:[ 47483647, 47483647, 47483647, 665414807 , 263901486, 665414807, 665414807, 665414807]}

 <?php 
$ json_arr = json_decode($ _ REQUEST ['fb_friend_uid'],True);
$ original_arr = $ json_arr [’returned_val’];

然后,使用此代码:

 <?php 
//仅提取整数值,忽略不为0-9字符的任何内容。
$ filtered_arr = array_filter($ original_arr,'ctype_digit');

//转义值以消除SQL注入的可能性。
$ filtered_arr = array_map('mysql_real_escape_string',$ filtered_arr);

//将数组转换为字符串
$ string_arr =’。 implode(’,’,$ filtered_arr)。 ’;

//执行SQL查询
mysql_query( SELECT * FROM投票,到那里表决_fb_uid输入($ string_arr));


I currently have var: $_REQUEST['fb_friend_uid'] which gives me the following output:

Array{"returned_val":["47483647","47483647","47483647","665414807","263901486","665414807","665414807","665414807"]}

Im looking to save the data here into a new array, containing only the numbers in a format of; 47483647, 47483647, etc

The objective is to use it in a sql query like so:

SELECT * FROM vote WHERE
vote_fb_uid IN ($myNumbers)

Saving it into a new array I figured could be done like so:

foreach ($_REQUEST['fb_friend_uid'] as $uid) {
    $uids[] = $uid['id'];
}

$ids = join(',', $uids); 

However my issue remains, how to "clean" the first var to contain numbers only. Suggestions?

解决方案

I can't give you an exact solution, because I'm not sure if the value returned by $_REQUEST['fb_friend_uid'] is a PHP array printed using json_encode(), or the value is actually a json string.

In either case, where is an example which makes use of both circumstances, so use whichever one makes sense in your scenario:

If PHP Array:

Assumes PHP Array has a format similar to:

array('returned_val' => array('47483647', '47483647', '47483647', '665414807', '263901486', '665414807', '665414807', '665414807'));

<?php
$original_arr = $_REQUEST['fb_friend_uid']['returned_val'];

If JSON String:

Assumes the JSON String has a format similar to:

{"returned_val":["47483647","47483647","47483647","665414807","263901486","665414807","665414807","665414807"]}

<?php
$json_arr = json_decode($_REQUEST['fb_friend_uid'], True);
$original_arr = $json_arr['returned_val'];

Then, use this code:

<?php
// Extract only whole number values, omit anything which is not a 0-9 character.
$filtered_arr = array_filter($original_arr, 'ctype_digit');

// Escape values to remove possibility of SQL injection.
$filtered_arr = array_map('mysql_real_escape_string', $filtered_arr);

// Convert the array to a string
$string_arr = "'" . implode("','", $filtered_arr) . "'";

// Perform SQL Query
mysql_query("SELECT * FROM vote WHERE vote_fb_uid IN ($string_arr)");

这篇关于如何仅将数字从数组保存到新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆