PHP - In an array like 1,3,6,7; how to select row 3? (lowest number below 6) -


i have obscure array follows product id's , respective details.

here example of array output :

id - 1 stockcode - 113 desc - printer 1 cost - r 113.00  id - 3 stockcode - 133 desc - printer 3 cost - r 133.00  id - 6 stockcode - 163 desc - printer 6 cost - r 163.00  id - 7 stockcode -  desc - printer 6 details cost -   id - 8 stockcode -  desc - printer 6 cost -  

for reason long explain, able loop through array , within loop , produce outcome.

that being id data in second value , nothing in first , third values, gets second value appended preceding 2nd value of key (id) came before here stuck current id. once has been done, key must deleted , process continues.

here code that, array named $csvarray , $key relate "id" in example:

# clean array - append 2nd values previous values foreach($csvarray $key=>$val) {        // imagine id # 7 in loop     if ($val[0] == '' && $val[1] !== '' && $val[2] == '')     {         $writeto = how select key before 7, in case 6                    if 6, key 3         $csvarray[$writeto][1] = $csvarray[$writeto][1] . ', ' . $val[1];         unset($csvarray[$key]);     } } 

so in essence, find way select key value preceding current value while looping through array...

if share knowledge or has suggestion or advice or kind of input on question, great hear!

thank you!

edit :

thanks input! given input correct approach :

# clean array - append 2nd values preceeding key second value $writeto = null; foreach($csvarray $key=>$val) {        if ($val[0] == '' && $val[1] !== '' && $val[2] == '')     {         $csvarray[$writeto][1] = $csvarray[$writeto][1] . ', ' . $val[1];         unset($csvarray[$key]);     }     else      {         $writeto = $key;      } } 

shouldn't in else ?? if understood well

all have after end of if statement

if {     .... } else {    $writeto=$key; } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -