How to check presence of a certain value in inner array and fetch some other value from the same inner array in following scenario?

PHPFan

I've following associative array titled $requests:

Note : Following is the output of the statement print_r($requests);

Array
(
    [0] => Array
        (
            [request_id] => 33
            [is_seen] => 1
            [message] => 
            [friend_user_id] => 901
        )

    [1] => Array
        (
            [request_id] => 23
            [is_seen] => 1
            [message] => 
            [friend_user_id] => 970
        )

)

Now what I want to achieve is I'll take value from user in a variable say $friend_user_id. Now I've to parse through the above array for checking whether any of the inner array has got the same value as of $friend_user_id in a key titled ['friend_user_id']. If the match is found then the value from key ['request_id'] from the same inner array should be fetched, if no match is found then it should return nothing.

For example, the variable $friend_user_id contains value 901 then it should return me 33(the respective request_id)

How should I achieve this in an efficient, optimum and reliable way?

Thanks in advance.

Narendrasingh Sisodia

A simple foreach along with in_array will do the trick as

$friend_user_id = 901;
foreach($arr as $key => $value){
    if(in_array($friend_user_id,$value)){
        $request_id = $value['request_id'];
    }
}
echo $request_id;

Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check presence of a certain value in inner array and fetch some other value from the same inner array in following scenario?

From Dev

Sorting an array of array by a certain value in the inner array

From Dev

How to create an inner array inside parent array in following scenario?

From Dev

Why the inner array elements are not getting sorted in descending order of time stamp value in following scenario?

From Dev

How to check if a value in an Array is contained in the same array as its following one

From Dev

Filtering array by inner array value

From Dev

Getting value from inner array in json

From Dev

How to aggregate value of inner array in MongoDB?

From Dev

Set inner value for multidimensional array

From Dev

Set inner value for multidimensional array

From Dev

How to check whether a value in a key is same for all inner arrays

From Dev

Firestore fetch data from inner array of the doc

From Dev

Ruby array of arrays find by inner array value

From Dev

How to check if an array in an arraylist contains a certain value?

From Dev

GSON @SerializedName to get value from inner array in json

From Dev

fetch value from an array

From Dev

How to get maximum value in inner array elements in php

From Dev

How to fetch a not so random value from an array?

From Dev

How to fetch and store value as coordinate from array

From Dev

How to check if multidimensional array contains same value?

From Dev

IOS how to check the value of an array if it contains a certain value and display it out

From Dev

how to check if a value inside an array is larger than a certain value

From Dev

Check value of array in array had same value

From Dev

How to manipulate the array in following scenario?

From Dev

How to change the array in following scenario?

From Dev

how to check an array if each following value is greater than the last

From Dev

how to fetch records from other table using inner join

From Dev

fetch value from a given array

From Dev

Find first value of inner array based on second value

Related Related

  1. 1

    How to check presence of a certain value in inner array and fetch some other value from the same inner array in following scenario?

  2. 2

    Sorting an array of array by a certain value in the inner array

  3. 3

    How to create an inner array inside parent array in following scenario?

  4. 4

    Why the inner array elements are not getting sorted in descending order of time stamp value in following scenario?

  5. 5

    How to check if a value in an Array is contained in the same array as its following one

  6. 6

    Filtering array by inner array value

  7. 7

    Getting value from inner array in json

  8. 8

    How to aggregate value of inner array in MongoDB?

  9. 9

    Set inner value for multidimensional array

  10. 10

    Set inner value for multidimensional array

  11. 11

    How to check whether a value in a key is same for all inner arrays

  12. 12

    Firestore fetch data from inner array of the doc

  13. 13

    Ruby array of arrays find by inner array value

  14. 14

    How to check if an array in an arraylist contains a certain value?

  15. 15

    GSON @SerializedName to get value from inner array in json

  16. 16

    fetch value from an array

  17. 17

    How to get maximum value in inner array elements in php

  18. 18

    How to fetch a not so random value from an array?

  19. 19

    How to fetch and store value as coordinate from array

  20. 20

    How to check if multidimensional array contains same value?

  21. 21

    IOS how to check the value of an array if it contains a certain value and display it out

  22. 22

    how to check if a value inside an array is larger than a certain value

  23. 23

    Check value of array in array had same value

  24. 24

    How to manipulate the array in following scenario?

  25. 25

    How to change the array in following scenario?

  26. 26

    how to check an array if each following value is greater than the last

  27. 27

    how to fetch records from other table using inner join

  28. 28

    fetch value from a given array

  29. 29

    Find first value of inner array based on second value

HotTag

Archive