dtm37893 2019-05-20 20:32
浏览 159
已采纳

将数组元素转换为单个字符串(PHP)

So basically, I use this to retrieve data

$sql = "SELECT * FROM user WHERE id = ".$_POST['id'];
$user = $this->oDb->selectQuery($sql);

^^ that is this:

$aResult = array();
        while($row = $res->fetch_assoc()) {
            $aResult[] = $row;
        }
        return $aResult;

So that would return this:

Array ( [0] => Array ( [id] => 11 [voornaam] => Franklin [tv] => Delano [achternaam] => Roosevelt ) ) 

How do I convert Franklin which is 'voornaam' and Delano which is 'tv' and Roosevelt which is 'achternaam'

to either $user['voornaam']
$user['tv']
$user['achternaam']

so it would result in echo $user['voornaam'] which would say Franklin

or either $voornaam
$tv
$achternaam

or it would result in echo $voornaam which would say Franklin
  • 写回答

2条回答 默认 最新

  • doujia5863 2019-05-20 20:55
    关注
    echo $aResult[0]['voornaam'];
    

    But this will only work, if you fetch just one row. (Not sure without trying, but I think you're whole code just works for returning only one row, because you always reassign the first element of the result)

    Regarding you're other comment it is of course echo $whatEverYouPutTheResultInto[0]['voornaam']

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效