Php - unique array values using foreach

unique array values using foreach

Page 1 of 1

12 Replies - 6611 Views - Last Post: 27 August 2009 - 01:16 AM Rate Topic: -----

#1 Chriszea101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-August 09

Php - unique array values using foreach

Post icon  Posted 24 August 2009 - 11:18 PM

Okay, so the basis of this code is to take an array of numbers/strings, use a foreach to iterate through the array, gather the unique values and spit them back out like:

1
2
3
4

<html>
<head> <title>Iterate</title>
<?php
function unique($arr) {
   $u_arr = array();
   foreach ($arr as $s) {
	  foreach ($u_arr as $us) {	
	  if ($us == $s){ 
	break;
	} 
	$us_arr[] = $s;
	sort($us_arr);
   }
}
   return $us_arr;
}
?>
</head>
<body>
<?php
$str = array(1,1,1,2,2,2,3,3,3,4,4,4);
$uStr = unique($str);
 foreach ($uStr as $u) {
print($u);
}
   
?>
</body>
</html>



My problem seems to be around the
 	} 
	$us_arr[] = $s;
	sort($us_arr);
   }


area. I HAVE to use this method to complete the task. I've found that if i just pass the array straight to print it will work, so theres something within the unique() method that is not working.

Thank you in advance!

Zea01

Is This A Good Question/Topic? 0
  • +

Replies To: Php - unique array values using foreach

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Php - unique array values using foreach

Posted 25 August 2009 - 12:28 AM

You initiate/define the array with this name "$u_arr" but you use it with this "$us_arr". Make sure you use the proper name consistently. Also instead of having the second foreach-loop you could use the native in_array()-function.

I remade your function(and it works for me):
function unique($arr)
{
	$us_arr = array();
	foreach ($arr as $s)
	{
		if( ! in_array($s, $us_arr))
		{
			$us_arr[] = $s;
			sort($us_arr);
		}
	}
	return $us_arr;
}


Hope it helps! :)
Was This Post Helpful? 1
  • +
  • -

#3 Chriszea101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-August 09

Re: Php - unique array values using foreach

Posted 25 August 2009 - 12:33 AM

Thank you very much for your help, Kudos++. That makes a lot more sense than what i was trying to do, and with less code.

When it prints on the page its all in one line, but im sure i will work that one out.

Cheers!

zea01
Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Php - unique array values using foreach

Posted 25 August 2009 - 01:10 AM

No problem, happy to help! :)

View PostChriszea101, on 25 Aug, 2009 - 09:33 AM, said:

Thank you very much for your help, Kudos++. That makes a lot more sense than what i was trying to do, and with less code.

When it prints on the page its all in one line, but im sure i will work that one out.

Cheers!

zea01

Was This Post Helpful? 0
  • +
  • -

#5 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Php - unique array values using foreach

Posted 25 August 2009 - 09:40 AM

Not to step on toes of wanting to create your own functions and all but like wimpy used a built in function why not use a built in function to replace your built function all together ... what i speak of is array_unique

in which case you could shorten your script further ....

<html>
	<head> 
		<title>Iterate</title>
	</head>
	<body>
<?php
	// our array of numbers
	$str = array(2,2,2,3,3,3,4,4,4,1,1,1);
	// order the numbers
	sort($str);
	// now the built in function removes duplicates
	$uStr = array_unique($str);
	// show the new array
	foreach ($uStr as $u) {
		echo "$u<br/>";
	}  
?>
	</body>
</html>


This post has been edited by RPGonzo: 25 August 2009 - 09:47 AM

Was This Post Helpful? 1
  • +
  • -

#6 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Php - unique array values using foreach

Posted 25 August 2009 - 11:11 AM

That's really nice RP, haven't come across that function before (hence my ignorance of its existence), thanks for taking the time to "... step on toes ..."! :)
Was This Post Helpful? 0
  • +
  • -

#7 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Php - unique array values using foreach

Posted 25 August 2009 - 11:15 AM

lol i just found it not to long ago myself so don't feel bad -_-
Was This Post Helpful? 0
  • +
  • -

#8 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Php - unique array values using foreach

Posted 25 August 2009 - 11:39 AM

Haha

View PostRPGonzo, on 25 Aug, 2009 - 08:15 PM, said:

lol i just found it not to long ago myself so don't feel bad -_-

Was This Post Helpful? 0
  • +
  • -

#9 ntbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-August 09

Re: Php - unique array values using foreach

Posted 25 August 2009 - 08:05 PM

If I wanted the function initially written by Chriszea101 to work with the nested foreach loops, how would I do it.

