doubingling4706 2018-04-24 12:18
浏览 121
已采纳

PDO - array_map返回键中的对象ID

hey i have array with returned keys

$temp = $sth->fetchAll(PDO::FETCH_ASSOC);

my result looks like this:

[0] => [
    'id' = 11,
    'title' => 't1'
]

[1] => [
    'id' = 12,
    'title' => 't2'
]

if i want to return ids as key i call something like this:

$temp = array_map(function($v){return $v[0];}, $sth->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC));

and my resoult looks like this:

[11] => [
    'title' => 't1'
]

[12] => [
    'title' => 't2'
]

how to return array of objects by ID? when i do this i dont have methods in object...

$temp = array_map(function($v){return $v[0];}, $sth->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_CLASS));
  • 写回答

3条回答 默认 最新

  • dswmmvrg40957 2018-04-24 12:25
    关注

    I will do a bit easier code like below:-

    $fianl_array = array_combine(array_column($temp,'id'),$temp);
    

    Output:- https://eval.in/993728

    Reference:-

    array_column()

    array_combine()

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

报告相同问题?

悬赏问题

  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码