2013-08-16 6 views
0

여러 배열에 액세스해야하는데, 내가 아래처럼 필요로하는 배열을 얻을 때 문제가 생깁니다. 열쇠가 매번 달라지기 때문에 전통적으로 액세스 할 수 없습니다.PHP에서 WP cron 다차원 배열에 효율적으로 액세스하기

나는 다음과 같은 배열을 다루는 해요 :

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

         ) 

       ) 

     ) 

) 

이 배열 수백있을거야 내가 효율적으로 내의 데이터에 액세스 할 수있는 방법이 필요합니다. 내가 예상 출력 다음 코드를 사용하고 있습니다 :

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>'; 
    } 
} 

하지만이 정도의 데이터를 처리 할 수 ​​없었습니다, 그래서 적절한 예방 조치를하고 싶습니다. 효율적인 방법으로 다차원 배열에 액세스 할 때 흘릴 수있는 모든 표시등은 크게 감사하겠습니다.

답변

1

개체로로드 한 다음 멤버 함수를 작성하여 원하는 것을 얻을 수 있습니다.

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); 
} 
+0

위대한 작품입니다! 나는 "printShitOut"메서드를 좋아한다, 하하 나는 그것을 그렇게 떠날 것이라고 생각한다! 내가 가지고있는 다른 문제는 출력을위한 최종 foreach 루프에서 "if (array_key_exists ('oe_schedule_charge', $ hook))"를 사용해야한다는 것입니다. 어쨌든 나는 그것을 수업에 포함시킬 수 있습니까? 나는 OOP에서 꽤 초보자이지만 여전히 그것을 배우고있다. – souporserious