doupao6698 2017-10-05 02:45
浏览 15
已采纳

访问数组内的数组

I'm working on my task right now, which accessing specific array when it is called by front end. My example data is like this

{
    "data": [
        {
            "name": "Jon Snow",
            "id": "01-0001",
        },
        {
            "name": "Robert Stark",
            "id": "01-0002"
        },
        {
            "name": "Sansa Stark",
            "id": "01-002333"
        },
        {
            "name": "Arya Stark",
            "id": "01-00012"
        },
        {
            "name": "Bran Stark",
            "id": "01-0003"
        },
         {
            "name": "Rickon Stark",
            "id": "01-0005"
        }

    ]
}

* In my front end I have this code *

 selectedEmployee: any=null;

    setActiveEmployee(employee: any) {
        this.selectedEmployee = employee;
        let myJSON = JSON.stringify(this.selectedEmployee);
        this.perEmpLateEarly();
    }

Whenever I choose the employee i get the id of that employee. So in this, if i choose "id": "01-0001" it will return the first array and it will keep the rest, and if I choose "id": "01-0002" it will return the second one and will keep the rest and so on. How can I do that in Php?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dsorecdf78171 2017-10-05 02:57
    关注

    You will do a GET/POST HTTP request from frontend, which would look something like this in

    your_backend_page.php?id=<ID HERE>
    

    Then the "your_backend_page.php" would look like as follows:

    $list = {  "data": [ { .... }, { ...   }] } // this is the array you have
    
    $idFromFrontEnd = $_GET["id"];
    
    foreach ($list["data"] as $item) {   // iterate over all items
        if ($item["id"] == $idFromFrontEnd) {   // check if id matches
           echo json_decode($item);     // if matches, print this item
        }
    
    }
    

    Please note this approach is okay if you have a small number of items. For a larger list, you might want to have a database, where you can use sql to select.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog