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 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测