PHP array and string -


i have array containing dates (year) , position. need build string out of it, where: every position separated / if it's in different year ; , - must appear if there no result on year. , if have -, there no need use / separate years ... i'm struggling build logic , code it.

example:

array(7) {    [0]=> array(2) {      ["year"]=> string(4) "2015"      ["hformpos"]=> string(1) "2" }    [1]=> array(2) {      ["year"]=> string(4) "2015"      ["hformpos"]=> string(1) "4" }    [2]=> array(2) {      ["year"]=> string(4) "2015"      ["hformpos"]=> string(1) "5" }    [3]=> array(2) {      ["year"]=> string(4) "2015"      ["hformpos"]=> string(1) "5" }    [4]=> array(2) {      ["year"]=> string(4) "2015"      ["hformpos"]=> int(0) }    [5]=> array(2) {      ["year"]=> string(4) "2014"      ["hformpos"]=> string(1) "2" }    [6]=> array(2) {      ["year"]=> string(4) "2014"      ["hformpos"]=> string(1) "3" } } 

should show: 32/05542

and

array(7) {    [0]=> array(2) {      ["year"]=> string(4) "2014"      ["hformpos"]=> string(1) "2" }    [1]=> array(2) {      ["year"]=> string(4) "2014"      ["hformpos"]=> string(1) "4" }    [2]=> array(2) {      ["year"]=> string(4) "2014"      ["hformpos"]=> string(1) "5" }    [3]=> array(2) {      ["year"]=> string(4) "2014"      ["hformpos"]=> string(1) "5" }    [4]=> array(2) {      ["year"]=> string(4) "2013"      ["hformpos"]=> int(0) }    [5]=> array(2) {      ["year"]=> string(4) "2011"      ["hformpos"]=> string(1) "2" }    [6]=> array(2) {      ["year"]=> string(4) "2011"      ["hformpos"]=> string(1) "3" } } 

32-5542-

thank you!

i assuming array sorted in descending order of dates(year).

$year_flag = $arrays[0]["year"]; $result = ""; foreach( $arrays $array ) {     if($year_flag == $array["year"]){         if( $array["hformpos"] !==0){             $result.= $array["hformpos"];            }else {             $result.= "-";           }      }elseif( $array["hformpos"] ===0){         $result.= "-";       }else{         $result.= "/";           if( $array["hformpos"] !==0){             $result.= $array["hformpos"];            }else {             $result.= "-";           }     }     $year_flag = $array["year"]; } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -