R
R
Roman Gor2014-07-15 12:27:14
PHP
Roman Gor, 2014-07-15 12:27:14

How to know if an array value is equal to a given number?

Hello!
There is an array like:

Array
(
    [0] => Array
        (
            [status] => 
            [user_id] => 39828
            [birthday] => 0
            [gender] => 1
        )

    [1] => Array
        (
            [status] => 
            [user_id] => 39827
            [birthday] => 0
            [gender] => 1
        )

    [2] => Array
        (
            [status] => 
            [user_id] => 38497
            [birthday] => 22
            [gender] => 1
        )

    [3] => Array
        (
            [status] => 
            [user_id] => 37043
            [birthday] => 29
            [gender] => 1
        )

)

I need to find out without using cycles whether there is an array with the element [user_id] === 37043 (for example), and if so, delete it.
I tried array_map(), but it takes an array as the last argument, and I only need to pass 37043.
Any ideas?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
maksmaers, 2014-07-15
@maksmaers

Returns found occurrences

$user_id = 37043;

$res = array_filter($your_array, function($arr) use ($user_id){
     return ($arr['user_id'] == $user_id);
});
Otherwise returns an empty array
Function reference - php.net/manual/en/function.array-filter.php

I
iDennis, 2014-07-15
@iDennis

Why don't you like the cycle?

A
Aliansys, 2014-07-15
@Aliansys

I think the array_filter method will suit you ( nl3.php.net/manual/en/function.array-filter.php )
That is, you get something like:

$result= array_filter($array, function($var){
return $var['user_id'] == <искомое_значение>
})

I can't vouch for the correctness of the code. Haven't written in PHP for 100,500 years. In JavaScript, this method works like this)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question