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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题