如何从PHP中的多维数组获取父数组索引 [英] how to get parent array index from multidimensional array in php

查看:180
本文介绍了如何从PHP中的多维数组获取父数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组$ arr.

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
                )

        )
)

并且我有一个变量$ categ.例如$ categ =" homeandgardenairconditioner-Type " 那么我想知道其"名称"为" homeandgardenairconditioner-Type "

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"

即--- o/p应该为9

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

如何获取此索引.请帮助我

how to get this index. Please help me

谢谢

推荐答案

我认为您正在寻找:

function getIndex($name, $array){
    foreach($array as $key => $value){
        if(is_array($value) && $value['name'] == $name)
              return $key;
    }
    return null;
}

这篇关于如何从PHP中的多维数组获取父数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