0

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", $array);

How exactly should something like this be approached?

Thank you.

2
  • in_array will only work on the values, that is, what you write after "=>" Commented Sep 29, 2012 at 22:01
  • 1
    What is the structure of the array when there's more than one user in there? Commented Sep 29, 2012 at 22:03

2 Answers 2

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.

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

0
 $users = array( 'user_id' => array('username' => "username",
                                    'location' => "location",
                                    'other' => "other");

user_id is their NUMBER user_id

So you then call $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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.