2013-10-03 73 views
-1

前面和後面的值我有數組,例如:獲取陣列

<?php 
$array = array(
    0 => array(
    'subject' => 'Stackoverflow', 
    'body' => '', 
    'name' => 'php' 
    ), 
    1 => array(
    'subject' => 'Test', 
    'body' => 'Wayne', 
    'name' => '' 
    ), 
    2 => array(
    'subject' => '', 
    'body' => 'this is ok', 
    'name' => '' 
    ), 
    3 => array(
    'subject' => 'cnn', 
    'body' => 'Google', 
    'name' => 'private' 
    ), 
    4 => array(
    'subject' => 'code', 
    'body' => '', 
    'name' => '7777' 
    ) 
); 

,我想獲得主題,正文和名稱鍵2,並且如果不存在鍵,然後這應該從過去和未來獲得(單獨的函數)值。

例如,如果我想從2鍵獲取值:

function getCurrentOrPrevious(2); 

應該返回:

array(
    'subject' => 'Test', //from [1] 
    'body' => 'this is ok', //from [2] - current and exist 
    'name' => 'php' //from [0] - in from [2] and [1] not exist 
    ) 

function getCurrentOrNext(2); 

應該返回:

array(
    'subject' => 'cnn', //from [3] 
    'body' => 'this is ok', //from [2] - current 
    'name' => 'php' //from [3] 
    ) 

如何是最好的方法? PHP中有這些操作的功能嗎?

LIVE

+1

這裏沒有現成的php函數。如果有其他statmenets你將不得不這樣做。 – 2013-10-03 18:33:45

+0

那麼我該如何做到這一點? – waynewilliams

回答

0

假設,你填寫類似$數組[] = $值的陣列; [即你必須從零開始的連續數字鍵]:

## $array is the array to take values from, $key is the target key 
## $fields are required fields 
function getCurrentOrPrevious($array, $key, $fields) { 
    if ($key < 0) return null; 
    $output = array(); 
    foreach ($fields as $field) { 
     for ($i = $key; $i >= 0; $i--) { 
     if (!empty($array[$i][$field])) { 
      $output[$field] = $array[$i][$field]; 
      break; 
     } 
    } 
    return $output; 
} 

使用方法如下:

$my_values = getCurrentOrPrevious($array, 12, array('subject', 'body', 'name')); 
0

我想你可以使用PHP empty()功能檢查。你也應該考慮一下這個函數shell到底有多遠?

  • 如果[3](下)也有在同一位置
  • 如果以前什麼指數<0空值?

UPDATE

function getCurrOrNext($array, $index){ 
    $keys = array_keys($array[$index]); 
    foreach($keys AS $key){ 
     if($array[$index][$key] == "") { 
      $array[$index][$key] = (!empty($array[$index+1]) && !empty($array[$index+1][$key])?$array[$index+1][$key]:null); 
     } 
    } 
    return $array; 
} 

我想是這樣的

+0

在這種情況下應該返回null – waynewilliams

+0

謝謝,但是如果我通過$ index = 2那麼身體應該返回'這很好' – waynewilliams

+0

我不明白你的問題?你什麼意思? – helle

0
function getCurrentOrPrev($array, $key) { 
    while(key($array)!==$key) next($array); //move internal pointer to required position first 
    $result = current($array); 

    //loop is going to execute as long as at least one of elements is empty and we didn't get to beginning of array yet 
    while((empty($result['subject']) 
      || empty($result['body']) 
      || empty($result['name'])) 
      && prev($array)!==false) { 
     $c = current($array); 
     //replace empty elements with values of current element 
     $result['subject'] = empty($result['subject']) ? $c['subject'] : ''; 
     $result['body'] = empty($result['body']) ? $c['body'] : ''; 
     $result['name'] = empty($result['name']) ? $c['name'] : ''; 
    } 

    return $result; 
} 

對於下一個功能簡單地next()取代prev()方法,或儘量減少重複代碼,您可能引入第三個參數並根據其值調用正確的方法。

此方法不關心除參數中指定的密鑰之外的其他密鑰的值。 您可能混合了文字和數字索引。