douyanjing8287 2013-08-16 04:13
浏览 37
已采纳

在PHP中有效地访问WP cron多维数组

I need to access multiple arrays, the problem lies when I get to the arrays I need like below, I can't access it traditionally because the key will be different every time.

I'm dealing with the following array:

Array
(
    [oe_schedule_charge] => Array
        (
            [617cdb2797153d6fbb03536d429a525b] => Array
                (
                    [schedule] => 
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [id] => cus_2OPctP95LW8smv
                                    [amount] => 12
                                )

                        )

                )

        )

)

There are going to be hundreds of these arrays and I need a way to efficiently access the data within them. I'm using the following code with expected output:

function printValuesByKey($array, $key) {
    if (!is_array($array)) return;
    if (isset($array[$key])) 
        echo $key .': '. $array[$key] .'<br>';
    else
        foreach ($array as $v)
            printValuesByKey($v, $key);
}

$cron = _get_cron_array();

foreach( $cron as $time => $hook ) {
    if (array_key_exists('oe_schedule_charge', $hook)) {
        echo '<div>';
        echo date('D F d Y', $time);
        echo printValuesByKey($hook, 'amount');
        echo printValuesByKey($hook, 'id');
        echo '</div>';
    }
}

But I've never had to deal with this much data, so I would like to take the proper precautions. Any light that can be shed on accessing a multidimensional array like this in an efficient way would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongsechuan0535 2013-08-16 05:09
    关注

    I would consider loading it into an object, then writing member functions to get what you want.

    class myclass { 
    
    private $_uniqueKey;
    private $_schedule;
    private $_args = array();
    
    private $_amount = array();
    private $_id = array();
    
    public function __construct($arrayThing)
    {
        foreach($arrayThing['oe_schedule_charge'] as $uniqueKey => $dataArray)
        {
            $this->_uniqueKey = $uniqueKey;
            $this->_schedule = $dataArray['schedule'];
            $this->_args = $dataArray['args'];
        }
        $this->_afterConstruct();
    }
    
    private function _afterConstruct()
    {
        foreach($this->_args as $argItem)
        {
            if(isset($argItem['amount']) && isset($argItem['id']))
            {
                $this->_amount[] = $argItem['amount'];
                $this->_id[] = $argItem['id'];
            }
        }
    }
    
    public function getUniqueKey()
    {
        return $this->_uniqueKey;
    }
    
    public function getSchedule()
    {
        return $this->_schedule;
    }
    
    public function getArgs()
    {
        return $this->_args;
    }
    
    public function printShitOut($time)
    {
        //You define this. But if you do a print_r( on the object, it will tell you all the items you need. )
    
    }
    
    //code would be like this:
    
    $cron = _get_cron_array();
    
    foreach( $cron as $time => $hook ) 
    {
        $obj = new myclass($hook);
        $obj->printShitOut($time);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。