Forum Moderators: coopster
I would like to find the next and previous key in the array according to the status. ie
[PHP]function getNextKey(){
//should return the 2 as 1 key has status Y (Yes)
// if 1 key has status 'N' then it should return 1 key
}
function getCurrentKey(){
// should return 1
// if 2 key too had status 'Y' then it should return 2
}
function getPrevkey(){
// should return null
// if 2 key too had status 'Y' then it should return 1
}[/PHP]
How to accomplish this type of array manipulation?
Thanks in advance for the valueable help.
>> // if 1 key has status 'N' then it should return 1 key
why should it return 2 and not 3, what differentiates the keys with status N
getCurrentKey seems like you could just walk the array and return whatever has status Y but then it all goes pear shaped when I read the desc for the third function
>> if 2 key too had status 'Y' then it should return 1
so then multiple entries could be set to Y as well, meaning I would really have no way to tell which is which
then you really just need to walk the array, in each iteration have the last iteration and the present one and compare them
getNextKey
if this.status == N and last.status == Y
show this
getPrevkey
if this.status == N and last.status == Y
then go back 2
getCurrentKey
if this.status == N and last.status == Y
show last
<?php
$thearr = array(1 => array('key1' => 'aaa', 'status' => 'Y'), 2 => array('key1' => 'yyy', 'status' => 'N'), 3 => array('key1' => 'zzz', 'status' => 'N'));
$counter = 1;
while (isset($thearr[$counter])) {
echo '<br><pre>',$counter,' ',print_r($thearr[$counter]),'</pre>';
$counter++;
}
?>
then add the this/last logic
<?php
$thearr = array(1 => array('key1' => 'aaa', 'status' => 'Y'), 2 => array('key1' => 'yyy', 'status' => 'N'), 3 => array('key1' => 'zzz', 'status' => 'N'));
$counter = 1;
$next = array();
$last = array('key1' => $thearr[$counter]['key1'], 'status' => $thearr[$counter]['status']);
while (isset($thearr[$counter])) {
$last = $next;
$next = array('key1' => $thearr[$counter]['key1'], 'status' => $thearr[$counter]['status']);
echo '<br><pre>' , $counter , ' ' , print_r($next) , '</pre>';
$counter++;
}
?>
then turn it into a function
<?php
function findmykey($thearr,$which) {
$counter = 1;
$retval = '';
$next = array();
$last = array('key1' => $thearr[$counter]['key1'], 'status' => $thearr[$counter]['status']);
while (isset($thearr[$counter])) {
$last = $next;
$next = array('key1' => $thearr[$counter]['key1'], 'status' => $thearr[$counter]['status']);
$retval .= '<br>' . $counter . ' : key1=' . $next['key1'] . ' status=' . $next['status'];
$counter++;
}
return $retval;
}
$whichkey = '';
$ourarr = array(1 => array('key1' => 'aaa', 'status' => 'Y'), 2 => array('key1' => 'yyy', 'status' => 'N'), 3 => array('key1' => 'zzz', 'status' => 'N'));
$whichkey = findmykey($ourarr,'placeholder');
echo $whichkey;
?>
then add your logic from there
<?php
function findmykey($thearr,$which) {
$counter = 1;
$retval = '';
$next = array();
$last = array('key1' => $thearr[$counter]['key1'], 'status' => $thearr[$counter]['status']);
while (isset($thearr[$counter])) {
$last = $next;
$next = array('key1' => $thearr[$counter]['key1'], 'status' => $thearr[$counter]['status']);
switch ($which) {
case 'current':
if ($next['status'] == 'N' && $last['status'] =='Y') $retval = $last['key1'];
break;
case 'next':
if ($next['status'] == 'N' && $last['status'] =='Y') $retval = $next['key1'];
break;
case 'previous':
if ($next['status'] == 'N' && $last['status'] =='Y') {
if ($counter - 2 <= 0) $retval = 'first key is current';
else $retval = $thearr[$counter - 2]['key1'];
}
break;
}
$counter++;
}
return $retval;
}
$whichkey = '';
$ourarr = array(1 => array('key1' => 'aaa', 'status' => 'Y'), 2 => array('key1' => 'yyy', 'status' => 'N'), 3 => array('key1' => 'zzz', 'status' => 'N'));
echo '<p>ourarr: <pre>';
print_r($ourarr);
echo '</pre><p><br>';
// test current
$whichkey = findmykey($ourarr,'current');
echo '<br>current: ',$whichkey;
// test next
$whichkey = findmykey($ourarr,'next');
echo '<br>next: ',$whichkey;
// test previous
$whichkey = findmykey($ourarr,'previous');
echo '<br>previous: ',$whichkey;
?>