Home > OS >  how to get words after word in string [duplicate]
how to get words after word in string [duplicate]

Time:09-16

I've a json response like this:

'{"success":true,"code":200,"message":"Success","data":[{"title":"title1","productKey":"123","thumbnailUrl":"https://example.com/1.jpg","star":0},{"title":"title2","productKey":"234","thumbnailUrl":"https://example.com/2.jpg","star":0}]}'

above code is in a variable as string

I need just product keys: 123,234;

how can I get product keys from this reponse?

Thanks

CodePudding user response:

Use json_decode to turn it into an associative array.

$jsonString = '{"success":true,"code":200,"message":"Success","data":[{"title":"title1","productKey":"123","thumbnailUrl":"https://example.com/1.jpg","star":0},{"title":"title2","productKey":"234","thumbnailUrl":"https://example.com/2.jpg","star":0}]}';

$json = json_decode($jsonString, true);
$productKeys = [];
foreach($json["data"] as $val)
{
  $productKeys[] = $val["productKey"];
}
  •  Tags:  
  • php
  • Related