【问题标题】:Delete data in response array with php用php删除响应数组中的数据
【发布时间】:2020-05-27 05:22:29
【问题描述】:

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

问题:我请求的服务与我自己的应用程序不同。答案是一个数组,它包含 id 信息。权限控制有permission()。权限函数将用户权限返回为 true 和 false。但是我写不出正确的语法,因为我记不起这个函数。当我使用自己的代码时,我可以像 output1 一样拉取有权限的用户。但我希望分页信息保留在数组中。如输出 2。我应该采用什么方法?

这个数组是一个例子。我只是试图解释我自己的问题。

我的代码=>(Laravel 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"
    ]
  ]
]

【问题讨论】:

  • 也许我遗漏了什么,但“示例”和“输出 2”在我看来是一样的(减去后者中 id 为 2 的记录)
  • @patrickQ 是的,你是对的,我只想在不破坏基本 json 结构的情况下删除 id 2。我丢失了 Output1 中的所有分页信息。删除 id 2 后,我希望 json 保留在 output2 中
  • 请包含您在问题正文中使用的实际代码。
  • 另外,您的示例 JSON 实际上不是有效的 JSON。请向我们提供一些可用的东西,以及您的代码使用的 JSON 的真实表示。
  • @PatrickQ 我无法共享真实的 Json 数据,因为我现在没有访问权限。我添加了我在 Laravel 中使用的代码。用户权限返回我的权限()函数为真和假。我想删除未经授权的。

标签: php arrays laravel


【解决方案1】:

因此,如果您所说的是正确的,那么您似乎已经正确过滤了您的 content 以排除那些不需要的 ids。好工作!现在,您想要返回的 JSON 也包含您的分页数据。首先让我们逐步检查您的代码,看看我们是如何结束的:

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

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

在这里,在前两行中,我们看到您仅从响应正文中选择了 content 并将其存储在 $list 中。

$collection = collect($list);

$filtered = $collection->filter(function ($item) [
    return auth()->user->permission($item['id'])
]);

现在,您将使用 content 并根据 id 对其进行过滤。

Return [‘content’ => $filtered];

最后,您将返回过滤后的content。因此,根本没有删除任何内容 - 您刚刚取出 content,过滤它并返回它。您的pagination 数据仍然位于您离开的位置。为了获得您想要返回的 JSON,您可以调整拉出 content 并对其进行过滤的方式,但是由于我不知道您在做什么或为什么以及您的过滤器已经在工作我的许多细节本能是为什么要打扰?您可以将您的 pagination 数据与过滤后的 content 结合起来,然后您就会得到您正在寻找的内容:

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

请注意,我没有展示如何组合 pagination 和过滤后的 content - 这是因为我不确定您现在拥有什么类型的对象。由于您的示例输出不是真正正确的 JSON,我不确定 pagination 是否真的应该是一个列表/数组,或者它只是您编写示例的方式,我不确定这些是字符串还是如果它们已被解析,等等。因此字符串需要将一个附加到另一个,集合将使用combine(),等等。

【讨论】:

    【解决方案2】:

    您的数组是无效数组!所以我无法尝试,正如我在评论中提到的!

    我从阅读您的所有编辑中了解到,这里有一些示例如何从 php 中的数组中删除 id 2:

    开始之前:我们可以选择忽略 php where 子句中的 id 2

    SELECT * FROM table WHERE id NOT IN (2)
    
    OR 
    
    SELECT FROM table WHERE id <> 2 
    

    你可以在查询时忽略 id 2,那么你将有一个没有 id 2 的数组,让我们继续使用数组

    如果要删除数组中的特定值,请执行以下操作。

    从关联数组中删除

       $YourArray = array("id"=>"2","name"=>"somename");
    
        //use array key exist to find if id exist
    
        $test = array_key_exists("id",$YourArray);
          if($test == 2){
            unset($YourArray['id']); //Here you unset id 2
          }
        print_r($YourArray); //result of new array
    

    多维数组的第二种解决方案

    // PHP program to creating two  
    // dimensional associative array 
    $data = array( 
    
        // Ankit will act as key 
        "paginate" => array( 
    
            // Subject and marks are 
            // the key value pair 
            "id" => 1, 
            "name" => "Widget #1",  
            "uri"=> "/products/1"
        ), 
    
        // Ram will act as key 
        "content" => array( 
    
            // Subject and marks are 
            // the key value pair 
            "id" => 2, 
            "name" => "Widget #1",  
            "uri"=> "/products/1"
        ),  
    ); 
    
    echo "Display Marks: \n"; 
    
    
    foreach ($data as $key => $subArr) {
        if($subArr['id'] == 2){
        unset($subArr['id']);
        $data[$key] = $subArr; 
        }
    }
    print_r($data); 
    

    这里是演示:https://3v4l.org/mvZPN

    如果你想删除数组中的 id 及其所有元素array_filter();used

    $test = array_filter($data, function($data) { return $data['id'] != 2; });
    
    print_r($test);
    

    这里是演示:https://3v4l.org/UgNqu

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 2020-11-06
      • 2021-11-11
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多