1

I am getting an array of specs from flipkart's API.

I am able to get this response from that API:

{
    "nextUrl": "https://affiliate-api.flipkart.net/1.0/affiliate/feeds/fnkygygma/category/reh/55ab6c2673a4773ffb8e4019.json?expiresAt=1452881213871&sig=1c4c5111b6b014a71a17b229e6df6afc",
    "validTill": 1452881213871,
    "productInfoList": [
    {
        "productBaseInfoV1": {
        "productId": "TDHDMH5GRSPZ3DNM",
        "title": "Newhide Designer",
        "productDescription": "",
        "imageUrls": {
            "400x400": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-400x400-imadmh8zgxbrsgq6.jpeg",
            "200x200": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-200x200-imadmh8zgxbrsgq6.jpeg",
            "unknown": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-original-imadmh8zgxbrsgq6.jpeg",
            "800x800": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-800x800-imadmh8zgxbrsgq6.jpeg"
        },
        "productFamily": null,
        "maximumRetailPrice": {
            "amount": 1145,
            "currency": "INR"
        },
        "flipkartSellingPrice": {
            "amount": 1145,
            "currency": "INR"
        },
        "flipkartSpecialPrice": {
            "amount": 1145,
            "currency": "INR"
        },
        "productUrl": "http://dl.flipkart.com/dl/newhide-designer/p/itmdp2nunbzffwzr?pid=TDHDMH5GRSPZ3DNM&affid=keshav",
        "productBrand": "Newhide",
        "inStock": true,
        "codAvailable": true,
        "discountPercentage": 0,
        "offers": [],
        "categoryPath": "[[{\"node_id\":20001,\"node_name\":\"FLIPKART_TREE\"},{\"node_id\":21183,\"node_name\":\"Lifestyle\"},{\"node_id\":21499,\"node_name\":\"Leather \\u0026 Travel Accessories\"},{\"node_id\":21960,\"node_name\":\"Wallets \\u0026 Clutches\"},{\"node_id\":21274,\"node_name\":\"Wallets \\u0026 Card Wallets\"}]]",
        "styleCode": null,
        "attributes": {
            "size": "",
            "color": "Black",
            "storage": "",
            "sizeUnit": "",
            "displaySize": ""
        }
      },
      "productShippingInfoV1": {
          "shippingCharges": {
              "amount": 0,
              "currency": "INR"
          },
          "sellerName": null,
          "sellerAverageRating": null,
          "sellerNoOfRatings": 0,
          "sellerNoOfReviews": 0
      },
      "categorySpecificInfoV1": {
          "keySpecs": [
              "Passport Holder",
              "Passport Wallet"
           ],
           "detailedSpecs": [],
           "specificationList": [
           {
            "key": "General",
            "values": [
              {
                "key": "Type",
                "value": [
                  "Passport Organizer"
                ]
              },
              {
                "key": "Ideal For",
                "value": [
                  "Men's, Women's"
                ]
              },
              {
                "key": "Material",
                "value": [
                  "Leather, Cloth"
                ]
              },
              {
                "key": "Slot for Cards",
                "value": [
                  "Yes"
                ]
              },
              {
                "key": "Slot for Passport",
                "value": [
                  "Yes"
                ]
              },
              {
                "key": "Slot for Cheque Book",
                "value": [
                  "Yes"
                ]
              },
              {
                "key": "Style Code",
                "value": [
                  "CDBP040739"
                ]
              },
              {
                "key": "Color Code",
                "value": [
                  "Black"
                ]
              }
            ]
          },
          {
            "key": "Warranty",
            "values": [
              {
                "key": "  ",
                "value": [
                  "1 Year Newhide warranty"
                ]
              }
            ]
          }
        ],
        "booksInfo": {
          "language": null,
          "binding": null,
          "pages": null,
          "publisher": null,
          "year": 0,
          "authors": []
        },
        "lifeStyleInfo": {
          "sleeve": null,
          "neck": null,
          "idealFor": [
            "Men's",
            "Women's"
          ]
        }
      }
    }
}

I and not able decode

"categorySpecificInfoV1": {
  "keySpecs": [
    "Passport Holder",
    "Passport Wallet"
  ],

I have tried json_decode. This gives error

Warning: json_decode() expects parameter 1 to be string, array given.

 $keySpecs=$product['categorySpecificInfoV1']['keySpecs'];
 $ar = json_decode($keySpecs,true); 

I am new to PHP, how can I print the keyspecs from keyspecs array?

2
  • foreach($product['categorySpecificInfoV1']['keySpecs'] as $spec) { echo $spec; }? (depending on what product is in your example). Commented Jun 11, 2016 at 22:02
  • its showing a random spec and only one item from whole array list Commented Jun 11, 2016 at 23:17

1 Answer 1

1

The warning is pretty clear. You need to pass the entire string you obtained from Flipkart into the json_decode() function.

See Documentation

$ar = json_decode($flipkart_result, true);  
print_r($ar); //to check the array.
print_r($ar['categorySpecificInfoV1']['keySpecs']);

This should do. Post more information if this does not help.

Sign up to request clarification or add additional context in comments.

3 Comments

The warning also clearly indicates it is an array. Doing an echo of an array will produce a notice Array to string conversion.
Oh yea, my bad. Didn't see what was inside keySpecs. Let me edit
if it is again saying that did you simply try to print keySpecs: print_r($keySpecs). Otherwise please provide more information about the product and keySpecs variable. Use var_dump($product) and var_dump($keySpecs) and put their results in your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.