-1

Having a little issue with getting the barcode and sku from this example array

{
  "title": "This is the title",
  "variants": [{
    "barcode": "123456789", 
    "sku": "sku1"
  }]
}

PHP

foreach ($products as $product) {

     $products[] = Array(
        "title" => $product["title"], // product title
        "barcode" => $product["variants"]["barcode"], // product barcode
        "sku" => $product["variants"]["sku"] // product SKU
     );
  }
James
  • 1,668
  • 19
  • 50
  • Have you decoded your json properly? – Rizier123 Aug 31 '16 at 12:41
  • Duplicate of [How do I extract data from JSON with PHP?](http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – Bartosz Zasada Aug 31 '16 at 12:42
  • Yea its all valid, thats just an example data i just wrote on here as far to much data to paste but need to get the sub data of the array – James Aug 31 '16 at 12:42
  • 1
    You're assigning the new values to the same `$products` array. I am almost sure it was not intentional – CarlosCarucce Aug 31 '16 at 12:43
  • Well first on turn on error reporting and check if you get some errors or warnings: `ini_set("display_errors", 1); error_reporting(E_ALL);` Also take a look at the structure of your array with `print_r($products);` to see what you got in there. Right now you just show us an array, where you would loop over `title` and `variants`. – Rizier123 Aug 31 '16 at 12:44

3 Answers3

5

Try this hope this should work:

foreach ($products as $product) {

     $products[] = Array(
        "title" => $product["title"], // product title
        "barcode" => $product["variants"][0]["barcode"], // product barcode
        "sku" => $product["variants"][0]["sku"] // product SKU
     );
  }
sietse85
  • 1,488
  • 1
  • 10
  • 26
er.irfankhan11
  • 1,280
  • 2
  • 16
  • 29
  • Yep that done the trick, i was going to try [0] but thought would just do the first but it does loop as expected thanks mate – James Aug 31 '16 at 12:44
  • I went to accept but could not for 10 minutes but just have done that :) – James Aug 31 '16 at 23:59
3

Your data looks like JSON, so you'll need to convert it to a PHP array first:

$products = json_decode($data, true);

Now you can iterate like this:

$product_array = array();
foreach ($products as $product) {

    $product_array[] = array(
       "title" => $product["title"], // product title
       "barcode" => $product["variants"][0]["barcode"], // product barcode
       "sku" => $product["variants"][0]["sku"] // product SKU
    );
}

Notice the added [0] in the foreach loop, as well as the $product_array array (because you were overwriting the $products array in the loop itself). The [0] is to get the first element of the array variants, as there can be more. You could add another foreach loop to iterate them all.

Also note that you already have a complete array after using json_decode. You can check that by just running:

var_dump(json_decode($data, true));

Where $data contains the JSON data you mentioned.

Updated answer with loop inside loop:

$product_array = array();

foreach ($products as $product) {
    // I'm using the name $item, if I used $product it would overwrite the variable in the loop
    $item = array(
        "title" => $product["title"], // product title
        "variants" => array() // for now an empty array, we will fill it in the next step
    );

    // now we fill the empty arrays inside the array above, by iterating all variants
    foreach($product["variants"] as $variant) {
        $item["variants"][] = array(
            "barcode" => $variant["barcode"], // product barcode
            "sku" => $variant["sku"] // product SKU
        );
    }
    $product_array[] = $item; // now we add the item with all the variants to the array with all the products
}

var_dump($product_array); // show the results
Tum
  • 6,937
  • 2
  • 25
  • 23
  • Im a little new to PHP, could you maybe give me an example so did not have to use [0] and using another loop? although that did work and displayed my data fine... if there is a better way would like to learn the better way of course :) and i already json the data from PHP framework i think... well i assume as when loaded page was loading all fine. – James Sep 01 '16 at 00:02
  • Sure thing, added it to my answer! – Tum Sep 01 '16 at 08:30
0

try this code

$a = '{
"title": "This is the title",
"variants": [{
  "barcode": "123456789", 
  "sku": "sku1"
 }]
}';
$products = json_decode($a, TRUE);
$products = Array(
   "title" => $products['title'], // product title
   "barcode" => $products['variants'][0]["barcode"], // product barcode
   "sku" => $products['variants'][0]["sku"] // product SKU
);
Rakesh Sojitra
  • 3,538
  • 2
  • 17
  • 34