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
  ]
]

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
  ]
]

If you are using Laravel, you can use the forever handy collection class. It has dozens of useful methods. In this case you could do:

$cleanedResults = collect($results)->map(function($item) {
    return collect($item)->only(['rtId', 'oCount']);
})->toArray();