rashmi_k28 Posted June 13, 2008 Share Posted June 13, 2008 Hi, How to push into the array 0//Push 0 //Skip 0//skip 0//Skip 0//Skip 1// Push 1//Skip 1//Skip 1//Skip 0 //Push 1 //Push 1 //Skip If any change from 0 to 1 then push else skip that row. If next row has the same value then push into the array else skip that row. Link to comment https://forums.phpfreaks.com/topic/110051-array_push/ Share on other sites More sharing options...
tapos Posted June 13, 2008 Share Posted June 13, 2008 try this $array = array(); $previous = null; foreach($row as $current) { if($previous === null || ($previous == 0 && $current == 1) || ($previous == 1 && $current == 0) ) { array_push($array, $current); //or u can use simply $array[] = $current } } Link to comment https://forums.phpfreaks.com/topic/110051-array_push/#findComment-564710 Share on other sites More sharing options...
kenrbnsn Posted June 13, 2008 Share Posted June 13, 2008 That won't work, since you're never setting $previous. Try something like: <?php $test = explode(',','1,1,1,0,0,0,1,1,0,0,1,1,0,0,0,1'); $prev = -1; $new = array(); foreach ($test as $t) { if ($t != $prev) $new[] = $t; $prev = $t; } echo implode(',',$new); ?> Ken Link to comment https://forums.phpfreaks.com/topic/110051-array_push/#findComment-564751 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.