How can I get multidimensional arrays value in PHP by using foreach loop?

I have tired to get each product’s title and product_small_image_urls by using foreach loop. But my code doesn’t work. Can you give me better suggestions. Here is my PHP code.

<?php 
.
.
.
$resp = $c->execute($req);
//var_dump($c->execute($req));


$respa = json_encode($resp, true);

//output
{
  "resp_result": {
    "resp_code": "200",
    "resp_msg": "Call succeeds",
    "result": {
      "current_page_no": "1",
      "current_record_count": "50",
      "products": {
        "product": [
          {
            "app_sale_price": "2.48",
            "app_sale_price_currency": "USD",
            "commission_rate": "7.0%",
            "discount": "40%",
            "first_level_category_id": "36",
            "first_level_category_name": "Jewelry & Accessories",
            "hot_product_commission_rate": "60.0%",
            "lastest_volume": "132",
            "original_price": "4.14",
            "original_price_currency": "USD",
            "product_detail_url": "https://www.exmalple.com/item/1005004119364480.html?pdp_npi=1%40dis%7CUSD%7C4.14%7C2.48%7C%7C%7C%7C%7C%40214fb80a16513074089871189e%7C12000028100170819%7Caffd",
            "product_id": "1005004119364480",
            "product_main_image_url": "https://exmalple/kf/S62b022abc914408b901764d955ac22deo.jpg",
            "product_small_image_urls": {
              "string": [
                "https://exmalple/kf/S62b022abc914408b901764d955ac22deo.jpg",
                "https://exmalple/kf/Sdf710fc0d19d4769ba6546c19831bc5cu.jpg",
                "https://exmalple/kf/S50116870a99f44bd8a5e976a5206338fx.jpg"
              ]
            },
            "product_title": "Vintage Women's Exquisite 925 Silver filling Hook Dangle Bird Earrings Engagement Wedding Birthday Anniversary Gift",
            "promotion_link": "https://exmalple/s/OnU3FS57cvSTUbHDIXP1rj4FXerTkM7osVL2mN0niVLHU2HRNYyWfvqTIy8sKNuiW39cGBW7rsoe4NhJx1gFO1N0AlMAT3lUHPyQmWNmzPpDcEquvBBwmohLJsCnJGhkO1HVv05wn9XfL2Km0JGmUSvQh0fiU8R3hz0cLrnsnsnVrjnrki9eNHhSEGUNdDi8UJHs8QPEDQR1Dnw8kP3RJTMVMaDBTB7vBAVHSzT7yxxA8e0poSUaEuw22INqjAj0PmeLYfdZjVzStMyapXufRdpKwi25OIIbJsYodXdxsKE4R83gYg0DZeJwKTXwvOYEX4RwxyyNDsE2GIDZuuFM1ytyn6Cwg3TmpsT4aiNJWuHO3sFlUr2iZc6Uq2FgX6ySK4hFlNwR5UIAnOByGNzgt5ewAfOyxHAX1adaoOT3GBwcRY8WKbsT2mOJTwFTvCHJCOmnBCIuJhqYn61BBG5v0jGGaKYNO5iRp9Wq7z2OAdHDpOtqAtfPakiojmyXH9s14t8tM2HX78ydIqbghtKNAqGJ3UAOSuR5zONXgud8wPErxhpfxhsoPXUccuZr96nc4mL6k23iMK1ebKZXf430iDCCZBszZTC903Zczg4qBqYPue5jh4uh6952XpKvlz28pF5v8MKY02wt9WB62IN4nEy2njFx8GAMwSRleXxXkkkHr0gyLNo0npw1eXaWasPkRUO2qlnNr6tTtMcKN3sXI69NSq71iZcxU6hxjL8RkSc2onoBlDrJMyFbbKGDwlvRKEX3mSGjXwsCCR251Q2ICYAblvBykb9AbI46HLJYH9CU3ig99opUFfRL8jGy7lrfDzwRQ19hp4HXZ5uwij951pxxOkddGWge4V7je1FG61Z74tJ6ZQWzijl3AAjjnpRxBFoLUBjXYehTXhrXFboAp2b",
            "relevant_market_commission_rate": "5.0%",
            "sale_price": "2.48",
            "sale_price_currency": "USD",
            "second_level_category_id": "1509",
            "second_level_category_name": "Fashion Jewelry",
            "shop_id": "1100256040",
            "shop_url": "https://www.exmalple.com/store/1100256040",
            "target_app_sale_price": "2.48",
            "target_app_sale_price_currency": "USD",
            "target_original_price": "4.14",
            "target_original_price_currency": "USD",
            "target_sale_price": "2.48",
            "target_sale_price_currency": "USD"
          },
          {
            "app_sale_price": "0.01",
            "app_sale_price_currency": "USD",
            "commission_rate": "7.0%",
            "discount": "99%",
            "evaluate_rate": "95.1%",
            "first_level_category_id": "36",
            "first_level_category_name": "Jewelry & Accessories",
            "hot_product_commission_rate": "8.0%",
            "lastest_volume": "3511",
            "original_price": "1.61",
            "original_price_currency": "USD",
            "product_detail_url": "https://www.exmalple.com/item/4001150137347.html?pdp_npi=1%40dis%7CUSD%7C1.61%7C0.01%7C%7C%7C%7C%7C%40214fb80a16513074089871189e%7C12000016726258712%7Caffd",
            "product_id": "4001150137347",
            "product_main_image_url": "https://exmalple/kf/H6b34718d41b1410ebd131962c39b9ac4a.jpg",
            "product_small_image_urls": {
              "string": [
                "https://exmalple/kf/H6b34718d41b1410ebd131962c39b9ac4a.jpg",
                "https://exmalple/kf/H4eae1f99ce204e3dbd508b7c3662c1b2q.jpg",
                "https://exmalple/kf/H1416f03a1c0c438d91cfc464890f7a15z.jpg",
                "https://exmalple/kf/H0763410525df4f6ca63ae06225d721b1m.jpg",
                "https://exmalple/kf/H2314472ba5f1414cb813ec3e77ba8bb21.jpg",
                "https://exmalple/kf/H5b8591578f82477aa94bb1f67183461b9.jpg"
              ]
            },
            "product_title": "Professional Ear Piercing Gun Tool Set 98pcs Ear Studs Steel Ear Nose Navel Body Piercing Gun Unit Tool Kit Safety Pierce Tool",
            "promotion_link": "https://exmalple/s/OnU3FS57cvSTUbHDIXP1rj4FXerTkM7osVL2mN0niVLHU2HRNYyWfvqTIy8sKNuiW39cGBW7rsoe4NhJx1gFO1N0AlMAT3lUHPyQmWNmzPpDcEquvBBwmohLJsCnJGhkO1HVv05wn9XfL2Km0JGmUSvQh0fiU8R3hz0cLrnsnsnVrjnrki9eNHhSEGUNdDi8UJHs8QPEDQR1Dnw8kP3RJTMVMaDBTB7vBAVHSzT7yxxA8e0poSUaEuw22INqjAj0PmeLYfdZjVzStMyapXufRdpKwi25OIIbJsYodXdxsKE4R83gYg0DZeJwKTXJx8qQQYHlpDAp6VpDSXqDmJqMSewA8iNMMt6Y7v2P9twsiUFNG78APQfKnqMFaqeX9YV2XdQs1kDNMTmKDlhIQlgOtgDJwiTnukV5pRomrkHhKY0SeXID2NxC0hhxtkX6yP1LLnmza5h5Ow2gklLxxLX6TMTmoteJ8TK5QtIewHKAIOb3iXs4YJjvhbmtfKdPEbUTHxbd1fJqxlwtQyf4MkVLPH4JlFMnXXsqCpoNPK3mcj1yU8REfoWCgk0t8zyMYCPZfFzOCtjkFq72foRCkmSCX7i4sYvruWhQ3GphDwvG5eLbV8MWAbjWl4mo7NCB3XGikNBYeZXCo3LS3NPf1T8v5BKYDaWSKoOP9uPXGng1DJrEzycQHwmsnWaZp1NajIjREIe2hDCoXE1UOYZ9uekX42oV7rFyKWHXCw0uYkwR6XmnjqnNUIlHUkAm53XOoMQ2vEgQtDy232K11h5dEI0GjYAdrXkdTDOMDJpKoMFjxrtH25QMMYSAQZKcUx3iSaAY9FvYZe8cfavsQPEpL7yhzbHytvjYixoW4nwA3FZfz41LB0HarIsz0HrLXxH5Ag6HDdf9q35ewSKzlLMkWK8PgRD",
            "relevant_market_commission_rate": "4.0%",
            "sale_price": "0.01",
            "sale_price_currency": "USD",
            "second_level_category_id": "1509",
            "second_level_category_name": "Fashion Jewelry",
            "shop_id": "1045551",
            "shop_url": "https://www.exmalple.com/store/1045551",
            "target_app_sale_price": "0.01",
            "target_app_sale_price_currency": "USD",
            "target_original_price": "1.61",
            "target_original_price_currency": "USD",
            "target_sale_price": "0.01",
            "target_sale_price_currency": "USD"
          }
        ]
          
      },
      "total_record_count": "2103334"
    }
  },
  "request_id": "16kd4llernra9"
}


foreach ($respa['resp_result']['result']['products']['product'] as $item) { 
    echo $item['product_title']['product_small_image_urls']; 
    
}?>

I have tired to get each product’s title and product_small_image_urls by using foreach loop. But my code doesn’t work. Can you give me better suggestions.

Leave a Comment