首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从硬数组中提取PHP

从硬数组中提取PHP
EN

Stack Overflow用户
提问于 2022-04-17 23:52:57
回答 2查看 58关注 0票数 -1

如何通过PHP从这个数组中提取所有元素?

我试着用array_slice,但没什么用

我的主要问题是,我需要得到所有的意义,而不仅仅是最后的意义。因此,它将在不同的编码下包含在“产品”中--尽管数组的键是相同的。

我不知道怎么做,请帮帮忙,已经把我的头弄断了

代码语言:javascript
复制
  "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
      }]
       }]

我想谈谈这件事

符号: CP标签:禁止,除非在规定的条件下 代码: 290377 报告代码: ex 2903 77\u00a0600 0 代码: 290379 报告代码: ex 2903 79\u00a0300 0 文号: NAL-X 标签:非自动授权 代码: 290377 报告代码: ex 2903 77\u00a0600 0 代码: 290379 报告代码: ex 2903 79\u00a0300 0

此外,我在一个变量中通过返回来返回所有这些

我试着这样写

代码语言:javascript
复制
$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'];
}}

一开始,我有这样一个密码

代码语言:javascript
复制
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'].'';
}}

但是在这个版本中,它输出循环的最后一个元素(因为每次它被覆盖),并且它也只从第一个忽略第二个数组的数组输出数据。

我的全部实际代码:

代码语言:javascript
复制
...

$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>';

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-18 00:59:10

代码语言:javascript
复制
  $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;

将产出:

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2022-04-18 00:44:41

您可以使用ob_start()ob_get_clean()函数。

这样,您就不必将HTML分配给变量,而是可以输出输出缓冲区,然后在一次返回中获得整个缓冲区。

您的函数应该如下所示( 你可以在这里看到它是如何工作的 ):

代码语言:javascript
复制
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();  
}

对于这段代码,还有其他一些事情可以做得更好,但这不在这个问题的范围之内。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71906343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档