使用 php 删除响应数组中的数据

我有一个数组,如示例中所示。使用我编写的代码,我得到了一个类似于 Output1 中的输出。


问:我请求的服务与我自己的应用程序不同。答案是一个数组,它包含 id 信息。有权限 () 用于授权控制。权限函数将用户权限返回为真假。但是我无法编写正确的语法,因为我无法完全记住这个函数。当我使用自己的代码时,我可以拉取具有 output1 中的权限的用户。但我希望分页信息保留在数组中。如输出2所示。我应该应用什么方法?


此数组就是一个示例。我只是试图解释我自己的问题。


我的代码=>(拉拉维尔 5.8)


$response = ….get(url)………


$list = $response->getBody()[‘content’];


$collection = collect($list);


$filtered = $collection->filter(function ($item) [

    return auth()->user->permission($item['id'])

]);


Return [‘content’ => $filtered];



原始响应:


[

  "paginate"=> 

  [

      "page"=> 5,

      "per_page"=> 20,

      "page_count"=> 20,

      "total_count"=> 521,

      "Links"=> [

        ["self"=> "/products?page=5&per_page=20"],

        ["first"=> "/products?page=0&per_page=20"],

      ]

  ],

  "content"=> [

    [

      "id"=> 1,

      "name"=> "Widget #1",

    ],

    [

      "id"=> 2,

      "name"=> "Widget #2",

    ],

    [

      "id"=> 3,

      "name"=> "Widget #3",

    ]

  ]

]

输出 1:


[

  "content"=> 

  [

      "id"=> 1,

      "name"=> "Widget #1",

    ],

   ----------------------> Authorized users(id= 1, 3) here, but no pagination.

[

      "id"=> 3,

      "name"=> "Widget #3",

    ]

  ]



预期输出:


[

  "paginate"=> 

  [

      "page"=> 5,

      "per_page"=> 20,

      "page_count"=> 20,

      "total_count"=> 521,

      "Links"=> [

        ["self"=> "/products?page=5&per_page=20"],

        ["first"=> "/products?page=0&per_page=20"],

      ]

  ],

  "content"=> [

    [

      "id"=> 1,

      "name"=> "Widget #1",

      "uri"=> "/products/1"

    ],

    [

      "id"=> 3,

      "name"=> "Widget #3",

      "uri"=> "/products/3"

    ]

  ]

]


噜噜哒
浏览 99回答 2
2回答

桃花长相依

所以,如果你说的是正确的,看起来你已经正确地过滤了你的,以排除那些有不需要的s.干得好!现在,您需要返回还包含分页数据的 JSON。首先,让我们逐步完成您的代码,看看我们是如何在这里结束的:contentid$response = ….get(url)………$list = $response->getBody()[‘content’];就在这里,在前两行中,我们看到您仅从响应正文中选择了 并将其存储在 .content$list$collection = collect($list);$filtered = $collection->filter(function ($item) [    return auth()->user->permission($item['id'])]);现在,您正在获取它并根据 进行过滤。contentidReturn [‘content’ => $filtered];最后,您将返回已过滤的 .因此,根本没有删除任何内容 - 您刚刚提取了 ,对其进行了过滤,然后返回了它。您的数据仍然位于您离开的位置。为了获得你想要返回的JSON,你可以调整你拉出和过滤它的方式,但是由于我不知道你正在做什么或为什么以及为什么以及你的过滤器已经在工作,我的直觉是为什么打扰?您可以获取数据并将其与过滤后的数据相结合,然后您将获得所需的确切内容:contentcontentpaginationcontentpaginationcontent$response = ….get(url)………$list = $response->getBody()[‘content’];$pagination = $response->getBody()[‘pagination’];$collection = collect($list);$filtered = $collection->filter(function ($item) [    return auth()->user->permission($item['id'])]);$alldata = // combine your pagination and filtered objects Return [‘data’ => $alldata];请注意,我没有展示如何组合和过滤 - 这是因为我不确定此时您拥有哪种类型的对象。由于您的示例输出不是真正正确的JSON,我不确定是否真的应该是一个列表/数组,或者它是否只是您编写示例的方式,我不确定这些是字符串还是它们是否已被解析,等等。因此,字符串需要将一个附加到另一个,集合将使用 ,依此类推。paginationcontentpaginationcombine()

aluckdog

您的数组是无效数组!所以我不能尝试,正如我在评论中提到的!我从阅读您的所有编辑中了解到,这里有一些示例,您如何从php中的数组中删除id 2:在开始之前:我们可以选择忽略php中的id 2,其中子句像这样SELECT * FROM table WHERE id NOT IN (2)OR&nbsp;SELECT FROM table WHERE id <> 2&nbsp;您可以在查询时忽略id 2,然后您将有一个没有id 2的数组,让我们与数组连接如果要删除数组中的特定值,请执行以下操作。从关联数组中删除&nbsp; &nbsp;$YourArray = array("id"=>"2","name"=>"somename");&nbsp; &nbsp; //use array key exist to find if id exist&nbsp; &nbsp; $test = array_key_exists("id",$YourArray);&nbsp; &nbsp; &nbsp; if($test == 2){&nbsp; &nbsp; &nbsp; &nbsp; unset($YourArray['id']); //Here you unset id 2&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; print_r($YourArray); //result of new array使用多个数组的第二个解决方案:// PHP program to creating two&nbsp;&nbsp;// dimensional associative array&nbsp;$data = array(&nbsp;&nbsp; &nbsp; // Ankit will act as key&nbsp;&nbsp; &nbsp; "paginate" => array(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Subject and marks are&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // the key value pair&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "id" => 1,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Widget #1",&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "uri"=> "/products/1"&nbsp; &nbsp; ),&nbsp;&nbsp; &nbsp; // Ram will act as key&nbsp;&nbsp; &nbsp; "content" => array(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Subject and marks are&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // the key value pair&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "id" => 2,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Widget #1",&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "uri"=> "/products/1"&nbsp; &nbsp; ),&nbsp;&nbsp;);&nbsp;echo "Display Marks: \n";&nbsp;foreach ($data as $key => $subArr) {&nbsp; &nbsp; if($subArr['id'] == 2){&nbsp; &nbsp; unset($subArr['id']);&nbsp; &nbsp; $data[$key] = $subArr;&nbsp;&nbsp; &nbsp; }}print_r($data);&nbsp;这是演示:https://3v4l.org/mvZPN如果要删除数组中的id及其所有元素:已使用array_filter();$test = array_filter($data, function($data) { return $data['id'] != 2; });print_r($test);这是演示:https://3v4l.org/UgNqu
打开App,查看更多内容
随时随地看视频慕课网APP