skip to Main Content

How can I extract all elements from this array through PHP?

I tried to do with array_slice but it didn’t help

My main problem is that I need to derive all the meanings, not just the last ones. So that it would be under different encodings that are contained in [‘product’] – despite the fact that the keys for the arrays are the same

I don’t know how to do it, please help, already broke my head

  "measures": [{
      "symbol": "CP",
      "label": "Prohibition except under defined conditions",
      "products": [{
        "hs_version": "HS-17",
        "chapter": "29",
        "code": "290377",
        "reported_code": "ex 2903 77u00a0600 0",
        "description": "Halogenated derivatives of acyclic hydrocarbons containing two or more different halogens, perhalogenated only with fluorine and chlorine (excl. chlorodifluoromethane, dichlorotrifluoroethanes, dichlorofluoroethanes, chlorodifluoroethanes, dichloropentafluoropropanes, bromochlorodifluoromethane, bromotrifluoromethane and dibromotetrafluoroethanes)",
        "reported_description": "CFCl3 (CFC-11) Trichlorofluoromethane",
        "interpreted": 0
      }, {
        "hs_version": "HS-17",
        "chapter": "29",
        "code": "290379",
        "reported_code": "ex 2903 79u00a0300 0",
        "description": "Halogenated derivatives of acyclic hydrocarbons containing two or more different halogens (excl. perhalogenated, and chlorodifluoromethane, dichlorotrifluoroethanes, dichlorofluoroethanes, chlorodifluoroethanes, dichloropentafluoropropanes, bromochlorodifluoromethane, bromotrifluoromethane and dibromotetrafluoroethanes)",
        "reported_description": "C3H6FCl (HCFC-271) Chlorofluoropropane",
        "interpreted": 0
      }]
    }, {
      "symbol": "NAL-X",
      "label": "Non-automatic licensing",
      "products": [{
        "hs_version": "HS-17",
        "chapter": "29",
        "code": "290377",
        "reported_code": "ex 2903 77u00a0600 0",
        "description": "Halogenated derivatives of acyclic hydrocarbons containing two or more different halogens, perhalogenated only with fluorine and chlorine (excl. chlorodifluoromethane, dichlorotrifluoroethanes, dichlorofluoroethanes, chlorodifluoroethanes, dichloropentafluoropropanes, bromochlorodifluoromethane, bromotrifluoromethane and dibromotetrafluoroethanes)",
        "reported_description": "CFCl3 (CFC-11) Trichlorofluoromethane",
        "interpreted": 0
      }, {
        "hs_version": "HS-17",
        "chapter": "29",
        "code": "290379",
        "reported_code": "ex 2903 79u00a0300 0",
        "description": "Halogenated derivatives of acyclic hydrocarbons containing two or more different halogens (excl. perhalogenated, and chlorodifluoromethane, dichlorotrifluoroethanes, dichlorofluoroethanes, chlorodifluoroethanes, dichloropentafluoropropanes, bromochlorodifluoromethane, bromotrifluoromethane and dibromotetrafluoroethanes)",
        "reported_description": "C3H6FCl (HCFC-271) Chlorofluoropropane",
        "interpreted": 0
      }]
       }]

I would like to get about this

Symbol: CP

Label: Prohibition except under defined conditions

Code: 290377

Reported Code: ex 2903 77u00a0600 0

Code: 290379

Reported Code: ex 2903 79u00a0300 0

Symbol: NAL-X

Label: Non-automatic licensing

Code: 290377

Reported Code: ex 2903 77u00a0600 0

Code: 290379

Reported Code: ex 2903 79u00a0300 0

moreover, I return all this through return in one variable

i tried writing like this

$rbn = array_slice($dfs2['measures'], 1);
foreach ($rbn as $dfs4){ 
$jko4 = '<span style="color:#fff;">Label:</span> '.$dfs4['label'].'<br><span style="color:#fff;">Symbol:</span> '.$dfs4['symbol'].'';
foreach ($dfs4['products'] as $dfs5){
$jko3 = $dfs5['reported_description'];
}}

Initially, I had such a code

