allinurl Posted May 19, 2008 Share Posted May 19, 2008 I have an array that looks like the ex below, what I want to do is just get one element of its kind, for instance just get: JRA, JRB and not the rest of the repeated values. Currently Im running throughout the whole array but it is not the way I want it. $data = array( array("CRA", 'First', 790, $name), array("CRB", 'Third', 721, "Yes"), array("CRA", 'Second', 721, "Yes"), array("CRA", 'Fifth', 721, "Yes"), array("CRB", 'Fout', 721, "Yes")); $lines = count($data); for($i = 0; $i < $lines; $i++){ if(substr($data[$i][0], 0, 2) == 'CR'){ if(substr($data[$i][0], 0, 3) != substr($data[$i+1][0], 0, 3)){ $cra = '<h3>TEST ' . substr($data[$i][0],2).'</h3>'; print $cra. '<br />'; } } } Results should be only these two: TEST A TEST B Could somebody give me some hints on this? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/106360-problem-on-displaying-array-elements/ Share on other sites More sharing options...
kenrbnsn Posted May 19, 2008 Share Posted May 19, 2008 Using a foreach loop like the following seems to work: <?php $sav = array(); foreach ($data as $arys) { if(substr($arys[0], 0, 2) == 'CR') if (!in_array('TEST' . substr($arys[0],2,1),$sav)) $sav[] = 'TEST' . substr($arys[0],2,1); } echo implode("<br>\n",$sav); ?> Ken Link to comment https://forums.phpfreaks.com/topic/106360-problem-on-displaying-array-elements/#findComment-545194 Share on other sites More sharing options...
allinurl Posted May 19, 2008 Author Share Posted May 19, 2008 Thanks for ur reply. Would this be possible by not creating a new array?, I was thinking more like keeping the same values of the original array, more like sorting it and then doing the !=. So far the way I posted works for me just fine but not when the values are not sorted I was trying to sort them out but I keep getting the same values. Here it is the algorithm that Im using, when I do a print_r I got them sorted, but when they go through the loop it comes out in the same way. What Am I missing? Thanks again <?php $data = array( array("CRA", 'First', 790, $name), array("CRB", 'Third', 721, "Yes"), array("CRA", 'Second', 721, "Yes"), array("CRA", 'Fifth', 721, "Yes"), array("CRB", 'Fout', 721, "Yes")); $SortOrder=1; // desc by default , 1- asc $newdata = sortByField($data,'0',$SortOrder); $lines = count($data); print '<pre>'; print_r($newdata); // here it comes sorted good print '</pre>'; for($i = 0; $i < $lines; $i++){ if(substr($newdata[$i][0], 0, 2) == 'CR'){ if(substr($newdata[$i][0], 0, 3) != substr($newdata[$i+1][0], 0, 3)){ $cra = '<h3>JUST TEST PRINT ' . substr($newdata[$i][0],2).'</h3>'; print $cra. '<br />'; // here comes out same way, like not been sorted... } } } function sortByField($multArray,$sortField,$desc=true){ $tmpKey=''; $ResArray=array(); $maIndex=array_keys($multArray); $maSize=count($multArray)-1; for($i=0; $i < $maSize ; $i++) { $minElement=$i; $tempMin=$multArray[$maIndex[$i]][$sortField]; $tmpKey=$maIndex[$i]; for($j=$i+1; $j <= $maSize; $j++) if($multArray[$maIndex[$j]][$sortField] < $tempMin ) { $minElement=$j; $tmpKey=$maIndex[$j]; $tempMin=$multArray[$maIndex[$j]][$sortField]; } $maIndex[$minElement]=$maIndex[$i]; $maIndex[$i]=$tmpKey; } if($desc) for($j=0;$j<=$maSize;$j++) $ResArray[$maIndex[$j]]=$multArray[$maIndex[$j]]; else for($j=$maSize;$j>=0;$j--) $ResArray[$maIndex[$j]]=$multArray[$maIndex[$j]]; return $ResArray; } ?> Link to comment https://forums.phpfreaks.com/topic/106360-problem-on-displaying-array-elements/#findComment-545245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.