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

将数组元素转换为单个字符串(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

图片转代码服务由CSDN问答提供 功能建议

基本上,我用它来检索数据

  $  sql =“SELECT * FROM user WHERE id =”。$ _ POST ['id']; 
 $ user = $ this-> oDb-> selectQuery($ sql); 
    
 
 

^^就是这样:

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

这样会返回: \ n

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

如何转换Franklin,即'voornaam' 和Delano,即'tv' 和Roosevelt'achternaam'

 到$ user ['voornaam'] 
 $ user ['tv'] 
 $ user ['achternaam'] 
 
它会导致echo $ user ['voornaam']会说 富兰克林
 \既不是$ voornaam 
 $ tv 
 $ achternaam 
 \也不会导致echo $ voornaam,这会说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']

    已采纳该答案
    打赏 评论
  • duanchi19820419 2019-05-20 20:51

    The result is already stored in an array.
    You can simply call it like:

    echo $aResult[0]['voornaam'];   
    

    which will give you output Franklin

    打赏 评论

相关推荐 更多相似问题