foreach ($dfs2['measures'] as $dfs4){ 
$jko4 = '<span style="color:#fff;">Label:</span> '.$dfs4['label'].'<br><span style="color:#fff;">Symbol:</span> '.$dfs4['symbol'].'';
foreach ($dfs4['products'] as $dfs5){
$jko3 = '<span style="color:#fff;">HS Version:</span> '.$dfs5['hs_version'].'<br><span style="color:#fff;">Code:</span> '.$dfs5['code'].'<br><span style="color:#fff;">Description:</span> '.$dfs5['description'].'<br><span style="color:#fff;">Reported Description:</span> '.$dfs5['reported_description'].'';
}}

But in this version, it output the last element of the cycle (because each time it was overwritten), and it also output data only from the first array ignoring the second

My Full Actual Code:

...

$zur = [];
foreach ($handles as $channel) {
$html = curl_multi_getcontent($channel);
$zur[] = json_decode($html, true, 999, JSON_THROW_ON_ERROR);
curl_multi_remove_handle ($multi, $channel);
}

curl_multi_close ($multi);

$dfs = [];
foreach ($zur as $zur2){
foreach ($zur2 as $zur3){
$dfs[] = $zur3;
}   
}
foreach ($dfs as $dfs2){
if ($dfs2['administrative_mechanisms'][0]):
$rop = '<h4>Administrative Mechanisms</h4>'.$dfs2['administrative_mechanisms'].'';  
endif;  
foreach ($dfs2['wto_agreements'] as $dfs3):
$jko2 = '<span style="color:#fff;">Agreement Label:</span> '.$dfs3['agreement_label'].'</span><br><span style="color:#fff;">Article Code:</span> <a href="'.$dfs3['agreement_web_link'].'" target="_blank" style="color:#00aeff">'.$dfs3['article_code'].'</a></span><br><span style="color:#fff;">Article Index:</span> '.$dfs3['article_index'].'</span><br><span style="color:#fff;">Article Description:</span> '.$dfs3['article_description'].'</span></dd>';
endforeach;
foreach ($dfs2['measures'] as $dfs4){ 
$jko4 = '<span style="color:#fff;">Label:</span> '.$dfs4['label'].'<br><span style="color:#fff;">Symbol:</span> '.$dfs4['symbol'].'';
foreach ($dfs4['products'] as $dfs5){
$jko3 = '<span style="color:#fff;">HS Version:</span> '.$dfs5['hs_version'].'<br><span style="color:#fff;">Code:</span> '.$dfs5['code'].'<br><span style="color:#fff;">Description:</span> '.$dfs5['description'].'<br><span style="color:#fff;">Reported Description:</span> '.$dfs5['reported_description'].'';
}}
$jkk .= '<dt><span>'.$dfs2['description'].'</span><div class="acc-icon-wrap parallax-wrap"><div class="acc-button-icon parallax-element"><i class="fa fa-angle-down"></i></div></div></dt><dd class="accordion-content"><h4>Document Symbol</h4>'.$dfs2['document_symbol'].'<h4>National Legal Bases</h4>'.$dfs2['national_legal_bases'].''.$rop.'<h4>Relevance</h4><span style="color:#fff;">Date From:</span> '.$dfs2['period_from_dt'].' <span style="color:#fff;">Date To:</span> '.$dfs2['period_to_dt'].' <span style="color:#fff;">In Force</span> '.$dfs2['in_force_dt'].'<h4>Measures</h4>'.$jko4.'<h4>Products</h4>'.$jko3.'<h4>WTO Agreements</h4>'.$jko2.'';
}
return '<dl class="accordion has-animation">'.$jkk.'</dl>';

2

