Question

I have an input of some products as "id" parameter, information about the purchase, status, and the user who's purchasing as you can see in this input example.

{"id":2694301,"state":3,"timeout":25,"date":"2014-04-27 00:32:37","date_timeout":"2014-04-27 01:00:02","user":"wolf_359","purchase":341.52},{"id":34017,"state":1,"timeout":10,"date":"2014-04-27 01:59:34","date_timeout":"2014-04-27 01:00:02","user":"mik10","purchase":217.90},{"id":12317,"state":2,"timeout":5,"date":"2014-04-27 03:51:31","date_timeout":"2014-04-27 01:00:02","user":"assemblyuser","purchase":1321.17};

What i'm trying is to get only one product, ordered with all the information, knowing the id. That means, for example, if I know that my Id is 34017, i just wanna show this row ignoring the others:

{"id":34017,"state":1,"timeout":10,"date":"2014-04-27 01:59:34","date_timeout":"2014-04-27 01:00:02","user":"mik10","purchase":217.90}

So the reason for what I'm asking help is that I don't know if there's any other possibility getting with substrings and explodes the data cause this looks very very complicated. This is what I've done:

preg_match_all('/{"id":2694017/', $page, $matches, PREG_OFFSET_CAPTURE); 
$firstkey = $matches[0][0][1];
$page = substr($page, $firstkey, 210);
$values = explode('/:,', $page);
$value1 = $values[1];
$value2 = $values[2];
$value3 = $values[3];
$value4 = $values[4];
$value5 = $values[5];
$value6 = $values[6];
$value7 = $values[7];

In the first lane I obtain the position in the string the { is. Then I cut with a substring the register plus a little bit more of the next preventing that the username could be very long. After, due to I know the registers I'm interested are in the next 7 colons, I just explode.

Was it helpful?

Solution

Heh it is too simple using json_decode function; Make sure that you have a correct format of data. In your case you need to wrap your input data with "[ ]" Just see the code:

$inputData = '[
{"id":2694301,"state":3,"timeout":25,"date":"2014-04-27 00:32:37","date_timeout":"2014-04-27 01:00:02","user":"wolf_359","purchase":341.52},
{"id":34017,"state":1,"timeout":10,"date":"2014-04-27 01:59:34","date_timeout":"2014-04-27 01:00:02","user":"mik10","purchase":217.90},
{"id":12317,"state":2,"timeout":5,"date":"2014-04-27 03:51:31","date_timeout":"2014-04-27 01:00:02","user":"assemblyuser","purchase":1321.17}
]';

$products = json_decode($inputData);
$productId = 34917; // product id wich you want to show;

$result = array();
foreach($products as $product) {
  if ($product['id'] == $productId) {
     $result = $product;
     break;
  }
}


var_dump($result); // here you will data for product you want.

OTHER TIPS

As suggested in the comment to your question... please don't use Regex to parse JSON strings and use json_decode instead.

$id = 2694017;
$input_arr = json_decode($input);
$needed_row = null;
foreach($input_arr as $row)
{
  if($row->id == $id)
  {
    $needed_row = $row;
    break;
  }
}

if(isset($needed_row)){ ... }

If you wanna access multiple IDs within your input you might create an array with the IDs being the keys of this additional array in the foreach loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top