problem with php algorithm

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ugd
Forum Newbie
Posts: 1
Joined: Thu Dec 29, 2011 5:54 am

problem with php algorithm

Post by ugd »

Code: Select all

foreach($values as $val =>$array) {
    //echo $val . '<br/>';
   //var_dump($array);

   if(is_array($array))
   {
   var_dump($array);
   }
   else
   {
   echo $val;
   }
   }
I can't find a way to read variables in URL or in a vector or in text form so that it return just
the value which is reapeating,in case it is only one value to display it or in case there are only
different values to display all of them.Please help me!It's pretty urgent.
For example if I have 1,2,3,1,4 i would like it to display 1,and if I have 1,2,3,4 to display all of them.
Need help please :(
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: problem with php algorithm

Post by Christopher »

You might want to look at array_unique() or similar funcitons: http://us.php.net/manual/en/function.array-unique.php
(#10850)
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: problem with php algorithm

Post by twinedev »

Is this something you are looking for?

Code: Select all

<pre><tt><?php

	// SET UP RANDOM ARRAY OF DATA
	$aryData = array();
	for($t=0;$t<20;$t++) { $aryData[] = rand(1,20); }

	// sort and output original array
	sort($aryData); print_r($aryData);

	// BEGIN: CODE TO FIND DUPLICATE ENTRIES
		$aryCount = array_count_values($aryData);
		foreach($aryCount as $value=>$count) {
			if ($count==1) { unset($aryCount[$value]); }
		}
		$aryData = array_keys($aryCount);
		unset($aryCount);
	// END: CODE TO FIND DUPLICATE ENTRIES

	// Sort and output array reduced to just duplicates
	sort($aryData); print_r($aryData);

?></tt></pre>
Post Reply