数组的一些事

数组在拼装和显示的时候如果,显示层级太多,造成阅读和立即的不便。

好的写法  ,

$productsData 数组操作
public function forwardProductList($manufacturerProducts)
    {
        $response = array(
            'message'      => 'No manufacturer product found',
            'isSuccessful' => false,
            'data'         => array(),
        );

        $productsData = array();

        foreach($manufacturerProducts as $manufacturerProduct){
            $imageData = array();
            $productImages = $manufacturerProduct->getActiveImages();
            foreach($productImages as $image){
                $imageData[] = array(
                    'url'       => $this->assetHelper->getUrl($image->getImageLocation(), 'manufacturer_product'),
                    'imageId'   => $image->getManufacturerProductImageId(),
                    'isPrimary' => $image->getIsPrimary(),
                );
            }
            $productUnitsData = array();
            $productUnits = $manufacturerProduct->getActiveUnits();
            foreach($productUnits as $productUnit){
                $productUnitsData[] = array(
                    'combinationid' => $productUnit->getManufacturerProductUnitId(),
                    'sku'           => $productUnit->getSku(),
                    'originalPrice' => $productUnit->getPrice(),
                    'finalPrice'    => $productUnit->getDiscountedPrice(),
                    'discount'      => $productUnit->getDiscountPercentage(),
                    'inventory'     => $productUnit->getQuantity(),
                    'weight'        => $productUnit->getWeight(),
                    'height'        => $productUnit->getHeight(),
                    'length'        => $productUnit->getLength(),
                    'width'         => $productUnit->getWidth(),
                    'attributes'    => $productUnit->getCombinationString(),
                    'moq'           => $productUnit->getMoq(),
                    'name'          => '',
                );
            }

            $productsData[] = array(
                'name'        => $manufacturerProduct->getName(),
                'referenceno' => $manufacturerProduct->getReferenceNumber(),
                'description' => $manufacturerProduct->getDescription(),
                'datecreated' => $manufacturerProduct->getDateAdded(),
                'images'      => $imageData,
                'combination'  => $productUnitsData,
            );
        }

        if(count($productsData) > 0){
            $response['message'] = "API Endpoint is currently unavailable";
            try{
                $request = new FormRequest(
                    FormRequest::METHOD_POST, '/'.$this->configuration['routes']['create_products'], $this->configuration['baseurl']
                );
                $request->setFields($productsData);
                $buzzResponse = new Response();
                $client = new Curl();
                $client->setTimeout(self::API_TIMEOUT_SEC);
                $client->send($request, $buzzResponse);

                if ($buzzResponse->isSuccessful()) {
                    $content = json_decode($buzzResponse->getContent(), true);
                    $response['isSuccessful'] = true;
                    $response['message'] = "Product created";
                    $response['data'] = $content;
                }
            }
            catch(RequestException $e){
                $response['message'] = "API endpoint request has timeout";
            }
        }

        return $response;
    }

不好的写法

public function forwardProductList($manufacturerProducts)
    {
        $response = array(
            'message'      => 'No manufacturer product found',
            'isSuccessful' => false,
            'data'         => array(),
        );

        $productsData = array();

        foreach($manufacturerProducts as $k => $manufacturerProduct){
            $productImages = $manufacturerProduct->getActiveImages();
            foreach($productImages as $image){
                $productsData[$k]['images'][] = array(
                    'url'       => $this->assetHelper->getUrl($image->getImageLocation(), 'manufacturer_product'),
                    'imageId'   => $image->getManufacturerProductImageId(),
                    'isPrimary' => $image->getIsPrimary(),
                );
            }
            $productUnitsData = array();
            $productUnits = $manufacturerProduct->getActiveUnits();
            foreach($productUnits as $productUnit){
                $productsData[$k]['combination'][] = array(
                    'combinationid' => $productUnit->getManufacturerProductUnitId(),
                    'sku'           => $productUnit->getSku(),
                    'originalPrice' => $productUnit->getPrice(),
                    'finalPrice'    => $productUnit->getDiscountedPrice(),
                    'discount'      => $productUnit->getDiscountPercentage(),
                    'inventory'     => $productUnit->getQuantity(),
                    'weight'        => $productUnit->getWeight(),
                    'height'        => $productUnit->getHeight(),
                    'length'        => $productUnit->getLength(),
                    'width'         => $productUnit->getWidth(),
                    'attributes'    => $productUnit->getCombinationString(),
                    'moq'           => $productUnit->getMoq(),
                    'name'          => '',
                );
            }
            $productsData[$k]['name'] = $manufacturerProduct->getName();
            $productsData[$k]['referenceno'] = $manufacturerProduct->getReferenceNumber();
            $productsData[$k]['description'] = $manufacturerProduct->getDescription();
            $productsData[$k]['datecreated'] = $manufacturerProduct->getDateAdded();

        }

        if(count($productsData) > 0){
            $response['message'] = "API Endpoint is currently unavailable";
            try{
                $request = new FormRequest(
                    FormRequest::METHOD_POST, '/'.$this->configuration['routes']['create_products'], $this->configuration['baseurl']
                );
                $request->setFields($productsData);
                $buzzResponse = new Response();
                $client = new Curl();
                $client->setTimeout(self::API_TIMEOUT_SEC);
                $client->send($request, $buzzResponse);

                if ($buzzResponse->isSuccessful()) {
                    $content = json_decode($buzzResponse->getContent(), true);
                    $response['isSuccessful'] = true;
                    $response['message'] = "Product created";
                    $response['data'] = $content;
                }
            }
            catch(RequestException $e){
                $response['message'] = "API endpoint request has timeout";
            }
        }

        return $response;
    }

先写值较复杂的,再元素整合在一个数组中。元素很多,放在数组中操作比较好阅读。


你可能感兴趣的:(数组的一些事)