简体   繁体   English

如何从php中的多维数组获取父数组索引

[英]how to get parent array index from multidimensional array in php

I have an array $arr.我有一个数组 $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 "并且我有一个变量 $categ。例如 $categ = " homeandgardenairconditioner-Type " 然后我想知道其 " name " 为 " homeandgardenairconditioner-Type " 的父数组的索引

ie --- o/p should be 9即 --- o/p 应该是 9

how to get this index.如何获得这个索引。 Please help me请帮我

Thanks谢谢

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM