dongping8572
2019-05-09 17:02
浏览 188
已采纳

两个JSON数据合并和ECHO与PHP

What is the difference from the previous questions?

In other questions, the numbers of both JSON data were equal. This question does not equal JSON files.

I have two JSON data. One of them contains only 2 values. The other one contains six values. I'm trying to distinguish with IF ELSE, but two of each value is written. In line with the numbers in the first JSON, I want to separate the second JSON data.

For example:

3 -> val3
5->  val5

First JSON:

{"0":"3","1":"5"}

Second JSON:

{"val1":"ValueOne","val2":"ValueSecond","val3":"ValueThree","val4":"4","val5":"ValueFive","val6":"ValueSix"}

$first = json_decode($jsonFile1);
$second = json_decode($jsonFile2);

foreach ($first as $key => $firstvalue) {
  foreach ($second as $secondvalue) {
    if (substr($firstvalue, -1) == $secondvalue) {  <-- 'valX' => 1
       echo "<strong>". $firstvalue . "</strong><br>";
    } else { 
       echo "<em>". $firstvalue . "</em>";
    } 
  }
}

Result:


  • ValueOne
  • ValueOne
  • ValueSecond
  • ValueSecond
  • ValueThree
  • ValueThree
  • ValueFour
  • ValueFour
  • ValueFive
  • ValueFive
  • ValueSix
  • ValueSix

What could be the reason?

图片转代码服务由CSDN问答提供 功能建议

与之前的问题有什么不同?

在其他问题中,两个JSON数据的数量相等。 这个问题不等于JSON文件。

我有两个JSON数据。 其中一个只包含2个值。 另一个包含六个值。 我试图区分IF ELSE,但是每个值都写了两个。 。与第一个JSON中的数字一致,我想分离第二个JSON数据。

< em>例如:

  3  - &gt;  VAL3 \ N5-&GT;  val5 
   
 
 

第一个JSON:

{“0”:“3” ,“1”:“5”}

第二个JSON:

{“ VAL1 “:” ValueOne”, “val2的”: “ValueSecond”, “VAL3”: “ValueThree”, “VAL4”: “4”, “val5”: “ValueFive”, “VAL6”: “ValueSix”}

  $ first = json_decode($ jsonFile1); 
 $ second = json_decode($ jsonFile2); 
 
foreach($ first as $ key =&gt; $ firstvalue  ){
 foreach($ second as $ secondvalue){
 if(substr($ firstvalue,-1)== $ secondvalue){&lt;  - 'valX'=&gt;  1 
 echo“&lt; strong&gt;”。  $ firstvalue。  “&lt; / strong&gt;&lt; br&gt;”; 
}其他{
 echo“&lt; em&gt;”。  $ firstvalue。  “&lt; / em&gt;”; 
} 
} 
} 
   
 
 

结果:


\ ñ
  • ValueOne
  • ValueOne
  • ValueSecond
  • ValueSecond
  • ValueThree
  • ValueThree
  • ValueFour
  • ValueFour
  • ValueFive < /强>
  • ValueFive
  • ValueSix
  • ValueSix

    可能是什么原因?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongrang2186 2019-05-09 17:16
    已采纳

    Not sure how you get the output you say you are, but the repeat is due to looping each item for every item in the second array - hence each option is in there twice.

    This version uses in_array() to see if the last character of the key is in the second JSON list (note that this is converted to an array and not objects using true as the second parameter of json_decode())...

    $jsonFile1 = '{"val1":"ValueOne","val2":"ValueSecond","val3":"ValueThree","val4":"4","val5":"ValueFive","val6":"ValueSix"}';
    $jsonFile2 = '{"0":"3","1":"5"}';
    $first = json_decode($jsonFile1);
    $second = json_decode($jsonFile2, true);
    
    foreach ($first as $key => $firstvalue) {
        if (in_array(substr($key, -1),$second)) {
            echo "<strong>". $firstvalue . "</strong><br>";
        } else {
            echo "<em>". $firstvalue . "</em><br>";
        }
    }
    

    gives...

    ValueOne
    ValueSecond
    ValueThree
    4
    ValueFive
    ValueSix

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题