[ create a new paste ] login | about

Link: http://codepad.org/WyMnITjY    [ raw code | output | fork ]

PHP, pasted on Apr 8:
<?php
$a = '{"product": [{
    "id" : "001",
    "title":"Product One",
    "price":"Price One",
    "desc":"Description One",
    "image": [{
        "img":"image 1 1"
    },{
        "img":"image 1 2"
    },{
        "img":"image 1 3"
    }]
}, {
"id" : "002",
    "title":"Product Two",
    "price":"Price Two",
    "desc":"Description Two",
    "image": [{
        "img":"image 2 1"
    },{
        "img":"image 2 2"
    },{
        "img":"image 2 3"
    }]
}]
}';
$product = json_decode($a, true);


function getArrValue($key, &$product) {
    	foreach ($product["product"] as $product) {
    		if (!empty($product["id"])) {
	    		return $product;
	    	}
    	}
    }

var_dump(getArrValue("002", $product));


Output:
array(5) {
  ["id"]=>
  string(3) "001"
  ["title"]=>
  string(11) "Product One"
  ["price"]=>
  string(9) "Price One"
  ["desc"]=>
  string(15) "Description One"
  ["image"]=>
  array(3) {
    [0]=>
    array(1) {
      ["img"]=>
      string(9) "image 1 1"
    }
    [1]=>
    array(1) {
      ["img"]=>
      string(9) "image 1 2"
    }
    [2]=>
    array(1) {
      ["img"]=>
      string(9) "image 1 3"
    }
  }
}


Create a new paste based on this one


Comments: