dqoqnmb163241 2013-12-25 03:33
浏览 58
已采纳

找出数组中特定键的位置

I have created a class to find out the position / level of a particular key inside an array. I was actually trying to create a function but I ended up creating an entire class as I had to keep track of my counter. Although the class is working perfectly, I really need to get this done using a function only.

Could you please help me with a solution so that I can achieve the same result using function / functions only.

Here's my code for the class :

class Boom {

private $count = 0; 

    function get_depth($array, $keyValue)
    {   

        foreach ($array as $key=>$value) {

            if($key==$keyValue)
            {   

                return $this->count;


            }
            elseif(is_array($value))
            {
                $this->count++;                 
                echo $this->get_depth($value,$keyValue);
                $this->count--;
            }

        }


    }

}

To use this class:

$obj = new Boom();  
echo $obj->get_depth($array, 'your_key'); // 'your_key' example: 'Refrigerator'

You can use an array like following:

$asset = array(

'Electronics' => array(
                "TV"=>array("TV1"=>500,"TV2"=>2500),
                "Refrigerator"=>200,
                "Washing Machine"=>array("WM1"=>array("b1"=>array("b11"=>80,"b22"=>10),"WM12"=>10),"WM2"=>5500),
                "Savings Accounts"=>200,
                "Money Market Accounts"=>200,
                 ),
'Sports'=> array(
                "Cricket"=>array("CBat"=>500,"CBall"=>2500),
                "Tennis"=>900,
                "Football"=>array("FBall"=>1500,"FJersy"=>5500),

                ),
'Random' =>"0"                                  
);
  • 写回答

1条回答 默认 最新

  • dtcpvz8162 2013-12-25 03:36
    关注

    Simple. Just pass in the key on each recursive call:

    function get_depth($array, $keyValue, $count = 0)
    {
        foreach ($array as $key=>$value) {
            if ($key==$keyValue) {
                return $count;
            } elseif (is_array($value)) {
                $count++;                 
                echo get_depth($value, $keyValue, $count);
                $count--;
            }
        }
    }
    

    Note: I haven't verified the functionality of your code, I just copied your exact code in a way that does not require a class to store the variable.

    Another way to do it might be to use a static variable (also not tested), but I would suggest going with the first method, above:

    function get_depth($array, $keyValue)
    {
        static $count = 0;
    
        foreach ($array as $key=>$value) {
            if ($key==$keyValue) {
                return $count;
            } elseif (is_array($value)) {
                $count++;
                echo get_depth($value, $keyValue);
                $count--;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看