I have written the exact same function, which I need to keep with the nested foreach loop. But i can't seem to get it to work. No elements are being printed out.

<html>
<head> <title>Unique</title></head>
<?php

function unique($s_arr){
   $us_arr = array();   
	foreach ($s_arr as $s) {
	   foreach ($us_arr as $us) {   
		  if ($us==$s){
			 break;
		  }
		  $us_arr[] = $s;
	   } 
   }
   return $us_arr;
}
?>

<body>
<?php
$str = array('42', '24', '2', '4', '42', '24', '2', '4', '24', '42', '42', '24');
$uStr = unique($str);
foreach ($uStr as $u) {
   print($u .'<br>');  
}
?>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#10 Chriszea101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-August 09

Re: Php - unique array values using foreach

Posted 26 August 2009 - 03:34 AM

Thanks RPGonzo =), however, part of my task for this code was NOT to use the array_unique, but to basically remake the code in our own terms, hence why i did not use that in the first place :P

ntbit, thats the same problem i had, i did not have any errors in the code (well none were coming up) but it was not printing either way, and the array would stay empty. Wimpy and RPGonzo's code is much better than my original.

Cheers again!

zea01
Was This Post Helpful? 0
  • +
  • -

#11 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Php - unique array values using foreach

Posted 26 August 2009 - 11:52 AM

It does not work because the second foreach-loop doesn't have any elements to iterate through since $us_arr is empty. You should rather do something like this:
function unique($s_arr)
{
	$us_arr = array();   
	foreach($s_arr as $s)
	{
		$flag = true;
		foreach($us_arr as $us)
		{   
			if ($us==$s)
			{
				$flag = false;
				break;
			}
			
		}
		
		if($flag)
		{
			$us_arr[] = $s;
		}
	}
	return $us_arr;
}
which should work! :)

View Postntbit, on 26 Aug, 2009 - 05:05 AM, said:

If I wanted the function initially written by Chriszea101 to work with the nested foreach loops, how would I do it.

I have written the exact same function, which I need to keep with the nested foreach loop. But i can't seem to get it to work. No elements are being printed out.

<html>
<head> <title>Unique</title></head>
<?php

function unique($s_arr){
   $us_arr = array();   
	foreach ($s_arr as $s) {
	   foreach ($us_arr as $us) {   
		  if ($us==$s){
			 break;
		  }
		  $us_arr[] = $s;
	   } 
   }
   return $us_arr;
}
?>

<body>
<?php
$str = array('42', '24', '2', '4', '42', '24', '2', '4', '24', '42', '42', '24');
$uStr = unique($str);
foreach ($uStr as $u) {
   print($u .'<br>');  
}
?>
</body>
</html>


Was This Post Helpful? 1
  • +
  • -

#12 ntbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-August 09

Re: Php - unique array values using foreach

Posted 26 August 2009 - 05:35 PM

Thanks a lot Wimpy. So simple. It's been one of those weeks :)

View PostWimpy, on 26 Aug, 2009 - 10:52 AM, said:

It does not work because the second foreach-loop doesn't have any elements to iterate through since $us_arr is empty. You should rather do something like this:
function unique($s_arr)
{
	$us_arr = array();   
	foreach($s_arr as $s)
	{
		$flag = true;
		foreach($us_arr as $us)
		{   
			if ($us==$s)
			{
				$flag = false;
				break;
			}
			
		}
		
		if($flag)
		{
			$us_arr[] = $s;
		}
	}
	return $us_arr;
}
which should work! :)

View Postntbit, on 26 Aug, 2009 - 05:05 AM, said:

If I wanted the function initially written by Chriszea101 to work with the nested foreach loops, how would I do it.

I have written the exact same function, which I need to keep with the nested foreach loop. But i can't seem to get it to work. No elements are being printed out.

<html>
<head> <title>Unique</title></head>
<?php

function unique($s_arr){
   $us_arr = array();   
	foreach ($s_arr as $s) {
	   foreach ($us_arr as $us) {   
		  if ($us==$s){
			 break;
		  }
		  $us_arr[] = $s;
	   } 
   }
   return $us_arr;
}
?>

<body>
<?php
$str = array('42', '24', '2', '4', '42', '24', '2', '4', '24', '42', '42', '24');
$uStr = unique($str);
foreach ($uStr as $u) {
   print($u .'<br>');  
}
?>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#13 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Php - unique array values using foreach

Posted 27 August 2009 - 01:16 AM

No problem, happy to help! :)

View Postntbit, on 27 Aug, 2009 - 02:35 AM, said:

Thanks a lot Wimpy. So simple. It's been one of those weeks :)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1