Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Add String to Array

I am wondering how I can add a string variable to the current array. For example, I have an array called $finalarray. Then I have a loop that adds a value on every run. Basically:

$finalarray = $results_array + string;

A very basic structure. I am using this for MySQL so that I can retrieve the final array of the column.

$query = "SELECT * FROM table";
$showresult = mysql_query($query);

while($results_array = mysql_fetch_assoc($showresult))
{
  $finalarray =  $finalarray + $results_array["column"];
}

Edit:

Currently using this code (still not working):

    $query = “SELECT * FROM table”;
$showresult = mysql_query($query);

while($results_array = mysql_fetch_assoc($showresult))
{
  $finalarray[] = $results_array["name"];
}

echo $finalarray;

The problem is that it just says "Array"

Thanks,

Kevin

like image 261
lab12 Avatar asked Mar 15 '26 23:03

lab12


2 Answers

Answer to your edited question:

You cannot use just a single echo to print the entire array contents. Instead

Use

var_dump($finalarray);

or

print_r($finalarray);

to print the array contents..

like image 157
codaddict Avatar answered Mar 18 '26 14:03

codaddict


Use the [] notation. + is for unioning two arrays.

$array = array('foo');

$array[] = 'bar'.

// $array == array('foo', 'bar')
like image 21
deceze Avatar answered Mar 18 '26 14:03

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!