前面和後面的值我有數組,例如:獲取陣列
<?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中有這些操作的功能嗎?
這裏沒有現成的php函數。如果有其他statmenets你將不得不這樣做。 – 2013-10-03 18:33:45
那麼我該如何做到這一點? – waynewilliams