Forum Moderators: coopster

Message Too Old, No Replies

how to manipulate this array ?

         

PHPycho

4:12 am on May 28, 2008 (gmt 0)

10+ Year Member



Hello forums !
Case:
I had the array as:
[PHP]$array = array(1 => array('key1' => '#*$!', 'status' => 'Y'), 2 => array('key1' => 'yyy', 'status' => 'N'), 3 => array('key1' => 'zzz', 'status' => 'N')...);[/PHP]

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.

jatar_k

4:21 am on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the things I don't get at all

>> // 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

PHPycho

4:24 am on May 28, 2008 (gmt 0)

10+ Year Member



Status key with status = Y means that key 's operation has been performed. so if key 1 has status Y then next would be obviously 2 to be performed next..Hope it helps

PHPycho

4:29 am on May 28, 2008 (gmt 0)

10+ Year Member



Additionally:
Status key with status = Y means that key 's operation has been performed.
so if key 1 has status Y then next would be obviously 2 to be performed next..
For getNextKey, it should be the next to the last key with status Y
for getPrevkey, it should be the prev to the last key with status Y
for getCurrentKey, it should be the last key with status Y.

jatar_k

4:43 am on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so it will all happen in order

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

PHPycho

4:56 am on May 28, 2008 (gmt 0)

10+ Year Member



can you please help a bit in code.
thanks a lot

jatar_k

2:54 pm on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



start with a little loop that will output each element

<?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

jatar_k

3:03 pm on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I didn't fully test this

<?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;
?>