Ehsan Ehsan - 21 days ago 10
PHP Question

PHP - storing array in one variable

i want to store

$user
in
$message_lnk
for using at
$content_lnk = array('chat_id' => $chatid,'text' => "$message_lnk" );


but my snippet not work. help me please !

foreach ($result as $user) {
$user = $user['movie_lnk'];
}
$message_lnk = print_r($user, true);
$content_lnk = array('chat_id' => $chatid,'text' => "$message_lnk" );

Answer

you're not building an array with this code, you're just setting $user to the last value within $result

You should have another variable, $links and do the following within the for loop.

$links[]=$user['movie_lnk'];

And then on line 3, if you want to store the array as a string use:

$message_lnk=implode(", ", $links);

Or as an array:

$message_lnk=$links; $content_lnk=array('chat_id'=>$chat_id, 'text'=>$message_lnk)

Notice I removed the quotation marks around $message_lnk

Hope that helps.

Comments