Home > Mobile >  PHP extracting from hard array
PHP extracting from hard array

Time:04-18

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 77\u00a0600 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 79\u00a0300 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 77\u00a0600 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 79\u00a0300 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 77\u00a0600 0

Code: 290379

Reported Code: ex 2903 79\u00a0300 0

Symbol: NAL-X

Label: Non-automatic licensing

Code: 290377

Reported Code: ex 2903 77\u00a0600 0

Code: 290379

Reported Code: ex 2903 79\u00a0300 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

CodePudding user response:

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.

CodePudding user response:

  $data = '{"measures": [{
        "symbol": "CP",
        "label": "Prohibition except under defined conditions",
        "products": [{
          "hs_version": "HS-17",
          "chapter": "29",
          "code": "290377",
          "reported_code": "ex 2903 77\u00a0600 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 79\u00a0300 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 77\u00a0600 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 79\u00a0300 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
  • Related