Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组推送在向数组添加值后创建其他元素_Php_Arrays - Fatal编程技术网

Php 数组推送在向数组添加值后创建其他元素

Php 数组推送在向数组添加值后创建其他元素,php,arrays,Php,Arrays,我有一个while循环,我从中获取帖子id并将它们添加到数组中,如下所示 $tobi = array(); while($result = mysqli_fetch_array($msql)){ $imp = $result['msg_id']; // these id are like 316 etc array_push($tobi, $imp); } print_r($tobi); 但当我打印数组时,结果是 数组([0]=>316)数组([0]=>315) 为什么数组创建了另一个元素?

我有一个while循环,我从中获取帖子id并将它们添加到数组中,如下所示

$tobi = array();
while($result = mysqli_fetch_array($msql)){
$imp = $result['msg_id'];  // these id are like 316 etc
array_push($tobi, $imp);
}
print_r($tobi);
但当我打印数组时,结果是

数组([0]=>316)数组([0]=>315)

为什么数组创建了另一个元素? 我希望数组是这样的

Array ( [0] => 316 [1] => 315 )
我尝试使用$tobi[]=$imp;也得到了同样的结果

完成打印后(结果)

完成打印后($msql)


用这个。这可能对你有帮助

$tobi = array();
while($result = mysqli_fetch_array($msql)){
$imp = $result['msg_id'];  
$tobi[]= $imp;
}
print_r($tobi);

试试这个,我希望它能起作用

$tobi = array(); $new_arr = array();
while($result = mysqli_fetch_array($msql)){
$imp = $result['msg_id'];
$new_arr[] = array_push($tobi, $imp);
}
print_r($new_arr);
试试这个:

$tobi = array();
while($result = mysqli_fetch_array($msql)){
if(isset($result['msg_id']))
 {

$imp = $result['msg_id'];  // these id are like 316 etc
array_push($tobi, $imp);
 }
}
print_r($tobi);

你做得太多了。从评论中删除您的SQL

$msql = mysqli_query($connecDB, "SELECT msg_id FROM messages WHERE time > $last_post_id ORDER BY time DESC")
只有在调用
$imp=$result['msg\u id']时才获取msg\u id您正在创建它。因此,您的数组很混乱

你应该能够做到:

$tobi = array();
while($result = mysqli_fetch_array($msql)){

    $tobi[] =  $result[0];
}
print_r($tobi);
更新

我不知道为什么会给出奇怪的结果。它似乎以我们在提供的代码中看不到的方式输出。我使用了我的数据库并运行:

$msql = mysqli_query($link, "SELECT f_name FROM leads");
有5个条目2没有f_名称。当我打印($msql)
时,我收到了以下信息:

mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 5 [type] => 0 )
然后,我用返回的数字和关联(两种方式)运行上面的代码:


在这个集合中,我还能够运行OP原始代码并获得预期的结果。

$tobi=array()每次迭代都是一个新的数组!对不起,$tobi=array();已超出while循环,生成此输出的代码行在哪里?@Rizier123 print\u r($tobi);请给出var\u dump($result)的结果,不起作用。请检查是否存在更新编辑导致数组([0]=>318)数组([0]=>317)数组([0]=>318[1]=>317)的问题,默认情况下
mysqli\u fetch\u fetch\u Array
将返回一个数组,其中包含两个数组索引,因此“您调用
$imp=$result['msg\u id'];
您正在创建该数组”不正确,因为关联索引也应work@JeffPuckettII使用$tobi[]=$result[0]后;我得到了这个数组([0]=>318)数组([0]=>317)数组([0]=>318[1]=>317)@SagarSingh,是的,我并不奇怪这个答案不起作用。我在评论为什么它不重要。@JeffPuckettII PHP是否将关联数组存储在其他位置?根据OP for print_r($msql)中提供的结果,它是数字数组
$msql = mysqli_query($link, "SELECT f_name FROM leads");
mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 5 [type] => 0 )
Array ( [0] => [1] => TEST [2] => [3] => Coury [4] => 222222 )