Reading JSon PHP

0

I'm starting to develop in PHP , and I'm having a hard time reading the return of a Json ;

This section, I was able to read:

"cost_per_action_type": [ { "action_type": "comment", "value": "261.1" }, { "action_type": "like", "value": "11.868182" }, { "action_type": "offsite_conversion.fb_pixel_add_to_cart", "value": "2.122764" }, { "action_type": "offsite_conversion.fb_pixel_initiate_checkout", "value": "12.433333" }, { "action_type": "offsite_conversion.fb_pixel_lead", "value": "261.1" }, { "action_type": "offsite_conversion.fb_pixel_purchase", "value": "29.011111" }, { "action_type": "offsite_conversion.fb_pixel_search", "value": "0.409248" }, { "action_type": "offsite_conversion.fb_pixel_view_content", "value": "0.124393" }, { "action_type": "post", "value": "261.1" }, { "action_type": "landing_page_view", "value": "0.326375" }, { "action_type": "commerce_event", "value": "18.65" }, { "action_type": "link_click", "value": "0.284733" }, { "action_type": "offsite_conversion", "value": "0.090315" }, { "action_type": "page_engagement", "value": "0.244247" }, { "action_type": "post_engagement", "value": "0.249379" }, { "action_type": "post_reaction", "value": "2.039844" } ], "unique_actions": [ { "action_type": "comment", "value": "1" }, { "action_type": "like", "value": "22" }, { "action_type": "offsite_conversion.fb_pixel_add_to_cart", "value": "41" }, { "action_type": "offsite_conversion.fb_pixel_initiate_checkout", "value": "16" }, { "action_type": "offsite_conversion.fb_pixel_lead", "value": "1" }, { "action_type": "offsite_conversion.fb_pixel_purchase", "value": "7" }, { "action_type": "offsite_conversion.fb_pixel_search", "value": "81" }, { "action_type": "offsite_conversion.fb_pixel_view_content", "value": "469" }, { "action_type": "post", "value": "1" }, { "action_type": "landing_page_view", "value": "467" }, { "action_type": "commerce_event", "value": "8" }, { "action_type": "link_click", "value": "577" }, { "action_type": "offsite_conversion", "value": "487" }, { "action_type": "page_engagement", "value": "643" }, { "action_type": "post_engagement", "value": "635" }, { "action_type": "post_reaction", "value": "88" } ]

I have read through the following foreach :

    foreach($values["data"][$i]["cost_per_action_type"] as $y => $z){
      if($z["action_type"] == "offsite_conversion.fb_pixel_view_content")
        $resultadosCustoPagina = $z["value"];

The excerpt from JSON below, I could not read.

"website_purchase_roas": [ { "action_type": "offsite_conversion.fb_pixel_purchase", "value": "6.479003" } ]

foreach I used to read

    foreach($values["data"][$i]["website_purchase_roas"] as $uRoas => $vRoas){
      if($vRoas["action_type"] == "offsite_conversion.fb_pixel_purchase")
        $resultadosROASNovo = $vRoas["value"];
    }

How does reading change? The error it gives is that it is not finding the index website_purchase_roas. Undefined index: website_purchase_roas .

    
asked by anonymous 18.05.2018 / 14:31

1 answer

1

Hi, if you have a json why not use json_decode . Example:

$json = '{ "website_purchase_roas": [{ "action_type": "offsite_conversion.fb_pixel_purchase", "value": "6.479003" }] }';
$array = json_decode($json, true);
var_dump($array['website_purchase_roas'][0]['value']);
    
18.05.2018 / 14:58