curl Laravel api foreach参数必须是数组类型|对象,给定空值

wtzytmuj  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(167)

我正在从API端获取产品数据。传入数组中有两个嵌套的“数据”。我可以使用foreach列出产品,但我无法访问单个元素。当我尝试使用一些方法时,它返回类似“array,object int”的错误。我哪里出错了?我如何访问它的元素?
我现在尝试的方法是这样的,我把这个过程应用到Laravel这一边。

{
    "data": {
        "data": [
            {
                "id": 101553,
                "company_id": 730,
                "category_id": 132288,
                "brand_id": 39549,
                "name": "Hh PRODUCT TEST",
                "remote_category_id": "scarf",
                "remote_brand_id": "bershka123",
                "spu": "zb3940823904"
            },
            {
                "id": 101554,
                "company_id": 730,
                "category_id": 132289,
                "brand_id": 39549,
                "name": "Hs PRODUCT TEST",
                "remote_category_id": "scarf",
                "remote_brand_id": "bershka123",
                "spu": "zb3940823905"
            }
        ],
        "count": 175,
        "search": {
            "category_id": null,
            "brand_id": null,
            "company_id": null
        },
        "start": 0,
        "length": 10
    },
    "error_code": 0,
    "message": ""
}

产品型号:

public function getProducts($start =0, $length =10, $category_id =null, $brand_id=null, $spu=null, $name=null, $is_active=null, $id=null, $remote_brand_id = null, $remote_category_id=null)
    {
        $request = Http::withHeaders([
            'Authorization' => 'Bearer '.$this->jwttoken,
            'Content-Type' => 'application/json'
        ])->post($this->sandbox . 'products/search',[
            "start" => $start,
            "length" => $length,
            "search" => [
                "category_id" => $category_id,
                "brand_id" => $brand_id,
                "spu" => $spu,
                "name" => $name,
                "is_active" => $is_active,
                "id" => $id,
                "remote_brand_id" => $remote_brand_id,
                "remote_category_id" => $remote_category_id
            ]
        ])->json();
        return $request;
    }

控制器

$company_id = 100;
    $products = (new Client($company_id))->getProducts();
    foreach($products as $product){
        foreach($product as $prd){
            dump($prd);
        }
    }

结果:

0 => array:27 [
    "id" => 101530
    "company_id" => 730
    "category_id" => 132047
    "brand_id" => 39316
    "name" => "Hs Test Shoe"
    "remote_category_id" => "5a39b114f1ff6e42639e9e041cd002d6"
    "remote_brand_id" => "617392d2b51dbba1a2e5561a3e4eefe7"
    "spu" => "845504"
],
1 => array:36 [
    "id" => 231303
    "company_id" => 730
    "product_id" => 101530
    "supplier_id" => null
    "remote_supplier_id" => null
    "name" => "Hd Test Shoe"
    "description" => "This is a test product."
    "sku" => "2K4F9966WHITE42"
]

错误异常:foreach()参数必须是数组类型|对象,给定整型
如何获得正确的循环格式和元素?

j13ufse2

j13ufse21#

你要这样转圈

foreach($products["data"]["data"] as $product)
{
    dump($product);
}

相关问题