简体   繁体   English

PHP array_filter 获取没有键的值

[英]PHP array_filter get value without key

I come from a Javascript background, and I am trying to use array_filter() , but it works quite different from JS.我来自 Javascript 背景,我正在尝试使用array_filter() ,但它的工作方式与 JS 完全不同。

Taking this JS example:以这个 JS 为例:

const people = [
  {
    name: 'Will',
    username: 'will',
  },
  {
    name: 'Alex',
    username: 'alex',
  },
  {
    name: 'Abraham',
    username: 'abraham',
  },
];

const usernameToFind = 'abraham';

const found = people.filter(person => person.username === usernameToFind);

console.log(found[0]); // index 0

// {
//   name: 'Abraham',
//   username: 'abraham'
// }

I expect all the usernames to be different, so it is always going to return only one value.我希望所有的用户名都不同,所以它总是只返回一个值。 So if I want to access the information found I just ask for index 0 .因此,如果我想访问找到的信息,我只需要索引0即可。

On PHP:在 PHP 上:

<?php

$people = [
  [
    'name' => 'Alex',
    'username' => 'alex',
  ],
  [
    'name' => 'Will',
    'username' => 'will',
  ],
  [
    'name' => 'Abraham',
    'username' => 'abraham',
  ],
];

$usernameToFind = 'abraham';

$found = array_filter($people, function($person) use ($usernameToFind) {
  return $person['username'] === $usernameToFind;
});

print_r($found);

// Array
// (
//     [2] => Array
//         (
//             [name] => Abraham
//             [username] => abraham
//         )
// )

So my issue is: I get an array with the index of the element found, but I don't know what the index is.所以我的问题是:我得到一个包含找到元素索引的数组,但我不知道索引是什么。

I saw this question but it is quite different: PHP array_filter to get only one value from an array .我看到了这个问题,但它完全不同: PHP array_filter to get only one value from an array

I am not using array_search() , because my needle to search is 2 or 3 levels deep like:我没有使用array_search() ,因为我的搜索针深度为 2 或 3 级,例如:

array_filter($people, function ($person) use ($cityToFind) {
   return $person['location']['city'] === $cityToFind;
}

I can use a for loop, but I really wanted to use filter instead.我可以使用 for 循环,但我真的想使用过滤器。 Thanks in advance!提前致谢!

You can do a couple things.你可以做几件事。

  1. To get the first element of the array you can use reset($found) https://www.php.net/manual/en/function.reset.php要获取数组的第一个元素,您可以使用reset($found) https://www.php.net/manual/en/function.reset.php

  2. After filtering the array you can reset the array keys to start at 0 using array_values($found) https://www.php.net/manual/en/function.array-values.php过滤数组后,您可以使用array_values($found) https://www.php.net/manual/en/function.array-values.php将数组键重置为从 0 开始

Using array_filter() will always process the entire array, in your example it's the last entry so it needs to anyway.使用array_filter()将始终处理整个数组,在您的示例中,它是最后一个条目,因此无论如何它都需要。 But if you had 500 entries and it was the first 1, it will still check all 500 entries.但是如果你有 500 个条目并且它是第一个,它仍然会检查所有 500 个条目。

Instead you could use a simple foreach() loop which stops as soon as it finds the first one...相反,您可以使用一个简单的foreach()循环,它会在找到第一个循环后立即停止...

foreach ( $people as $index => $person )    {
    if ( $person['username'] === $usernameToFind )  {
        echo "Index={$index} name={$person['name']}";
        break;
    }
}

gives...给...

Index=2 name=Abraham

As an answer to your original question - you can use After array_filter(), how can I reset the keys to go in numerical order starting at 0 to reset the keys so you can use [0] .作为对原始问题的回答 - 您可以使用After array_filter(), how can I reset the keys to go 以从 0 开始的数字顺序来重置密钥,以便您可以使用[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM