简体   繁体   中英

How to convert array into strings in PHP?

I have a query that returns a row from database.

I would like to convert resulting array to strings, where each string is named as the dabase header, so I can use those strings in code.

I am currently doing it like below, but that doesn't seem like a good practice (or?)?

foreach($news_array as $position_in_main_array => $inner_array_member)
{           
    echo $inner_array_member["Date"];
    echo $inner_array_member["Published"];
    echo $inner_array_member["Title"];
    echo $inner_array_member["Text"];
}

Thanks

you can use implode function

$headarray = array($inner_array_member["Date"],$inner_array_member["Published"],$inner_array_member["Title"],$inner_array_member["Text"]);

echo implode(" ",$headarray);

You can use

complete code

foreach($news_array as $position_in_main_array => $inner_array_member)
{           
    $headarray[] = $inner_array_member["Date"];
    $headarray[] = $inner_array_member["Published"];
    $headarray[] = $inner_array_member["Title"];
    $headarray[] = $inner_array_member["Text"];
}

echo implode(" ",$headarray);

Thanks Amit

foreach($news_array as $position_in_main_array => $inner_array_member)
{           
    foreach($inner_array_member as $key =>  $value)
    {           
        echo $key.": ".$value." ";
    }
    //new line echo "\n";
    echo "<br>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM