Reputation:
I have an array that shows properly with print_r but when I convert it to a string it only shows array,array,array,array
instead of the actual array values.
if (!$result) {
printf("Query failed: %s\n", $mysqli->error);
exit;
}
while($row = $result->fetch_row()) {
$recipients[]=$row;
}
print_r($recipients);
$ids = implode(',', $recipients);
echo $ids;
The output is:
Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 3 ) [2] => Array ( [0] => 100 ) [3] => Array ( [0] => 118 ) [4] => Array ( [0] => 142 ) [5] => Array ( [0] => 276 ) [6] => Array ( [0] => 308 ) ) Array,Array,Array,Array,Array,Array,Array
Upvotes: 1
Views: 143
Reputation: 5689
$input = array( 0 => array( 0 => 1 ), 1 => array( 0 => 3 ), 2 => array( 0 => 100 ), 3 => array( 0 => 118 ), 4 => array( 0 => 142 ), 5 => array( 0 => 276 ), 6 => array( 0 => 308 ) );
To apply implode()
function on a multi dimensional array, You could make use of array_map()
echo implode(', ', array_map(function ($entry) {
return $entry[0];
}, $input));
From php v5.5.0, array_column
:
echo implode(', ', array_column($input, 0));
Upvotes: 0
Reputation: 14649
mysqli_result::fetch_row
-- mysqli_fetch_row — Get a result row as an enumerated array. You need to access indexes of the array to get data.
To access indexes of an array in PHP, you would form a operation.
$array[X]
In this instance X
can be an integer, or a string, but in this case, mixed mysqli_result::fetch_row
returns an array in order, so you would access it in like any other array (e.g. array[0], ...)
Upvotes: 2
Reputation: 5745
You are adding an array to the recipient array not just the id value
Use this instead
while($row = $result->fetch_row()) {
$recipients[]=$row[0]; //gets the id assuming that is the first element in the row
}
Upvotes: 2