0

I have an array $arr.

when I print this it shows the results as follows.

Array
(
    [0] => Array
        (
            [name] => homeandgarden-Control Type
            [1] => Array
                (
                    [0] => product
                )

        )

    [1] => Array
        (
            [name] => homeandgarden-Depth
            [1] => Array
                (
                    [0] => product
                )

        )

    [2] => Array
        (
            [name] => homeandgarden-Height
            [1] => Array
                (
                    [0] => product
                )

        )

    [3] => Array
        (
            [name] => homeandgarden-Load Type
            [1] => Array
                (
                    [0] => product
                )

        )

    [4] => Array
        (
            [name] => homeandgarden-Machine Type
            [1] => Array
                (
                    [0] => product
                )

        )

    [5] => Array
        (
            [name] => homeandgarden-Type
            [1] => Array
                (
                    [0] => product
                )

        )

    [6] => Array
        (
            [name] => homeandgarden-Width
            [1] => Array
                (
                    [0] => product
                )

        )

    [7] => Array
        (
            [name] => computer & ele
            [label] => 
            [singular_label] => 
            [hierarchical] => 1
            [show_ui] => 1
            [query_var] => 1
            [rewrite] => 1
            [rewrite_slug] => 
            [0] => Array
                (
                    [search_items] => 
                    [popular_items] => 
                    [all_items] => 
                    [parent_item] => 
                    [parent_item_colon] => 
                    [edit_item] => 
                    [update_item] => 
                    [add_new_item] => 
                    [new_item_name] => 
                    [separate_items_with_commas] => 
                    [add_or_remove_items] => 
                    [choose_from_most_used] => 
                )

            [1] => Array
                (
                    [0] => product
                )

        )

    [8] => Array
        (
            [name] => computer
            [label] => 
            [singular_label] => 
            [hierarchical] => 1
            [show_ui] => 1
            [query_var] => 1
            [rewrite] => 1
            [rewrite_slug] => 
            [0] => Array
                (
                    [search_items] => 
                    [popular_items] => 
                    [all_items] => 
                    [parent_item] => 
                    [parent_item_colon] => 
                    [edit_item] => 
                    [update_item] => 
                    [add_new_item] => 
                    [new_item_name] => 
                    [separate_items_with_commas] => 
                    [add_or_remove_items] => 
                    [choose_from_most_used] => 
                )

            [1] => Array
                (
                    [0] => product
                )

        )

    [9] => Array
        (
            [name] => homeandgardenairconditioner-Type
            [label] => 
            [singular_label] => 
            [hierarchical] => 1
            [show_ui] => 1
            [query_var] => 1
            [rewrite] => 1
            [rewrite_slug] => 
            [0] => Array
                (
                    [search_items] => 
                    [popular_items] => 
                    [all_items] => 
                    [parent_item] => 
                    [parent_item_colon] => 
                    [edit_item] => 
                    [update_item] => 
                    [add_new_item] => 
                    [new_item_name] => 
                    [separate_items_with_commas] => 
                    [add_or_remove_items] => 
                    [choose_from_most_used] => 
                )

            [1] => Array
                (
                    [0] => product
                )

        )

    [10] => Array
        (
            [name] => homeandgardendishwasher-Control Type
            [label] => 
            [singular_label] => 
            [hierarchical] => 1
            [show_ui] => 1
            [query_var] => 1
            [rewrite] => 1
            [rewrite_slug] => 
            [0] => Array
                (
                    [search_items] => 
                    [popular_items] => 
                    [all_items] => 
                    [parent_item] => 
                    [parent_item_colon] => 
                    [edit_item] => 
                    [update_item] => 
                    [add_new_item] => 
                    [new_item_name] => 
                    [separate_items_with_commas] => 
                    [add_or_remove_items] => 
                    [choose_from_most_used] => 
                )

            [1] => Array
                (
                    [0] => product
                )

        )
)

and I have a variable $categ.For example $categ = "homeandgardenairconditioner-Type" then I want to know the index of the parent array whose "name" is "homeandgardenairconditioner-Type"

i.e --- o/p should be 9

how to get this index. Please help me

Thanks

3
  • @PLB I don't know how to traverse in this array so that first I check it's name and then get it's parent array index that's why I am asking.Thanks Commented Jul 12, 2013 at 4:56
  • Is it already defined that your values will be located in the first dimension of the array ? Or it should be used to find values in $array[$i][$j][$k][$etc] ? Commented Jul 12, 2013 at 4:59
  • @Frederik.L I have 'name' .Can you write your code which will work?Thanks Commented Jul 12, 2013 at 5:04

1 Answer 1

7

I think you are looking for this:

function getIndex($name, $array){
    foreach($array as $key => $value){
        if(is_array($value) && $value['name'] == $name)
              return $key;
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

1 Comment

$index = array_search( $value, array_column( $array, 'name' ) ); I think this work too .

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.