程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从 PHP 循环组织 JSON大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决从 PHP 循环组织 JSON?

开发过程中遇到从 PHP 循环组织 JSON的问题如何解决?下面主要结合日常开发的经验,给出你关于从 PHP 循环组织 JSON的解决方法建议,希望对你解决从 PHP 循环组织 JSON有所启发或帮助;

晚安,我有一个 JsON 文件,我需要按票来组织它,从每张票中取出他们的主题,但我无法执行此循环。有人能帮我吗? 我有 25 张这样的票,这里有一个结构示例。

[
  {
    "TicketID": 28890,"categoryID": 57526,"CustomerID": 97979,"Customername": "Cox Workman","CustomerEmail": "cox.workman@seges.com.br","DateCreate": "2017-12-13 03:08:42","DateUpdate": "2018-01-04 09:18:25","Interactions": [
      {
        "Subject": "Without Subject","Message": "I'm looking for men's shorts With sIDe pockets 100 percent polyester.","Sender": "Customer"
      },{
        "Subject": "RE: Without Subject","Message": "Hello! How are you? We don't have this model: / We have other models on the site,take a look.","DateCreate": "2018-01-03 09:18:25","Sender": "Expert"
      },"Message": "Thank you. GO shorts like with sIDe pockets. Who made this model?","DateCreate": "2018-01-04 09:18:25","Sender": "Customer"
      }
    ]
  },{
    "TicketID": 28891,"categoryID": 46403,"CustomerID": 97974,"Customername": "Vilma Mcmahon","CustomerEmail": "vilma.mcmahon@seges.com.br","DateCreate": "2017-12-25 03:12:39","DateUpdate": "2018-02-12 05:14:11","Interactions": [
      {
        "Subject": "Complaint","Message": "Good night! I bought a shoe for my daughter at the store,but it is staining the sock,see the photos; What can I do to not ruin all the socks ???",{
        "Subject": "RE: Complaint","Message": "Good morning How are you? We hope so! Wow,a shame it happened with tennis ... In this case,there is a different warranty for 12 months,and to offer this guarantee,the brand defines that for cases of defects,the evaluation is done directly with them,to evaluate the defect and already issue the authorization code for the exchange. As soon as the code is issued,just go to the store with the product to exchange! ","DateCreate": "2018-02-12 05:14:11","Sender": "Expert"
      }
    ]
  }

解决方法

$subjects = array();
$json = json_decode($input);
foreach($json as $ticket){
    print_r($ticket->Interactions);
    foreach($ticket->Interactions as $interaction){
        $subjects[] = $interaction->Subject;
    }
}
print_r($subjects);

不确定您想要达到的目标,但是$subjects应该包含每张票的每次互动中的所有主题。

大佬总结

以上是大佬教程为你收集整理的从 PHP 循环组织 JSON全部内容,希望文章能够帮你解决从 PHP 循环组织 JSON所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: