Let's say I have an array
假设我有一个数组
$array = array(
'username' => "username",
'location' => "location",
'other' => "other");
This array can hold data for many users, so there could be different values for each 'username', 'location', and 'other' fields. How can I use in_array() or another function to determine if a specific username exists in the array already? Because what if a user has a username like "nyc" and a location of "nyc" and I do
此数组可以容纳许多用户的数据,因此每个“用户名”,“位置”和“其他”字段可能有不同的值。如何使用in_array()或其他函数来确定数组中是否存在特定的用户名?因为如果用户拥有像“nyc”这样的用户名和“nyc”的位置,我会怎么做
in_array("nyc", $array);
How exactly should something like this be approached?
究竟应该接近这样的事情?
Thank you.
2 个解决方案
#1
2
To achieve something that I think is what you want, you can make an array of associative arrays that have the same keys.
为了实现我认为你想要的东西,你可以创建一个具有相同键的关联数组。
<?php
// This syntax will work only on PHP 5.4
$a=[["name"=>"john","age"=>25],["name"=>"philip","age"=>110]];
print_r(array_filter($a, function($item) {return $item["name"] === "john"; }));
?>
Outputs:
Array
(
[0] => Array
(
[name] => john
[age] => 25
)
)
If you just wanted to know if a person named John was in the list, you can just use sizeof/count on the returned array.
如果您只是想知道名为John的人是否在列表中,您可以在返回的数组上使用sizeof / count。
This will allow you to have any number of duplicates, and you don't need to specify any keys. Check out the functions: array_filter, array_reduce, array_map. With all of these, you can process your list using closures like in my example above.
这将允许您具有任意数量的重复项,并且您不需要指定任何键。查看函数:array_filter,array_reduce,array_map。通过所有这些,您可以使用上面的示例中的闭包来处理列表。
Instead of using associative arrays in your array, you could have objects too. Objects are more heavyweight, and need initialization and stuff, so it is grotesque for using them for tiny static (hardcoded) lists. But they may come handy when your data structures grow and you want to make sure every list item has a certain property (the constructor of the class could ensure that everything is initialized). But the good thing is that filter, reduce and map would still work. The "$item" would then be your object.
您也可以拥有对象,而不是在数组中使用关联数组。对象更重量级,需要初始化和东西,所以将它们用于微小的静态(硬编码)列表是怪诞的。但是当数据结构增长并且您希望确保每个列表项都具有某个属性时,它们可能会派上用场(类的构造函数可以确保所有内容都已初始化)。但好处是过滤,减少和映射仍然有效。那么“$ item”就是你的对象。
#2
0
$users = array( 'user_id' => array('username' => "username",
'location' => "location",
'other' => "other");
user_id is their NUMBER user_id
user_id是他们的NUMBER user_id
So you then call $users['####']['username'];
所以你再调用$ users ['####''] ['username'];
IE:
$users = array( '1' => array('username' => 'Jim',
'location' => 'Florida',
'other' => "other"),
'2' => array('username' => 'Jane',
'location' => 'Maryland',
'other' => "Grapes"));
Then use array_keys() to search for their user_id
然后使用array_keys()搜索其user_id
#1
2
To achieve something that I think is what you want, you can make an array of associative arrays that have the same keys.
为了实现我认为你想要的东西,你可以创建一个具有相同键的关联数组。
<?php
// This syntax will work only on PHP 5.4
$a=[["name"=>"john","age"=>25],["name"=>"philip","age"=>110]];
print_r(array_filter($a, function($item) {return $item["name"] === "john"; }));
?>
Outputs:
Array
(
[0] => Array
(
[name] => john
[age] => 25
)
)
If you just wanted to know if a person named John was in the list, you can just use sizeof/count on the returned array.
如果您只是想知道名为John的人是否在列表中,您可以在返回的数组上使用sizeof / count。
This will allow you to have any number of duplicates, and you don't need to specify any keys. Check out the functions: array_filter, array_reduce, array_map. With all of these, you can process your list using closures like in my example above.
这将允许您具有任意数量的重复项,并且您不需要指定任何键。查看函数:array_filter,array_reduce,array_map。通过所有这些,您可以使用上面的示例中的闭包来处理列表。
Instead of using associative arrays in your array, you could have objects too. Objects are more heavyweight, and need initialization and stuff, so it is grotesque for using them for tiny static (hardcoded) lists. But they may come handy when your data structures grow and you want to make sure every list item has a certain property (the constructor of the class could ensure that everything is initialized). But the good thing is that filter, reduce and map would still work. The "$item" would then be your object.
您也可以拥有对象,而不是在数组中使用关联数组。对象更重量级,需要初始化和东西,所以将它们用于微小的静态(硬编码)列表是怪诞的。但是当数据结构增长并且您希望确保每个列表项都具有某个属性时,它们可能会派上用场(类的构造函数可以确保所有内容都已初始化)。但好处是过滤,减少和映射仍然有效。那么“$ item”就是你的对象。
#2
0
$users = array( 'user_id' => array('username' => "username",
'location' => "location",
'other' => "other");
user_id is their NUMBER user_id
user_id是他们的NUMBER user_id
So you then call $users['####']['username'];
所以你再调用$ users ['####''] ['username'];
IE:
$users = array( '1' => array('username' => 'Jim',
'location' => 'Florida',
'other' => "other"),
'2' => array('username' => 'Jane',
'location' => 'Maryland',
'other' => "Grapes"));
Then use array_keys() to search for their user_id
然后使用array_keys()搜索其user_id