drfu80954 2016-08-03 06:41
浏览 38

Laravel - 减少阵列大小

Here is my code :

Controller :

 $userOrder = $doctrineOrderRepository->getOrders(Auth::id());

Doctrine:

   public function getUserOrders( $userId ) :array
    {

        $results = $this->_em->createQueryBuilder()
            ->select( 'o.restaurantId as rtId, o.oCount, o.oPrice ,
                     o.oCreated', 'o.oId' )
            ->from( $this->entityClass, 'o' )
            ->where( "o.userId = '{$userId}'" )
            ->andWhere( "o.orderItemCount > '0'" )
            ->andWhere( "o.orderTotalPrice > '0'" )
            ->getQuery()->getArrayResult();

        return $results;
    }

I get this array from my database Using Doctrine :

array:1 [▼
  0 => array:5 [▼
    "rtId" => 154
    "oCount" => 2
    "oPrice" => 33900
    "oCreated" => DateTime {#677 ▶}
    "oId" => 17428
  ]
]

I'm using this Doctrine method in different situations. In this case , I just use two item of all list : rtId , oCount

So , how to modify $userOrder to convert result array to this bellow array?

array:1 [▼
  0 => array:2 [▼
    "rtId" => 154
    "oCount" => 2
  ]
]
  • 写回答

2条回答 默认 最新

  • duankange2433 2016-08-03 06:58
    关注

    foreach your $userOrder in controller and unset values like this

    $newArr = array();
    
        foreach($userOrder as $ar)
        {
            unset($ar['oPrice']);
            unset($ar['oCreated']);
            unset($ar['oId']);
            $newArr[] = $ar;
        }
    
    dd($newArr);
    

    you will get this result..

    array:1 [▼
      0 => array:2 [▼
        "rtId" => 154
        "oCount" => 2
      ]
    ]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?