Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php删除响应数组中的数据_Php_Arrays_Laravel - Fatal编程技术网

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

用php删除响应数组中的数据,php,arrays,laravel,Php,Arrays,Laravel,我有一个数组,如示例中所示。使用我编写的代码,我得到了类似Output1的输出 问题:我请求的服务与我自己的应用程序不同。答案是一个数组,它包含id信息。有权限进行授权控制。permission函数将用户权限返回为true和false。但是我不能写出正确的语法,因为我不能完全记住这个函数。当我使用我自己的代码时,我可以像output1一样拉取拥有权限的用户。但我希望分页信息保留在数组中。如输出2所示。我应该用什么方法 这个数组就是一个例子。我只是想解释我自己的问题 我的代码=>Laravel 5

我有一个数组,如示例中所示。使用我编写的代码,我得到了类似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"
    ]
  ]
]

所以,若你们所说的是正确的,那个么看起来你们已经正确地过滤了你们的内容,以排除那个些带有不需要的ID的内容。干得好现在,您希望返回也包含分页数据的JSON。首先,让我们逐步浏览您的代码,看看我们是如何在这里结束的:

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

$list = $response->getBody()[‘content’];
就在这里,在前两行中,我们看到您仅从响应正文中选择了内容,并将其存储在$list中

现在,您将获取该内容并根据id对其进行过滤

最后,返回过滤后的内容。因此,没有任何内容被删除——您只是取出内容,过滤并返回。您的分页数据仍然位于您留下的位置。为了得到你想要返回的JSON,你可以调整你提取内容和过滤内容的方式,但是因为我不知道你在做什么或者为什么,你的过滤器已经在工作,我的直觉是为什么要麻烦?您只需获取分页数据并将其与筛选的内容相结合,就可以得到您想要的内容:

$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,我不确定分页是否真的应该是列表/数组,或者这只是您编写示例的方式,我不确定这些是否是字符串,或者它们是否已被解析,等等。因此字符串需要将一个附加到另一个,集合将使用combine,等等

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

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

在开始之前:我们可以选择忽略php where子句中的ID2,如下所示

SELECT * FROM table WHERE id NOT IN (2)

OR 

SELECT FROM table WHERE id <> 2 
具有多元素阵列的第二种解决方案:

以下是演示:

如果要删除数组中的id及其所有元素:array\u filter;使用


下面是演示:

也许我遗漏了一些东西,但是示例和输出2在我看来是一样的,减去列表中id为2的记录latter@patrickQ是的,你是对的,我只想在不破坏基本json结构的情况下删除ID2。我在Output1中丢失了所有分页信息。删除id 2后,我希望json保留在output2Please中,包括您在问题主体中使用的实际代码。此外,您的示例json实际上不是有效的json。请向我们提供一些有用的东西,以及您的代码使用的JSON的真实表示。@PatrickQ我无法共享真正的JSON数据,因为我现在没有访问权限。我添加了与Laravel一起使用的代码。用户权限返回我,权限函数为true和false。我想删除未经授权的。
$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];
SELECT * FROM table WHERE id NOT IN (2)

OR 

SELECT FROM table WHERE 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); 
$test = array_filter($data, function($data) { return $data['id'] != 2; });

print_r($test);