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 刚接触VFP,我想知道为什么是这种情况,有人可以看看吗
  • ¥15 io.jsonwebtoken.security.Keys
  • ¥15 急,ubuntu安装后no caching mode page found等
  • ¥15 联想交换机NE2580O/NE1064TO安装SONIC
  • ¥15 防火墙的混合模式配置
  • ¥15 Ubuntu不小心注销了要怎么恢复啊
  • ¥15 win10电脑安装完plcsim advanced4.0运行时为什么会提示找不到虚拟网卡
  • ¥15 安装powerbuilder10卡在安装程序正在运行这个页面 没有下一步任何指令
  • ¥15 关于mpi的问题:请问遇到这种情况需要怎么解决,出现这个问题后电脑不能进行mpi多核运行只能进行单核运行
  • ¥50 微信聊天记录备份到电脑提示成功了,但还是没同步到电脑微信