array_merge()我如何添加一个数组名的“范围” [英] array_merge() How can I add a 'range' of an array name
问题描述
我有一个$ NR变量作为数组与我在previous函数创建的同名号,是这样的:
是$ var ='_ sorteios'$ NR。
$$ VAR =阵列($ sorteio_id);
我有它在一段时间函数,因此创建像3阵列的名称:
$ sorteios_1,$ sorteios_2,$ sorteios_3
,我想给他们增加一个array_merge在里面,所以我必须使用$ NR写着同名的很多阵列是如何创建的。
$ NR = 3;
我想,最后的结果看起来是这样的。
$结果= array_merge($ sorteios_1,$ sorteios_2,$ sorteios_3);
这就是全部的功能,如果你要检查它(这是因为我遇到的问题不完全):
函数check_sorteios(){全球$分贝;$ n = $ _SESSION ['用户id'];$ QUERY1 =SELECT * FROM sorteios其中userid = $编号;
$ RESULT1 = $ DB-GT&;查询($ QUERY1);
$数= $ result1-> rowCount时();如果($计数== 0){$ sorteios = 0;回声SEM sorteios;}其他{
$ NUMERO = 0;
$ sorteios = 0;
$ NR = 0;而($ ROW1 = $ result1->取()){
如果($ NUMERO == $计数){返回0;}$ NUMERO ++;
$ sorteio_id = $ ROW1 ['身份证'];$ QUERY2 =SELECT * FROM WHERE PRODUCTOS ID = $ sorteio_id;
$结果2 = $ DB-GT&;查询($ QUERY2);
而($ ROW2 = $ result2->取()){$数据= $ ROW2 ['数据'];
$ TITULO = $ ROW2 ['TITULO'];如果(的strtotime($数据)GT;时间()){
如果(!使用isset($$ sorteio_id)){
$$ sorteio_id = 1;
}$ NR ++;
是$ var ='_ sorteios'$ NR。
$$ VAR =阵列($ sorteio_id);
}
}
}
}$结果= array_merge($ sorteios_1,$ sorteios_2,$ sorteios_3);$ OCCURENCES = array_count_values($结果);
的print_r($ OCCURENCES);}
有没有你不能递归合并的理由?
如果($ NR大于0){
$结果= $ sorteios_1;
为($ i = 2; $ I< = $ NR ++ $ I){
$结果= array_merge($结果,$ {'sorteios _'$ I});
}
}其他{
//你可能想以不同的方式处理这种情况
$结果=阵列();
}
I have a $nr variable that as the number of arrays with the same name that i created in a previous function, something like this:
$var = 'sorteios_'.$nr;
$$var = array($sorteio_id);
I have it in a While function so it was created something like 3 arrays with the names:
$sorteios_1 , $sorteios_2 , $sorteios_3
And i want to add them inside an array_merge, so i have to use the $nr that says how many arrays with the same name were created.
$nr = 3;
i want that the final result looks something like this.
$result = array_merge($sorteios_1, $sorteios_2, $sorteios_3);
That's the whole function if you want to check it (it's not complete because of the problem i'm having):
function check_sorteios(){
global $db;
$id = $_SESSION['userid'];
$query1 = "SELECT * FROM sorteios WHERE userid = $id";
$result1 = $db->query($query1);
$count = $result1->rowCount();
if ($count == 0){ $sorteios = 0; echo "sem sorteios";}
else{
$numero = 0;
$sorteios = 0;
$nr = 0;
while($row1 = $result1->fetch()){
if ( $numero == $count ){ return 0;}
$numero++;
$sorteio_id = $row1['id'];
$query2 = "SELECT * FROM productos WHERE id = $sorteio_id";
$result2 = $db->query($query2);
while($row2 = $result2->fetch()){
$data = $row2['data'];
$titulo = $row2['titulo'];
if (strtotime($data) > time()){
if(!isset($$sorteio_id)){
$$sorteio_id = 1;
}
$nr++;
$var = 'sorteios_'.$nr;
$$var = array($sorteio_id);
}
}
}
}
$result = array_merge($sorteios_1, $sorteios_2, $sorteios_3);
$occurences = array_count_values($result);
print_r($occurences);
}
Is there a reason you can't recursively merge?
if ($nr > 0) {
$result = $sorteios_1;
for ($i = 2; $i <= $nr; ++$i) {
$result = array_merge($result, ${'sorteios_'.$i});
}
} else {
// you might want to handle this case differently
$result = array();
}
这篇关于array_merge()我如何添加一个数组名的“范围”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!