Answers


  1. You may use the ob_start() and ob_get_clean() functions.

    That way, you don’t have to assign the HTML to variables, but you can output it the output buffer and then get the whole buffer in one return.

    Your function would look something like this ( You can see how it works here ):

    function get_measures( $data ) {
        ob_start();
        
        foreach ($data['measures'] as $dfs4){ 
            echo '<div><span style="color:#fff;">Symbol:</span> '.$dfs4['symbol'].'</div>';
            echo '<div><span style="color:#fff;">Label:</span> '.$dfs4['label'].'</div>';
            
            foreach ($dfs4['products'] as $dfs5){
                echo '<br />';
                echo '<div><span style="color:#fff;">Code:</span> '.$dfs5['code'].'</div>';
                echo '<div><span style="color:#fff;">Reported Code:</span> '.$dfs5['reported_code'].'</div>';
            }
            
            echo '<br /><br />';
        }
        
        return ob_get_clean();  
    }
    

    There are some other things that can be done to this code to be better, but it’s off the scope of this question.

    Login or Signup to reply.
  2.   $data = '{"measures": [{
            "symbol": "CP",
            "label": "Prohibition except under defined conditions",
            "products": [{
              "hs_version": "HS-17",
              "chapter": "29",
              "code": "290377",
              "reported_code": "ex 2903 77u00a0600 0",
              "description": "Halogenated derivatives of acyclic hydrocarbons containing two or more different halogens, perhalogenated only with fluorine and chlorine (excl. chlorodifluoromethane, dichlorotrifluoroethanes, dichlorofluoroethanes, chlorodifluoroethanes, dichloropentafluoropropanes, bromochlorodifluoromethane, bromotrifluoromethane and dibromotetrafluoroethanes)",
              "reported_description": "CFCl3 (CFC-11) Trichlorofluoromethane",
              "interpreted": 0
            }, {
              "hs_version": "HS-17",
              "chapter": "29",
              "code": "290379",
              "reported_code": "ex 2903 79u00a0300 0",
              "description": "Halogenated derivatives of acyclic hydrocarbons containing two or more different halogens (excl. perhalogenated, and chlorodifluoromethane, dichlorotrifluoroethanes, dichlorofluoroethanes, chlorodifluoroethanes, dichloropentafluoropropanes, bromochlorodifluoromethane, bromotrifluoromethane and dibromotetrafluoroethanes)",
              "reported_description": "C3H6FCl (HCFC-271) Chlorofluoropropane",
              "interpreted": 0
            }]
          }, {
            "symbol": "NAL-X",
            "label": "Non-automatic licensing",
            "products": [{
              "hs_version": "HS-17",
              "chapter": "29",
              "code": "290377",
              "reported_code": "ex 2903 77u00a0600 0",
              "description": "Halogenated derivatives of acyclic hydrocarbons containing two or more different halogens, perhalogenated only with fluorine and chlorine (excl. chlorodifluoromethane, dichlorotrifluoroethanes, dichlorofluoroethanes, chlorodifluoroethanes, dichloropentafluoropropanes, bromochlorodifluoromethane, bromotrifluoromethane and dibromotetrafluoroethanes)",
              "reported_description": "CFCl3 (CFC-11) Trichlorofluoromethane",
              "interpreted": 0
            }, {
              "hs_version": "HS-17",
              "chapter": "29",
              "code": "290379",
              "reported_code": "ex 2903 79u00a0300 0",
              "description": "Halogenated derivatives of acyclic hydrocarbons containing two or more different halogens (excl. perhalogenated, and chlorodifluoromethane, dichlorotrifluoroethanes, dichlorofluoroethanes, chlorodifluoroethanes, dichloropentafluoropropanes, bromochlorodifluoromethane, bromotrifluoromethane and dibromotetrafluoroethanes)",
              "reported_description": "C3H6FCl (HCFC-271) Chlorofluoropropane",
              "interpreted": 0
            }]
      }]}';
           
      $array = json_decode($data, true);
      
      $output = '';
      
      foreach ($array['measures'] as $measure) {
        $symbol = $measure['symbol'];
        $label = $measure['label'];
        
        $output .= '<p>';
        
          $output .= 'Symbol: '.$symbol.'<br/>';
          $output .= 'Label: '.$label.'<br/>';
    
            foreach ($measure['products'] as $product) {
              $code = $product['code'];
              $reported_code = $product['reported_code'];
    
              $output .= 'Code: '.$code.'<br/>';
              $output .= 'Reported Code: '.$reported_code.'<br/>';
            }
    
        $output .= '</p>';
      }
      
      echo $output;
    

    Will output :

    Symbol: CP
    Label: Prohibition except under defined conditions
    Code: 290377
    Reported Code: ex 2903 77Â 600 0
    Code: 290379
    Reported Code: ex 2903 79Â 300 0
    
    Symbol: NAL-X
    Label: Non-automatic licensing
    Code: 290377
    Reported Code: ex 2903 77Â 600 0
    Code: 290379
    Reported Code: ex 2903 79Â 300 0
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search