$result = mysqli_query($con, "SELECT * FROM users"); $usersArray=[]; tableArrayPushData($result, $usersArray); function tableArrayPushData($result, $tableArray){ while ($row = $result->fetch_assoc()) { $str = ''; foreach ($row as $value) { $str = $str.$value.'|'; } $newStr = rtrim($str, "| "); array_push($tableArray,$newStr); } } for ($i=0; $i<count($usersArray); $i++){//array is always empty at this point echo "Ok"; echo "<br>"; }
I don’t understand why, but usersArray
is empty despite the fact that I added data there.
The MySQL table has rows with data, so it can’t be empty.
Advertisement
Answer
You should use the &
operator to allow the function to access the outer variable, like this:
function tableArrayPushData($result, &$tableArray) {}
Or use return
.