1

i have a while loop from which im fetching post id and adding them to array like shown below

$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);

but when i printed the array it resulted

Array ( [0] => 316 ) Array ( [0] => 315 )

why the array has created another element? i want the array to be like

Array ( [0] => 316 [1] => 315 )

i tried using $tobi[] = $imp; also but same result got

after doing print_r($result);

Array ( [0] => 318 [msg_id] => 318 ) Array ( [0] => 318 ) Array ( [0] => 317 [msg_id] => 317 ) Array ( [0] => 317 ) 

after doing print_r($msql);

mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 2 [type] => 0 ) Array ( [0] => 318 ) Array ( [0] => 317 ) 
22
  • 2
    $tobi = array(); Every iteration a fresh array! Commented Apr 26, 2016 at 13:56
  • sorry the $tobi = array(); is already out of while loop Commented Apr 26, 2016 at 13:57
  • And where is the code line to produce this output? Commented Apr 26, 2016 at 13:59
  • @Rizier123 print_r($tobi); Commented Apr 26, 2016 at 14:00
  • 1
    Please give the result for var_dump($result) Commented Apr 26, 2016 at 14:10

4 Answers 4

2

You're doing too much. Your SQL from the comment

$msql = mysqli_query($connecDB, "SELECT msg_id FROM messages WHERE time > $last_post_id ORDER BY time DESC")

is only getting the msg_id so when you call $imp = $result['msg_id']; you are creating that. Hence why your Arrays were confusing.

you should be able to just do:

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

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

UPDATE

I am not sure why the odd results are being given. It seems that is outputting in ways we cannot see in the code provided. I used my database and ran:

$msql = mysqli_query($link, "SELECT f_name FROM leads");

There are 5 entries 2 do not have f_name. When I did a print_r($msql) I received the following:

mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 5 [type] => 0 )

I then ran my code above with both numeric and associative which returned (both ways):

Array ( [0] => [1] => TEST [2] => [3] => Coury [4] => 222222 )

Also on this set I was able to run the OP original code and get the expected results.

Sign up to request clarification or add additional context in comments.

13 Comments

it resulted in Array ( [0] => 318 ) Array ( [0] => 317 ) Array ( [0] => 318 [1] => 317 )
by default mysqli_fetch_array will return one array with both array indexes, so "you call $imp = $result['msg_id']; you are creating that" isn't correct because the associative index should also work
@JeffPuckettII after using $tobi[] = $result[0]; i got this Array ( [0] => 318 ) Array ( [0] => 317 ) Array ( [0] => 318 [1] => 317 )
@SagarSingh, yes I am not surprised this answer doesn't work. I was commenting why it shouldn't matter.
@JeffPuckettII does PHP store the associative array elsewhere? Based on the results provided in OP for print_r($msql) it is numeric arrays
|
0

Try this, I hope this will work!

$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);

Comments

0

Try this:

$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);

1 Comment

no it does't work please check question i have updated
-1

use this. it may help you.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.