0

I have one multi dimentional array, the problem is I want the array values of specific key. I already tried current() and end() of array which is not useful to me. So please suggest me appropriate solution to find array values of specific key without using any loop. My Demo array is

Array
(
    [0] => Array
        (
            [EntityType] => Array
                (
                    [Id] => 1
                    [Code] => SUP/13-14/10001
                    [Name] => Supplier
                    [DisplayName] => Supplier
                    [ModuleIdentifier] => 1
                    [IsAdd] => 
                    [IsEdit] => 1
                    [IsDelete] => 1
                )

        )

    [1] => Array
        (
            [EntityType] => Array
                (
                    [Id] => 2
                    [Code] => Emp/13-14-10002
                    [Name] => Employee
                    [DisplayName] => Employee
                    [ModuleIdentifier] => 1
                    [IsAdd] => 
                    [IsEdit] => 
                    [IsDelete] => 
                )

        )

    [2] => Array
        (
            [EntityType] => Array
                (
                    [Id] => 3
                    [Code] => CUS/13-14/10003
                    [Name] => Customer
                    [DisplayName] => Customer
                    [ModuleIdentifier] => 1
                    [IsAdd] => 1
                    [IsEdit] => 
                    [IsDelete] => 
                )

        )

)

I want array having name Customer. So how to get these array... Thanks !

2
  • What do you mean 'specific key' ? From where it is derived? Commented Apr 1, 2014 at 8:10
  • I have an answer to this maybe a simple and precise one. But, not using loop? Then how are you gonna traverse your array with finite elements to check if the name is equal to Customer? Commented Apr 1, 2014 at 9:21

4 Answers 4

1

You may use array_filter in conjunction with array_map:

function findElem($array, $val) {
    $result = array_map(
        function ($v) { return $v['EntityType']; },
        array_filter($array, function ($v) use($val) { return $v['EntityType']['Name'] == $val; })
    );
    return count($result)? $result[0] : false;
}

print_r(findElem($array, 'Customer'));
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to access n'th element of your array just try with:

$array[n]

Where n is an integer value, so:

$array[2]

Comments

0

This line will get you all values of the key "Name" assuming your source array is named $arr if that's what you wanted:

$names = array_map( function($item) { return $item["EntityType"]["Name"]; } , $arr );

Comments

0

You can access your array data like this: $array[0]["EntityType"]["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.