4 Replies - 2833 Views - Last Post: 30 April 2009 - 09:58 AM Rate Topic: -----

#1 skirkpatrick  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-February 08

Delete null value in an array

Posted 29 April 2009 - 09:44 AM

I have an array that I am trying to get only the unique numbers out of and then create another array without the null values.

80374
81374
79374
79374
79374
81374
87374
79374
79374

using
$result = array_unique($kind);


I get:
80374
81374
79374



87374


But the non unique items are now Null or blank but still fill the array.

How can I create a new array with only the unique numbers??

I know I am being braindead but what can you do???
Any help is greatly appeciated.

Steve

Is This A Good Question/Topic? 0
  • +

Replies To: Delete null value in an array

#2 RPGonzo  Icon User is offline

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

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

Re: Delete null value in an array

Posted 29 April 2009 - 10:39 AM

Might be a cleaner way but here is what i did :

$yours = array(80374,81374,79374,79374,79374,81374,87374,79374,79374);
	$uni = array_unique($yours);
	
	echo '<br/><br/>Yours Dirty = <br/>';
	
	foreach ($yours as $val) {
		echo $val.' ____ ';
	}
	
	$clean = Clean_Array($uni);
	
	echo '<br/><br/>Yours Cleaned = <br/>';
	
	foreach ($clean as $val) {
		echo $val.' ____ ';
	}
	
	$test = array(1,2,3,4,'','','','',7,12,'',423);
	
	echo '<br/><br/>Dirty = <br/>';
	foreach ($test as $val) {
		echo $val.' ____ ';
	}
	
	echo '<br/><br/>Test Cleaned = <br/>';
	$cleaned = Clean_Array($test);
	
	foreach ($cleaned as $val) {
		echo $val.' ____ ';
	}
	
	function Clean_Array($array){
		foreach ($array as $val) { 
			if (!empty($val) || !isset($val) || $val != '') {
				$clean[] = $val;
			}
		}
		return $clean;
	}



EDIT: i added your array in there as well as the array_unique.

This post has been edited by RPGonzo: 29 April 2009 - 11:05 AM

Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Delete null value in an array

Posted 29 April 2009 - 11:12 AM

No need to create a function. Just use array_filter:
<?php

$yours = array(80374,81374,79374,79374,79374,81374,87374,79374,79374);
	$uni = array_unique($yours);
	
	echo '<br/><br/>Yours Dirty = <br/>';
	
	foreach ($yours as $val) {
		echo $val.' ____ ';
	}
	
	$clean = array_filter($uni);
	
	echo '<br/><br/>Yours Cleaned = <br/>';
	
	foreach ($clean as $val) {
		echo $val.' ____ ';
	}
	
	$test = array(1,2,3,4,'','','','',7,12,'',423);
	
	echo '<br/><br/>Dirty = <br/>';
	foreach ($test as $val) {
		echo $val.' ____ ';
	}
	
	echo '<br/><br/>Test Cleaned = <br/>';
	$cleaned = array_filter($test);
	foreach ($cleaned as $val) {
		echo $val.' ____ ';
	}
?>

Was This Post Helpful? 0
  • +
  • -

#4 skirkpatrick  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-February 08

Re: Delete null value in an array

Posted 30 April 2009 - 09:56 AM

View PostCTphpnwb, on 29 Apr, 2009 - 10:12 AM, said:

No need to create a function. Just use array_filter:
<?php

$yours = array(80374,81374,79374,79374,79374,81374,87374,79374,79374);
	$uni = array_unique($yours);
	
	echo '<br/><br/>Yours Dirty = <br/>';
	
	foreach ($yours as $val) {
		echo $val.' ____ ';
	}
	
	$clean = array_filter($uni);
	
	echo '<br/><br/>Yours Cleaned = <br/>';
	
	foreach ($clean as $val) {
		echo $val.' ____ ';
	}
	
	$test = array(1,2,3,4,'','','','',7,12,'',423);
	
	echo '<br/><br/>Dirty = <br/>';
	foreach ($test as $val) {
		echo $val.' ____ ';
	}
	
	echo '<br/><br/>Test Cleaned = <br/>';
	$cleaned = array_filter($test);
	foreach ($cleaned as $val) {
		echo $val.' ____ ';
	}
?>

Even better
Thank you
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: Delete null value in an array

Posted 30 April 2009 - 09:58 AM

I knew there was a cleaner way i just couldn't think of it :D glad someone refreshed my memory as well :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1