PHP - 将数组存储在一个变量中[关闭]

PHP  - 将数组存储在一个变量中[关闭]

问题描述:

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

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.

<?php
$links[]=$user['movie_lnk'];

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

<?php
$message_lnk=implode(", ", $links);

Or as an array:

<?php 
$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.

This might be more like what you are expecting but this also overwrites the value on each iteration

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

so, perhaps add the $content_lnk to an array?

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