To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 07-23-2003, 08:35 AM   #1
Dollar
Junior Member
 
Join Date: Jul 2003
Posts: 13
how to remove empty element from array

HI I got so many empty element in array( contain only whitespace). anyone know how to remove it?
$my[0] =
$my[1] = 5.21
$my[2] = 425
$my[3] =
$my[4] =
$my[5] = 75

I want to remove 0,3,4,5. I did try several way but really get stuck. Anyone help me please.
Dollar is offline   Reply With Quote
Old 07-23-2003, 09:21 AM   #2
BigBadKev
Member
 
Join Date: Jun 2003
Location: New Zealand
Posts: 96
Hi Dollar,

This is my first time replying so be gentle, try using the unset function like this
PHP Code:
$my = array();
$my[0] = null;
$my[1] = 5.21;
$my[2] = 425;
$my[3] = null;
$my[4] = null;
$my[5] = 75;
foreach (
$my as $key => $value) {
  if (
is_null($value)) {
    unset(
$my[$key]);
  }
}
foreach (
$my as $key => $value) {
  echo   
"  $key : $value <br>";
}
//....out put.....
1 : 5.21
2
: 425
5
: 75

//but if you want to re-order the array lets try this, only draw back
//is we are creating a new array
$newMy = array();
$i = 0;
foreach (
$my as $key => $value) {
  if (!
is_null($value)) {
    
$newMy[$i] = $value;
    
$i++;
  }
}
foreach (
$newMy as $key => $value) {
  echo   
"  $key : $value <br>";
}
//....out put....
0 : 5.21
1
: 425
2
: 75
I really hope this helps you a little, I actually would like to know how we keep the same array if its possible?

BBK
BigBadKev is offline   Reply With Quote
Old 07-23-2003, 12:06 PM   #3
dopp
Member
 
Join Date: May 2003
Posts: 31
PHP Code:
foreach($array as $key => $value) {
  if(
$value == "") {
    unset(
$array[$key]);
  }
}
$new_array = array_values($array);
and voila, $new_array should only contain entries that are not empty & the indexes of the array are reordered so that they are not missing indexes!

Last edited by dopp; 07-23-2003 at 12:10 PM.
dopp is offline   Reply With Quote
Old 07-23-2003, 01:14 PM   #4
Dollar
Junior Member
 
Join Date: Jul 2003
Posts: 13
Thank you so much, you guys. It works very well. thank you..Dollar
Dollar is offline   Reply With Quote
Old 07-23-2003, 07:34 PM   #5
BigBadKev
Member
 
Join Date: Jun 2003
Location: New Zealand
Posts: 96
Hi dollar,

just be very careful with "" and null as they are two different things, eg look at this array below
PHP Code:
$my = array();
$my[0] = null;
$my[1] = 5.21;
$my[2] = 425;
$my[3] = null;
$my[4] = "";
$my[5] = 75;

//note element [4] is empty not a null value

$newMy = array();
$i = 0;
foreach (
$my as $key => $value) {
  if (!
is_null($value)) {
    
$newMy[$i] = $value;
    
$i++;
  }
}
foreach (
$newMy as $key => $value) {
  echo   
"  $key : $value <br>";
}
//....out put ....
0 : 5.21
1
: 425
2
:
3 : 75

/*
so we still have a empty element at [2]
but if we change the line below
if (!is_null($value)) {
with this one
if ((!is_null($value)) && ($value !== "")) {
our out put is this
0 : 5.21
1 : 425
2 : 75

*/
whats important here is that null and "" are treated as two different values, not sure if you knew this.

Have fun

BBK
BigBadKev is offline   Reply With Quote
Old 11-07-2007, 06:04 AM   #6
KingIsulgard
Junior Member
 
Join Date: Nov 2007
Posts: 1
[MOD]Before anyone else replies, please note that this is an over 4-year-old thread. -- NogDog[/MOD]

If you also want to delete values which are not empty but equal to "" then just use this foreach:
PHP Code:
    foreach ($myarr as $key => $value) {
      if (
is_null($value) || $value=="") {
        unset(
$myarr[$key]);
      }
    }
By adding or $value=="" to the if function he will also remove the empty strings .

Last edited by NogDog; 11-07-2007 at 08:38 AM. Reason: Old thread warning
KingIsulgard is offline   Reply With Quote
Old 11-21-2007, 12:36 AM   #7
raahool_16
Junior Member
 
Join Date: Nov 2007
Posts: 1
resolved its easy

TRY THIS---

array_filter($my);
raahool_16 is offline   Reply With Quote
Old 05-24-2008, 05:36 AM   #8
saami123
Junior Member
 
Join Date: May 2008
Posts: 1
can remove the empty values in array

[MOD]Before replying to this thread, note the NogDog's edit made about half a year ago. -- Weedpacket[/MOD]

Here remove the empty values and arraged the array in correct order


$ii=0;
foreach($list as $key_null=>$val_null){
if($val_null){
$list1[$ii]=$val_null;$ii++;
}
}

by saami

[MOD]Besides, array_values(array_filter($list)).[/MOD]

Last edited by Weedpacket; 05-25-2008 at 01:03 AM. Reason: Old thread warning
saami123 is offline   Reply With Quote
Old 06-08-2008, 01:40 PM   #9
Shane10101
Junior Member
 
Join Date: Jun 2008
Posts: 2
Smile php to English translation, please :)

Hey gang,

Could I get someone to explain what this line is doing. I'm learning php fast, but still have a ways to go!

" $key => $value "

Here's the context (from this tread, c. 2003):

foreach ($thisArray as $key => $value) {
if (is_null($value) || $value == "") {
unset ($thisArray[$key]);
}
}

The part I'm having trouble understanding is the " => " I don't see it listed as an operator in the php list on devguru.com (my hero, btw). Is this just a way of drilling-down into an associative array?

Also, why is it necessary to act on "$thisArray" as "$key"? Why not just look directly at $thisArray?

Thanks in advance for any help!

Newbie
Shane10101 is offline   Reply With Quote
Old 06-09-2008, 05:42 AM   #10
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 20,031
Quote:
Originally Posted by Shane10101
Is this just a way of drilling-down into an associative array?
Yes. $key isn't the same thing as $thisArray. It's one key from the array. The page you want to look at is the one in the PHP manual on foreach.
__________________
THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
Weedpacket is offline   Reply With Quote
Old 06-09-2008, 09:07 AM   #11
Shane10101
Junior Member
 
Join Date: Jun 2008
Posts: 2
Thanks!

I'm still stuck on the "=>" part, but that helps.
Shane10101 is offline   Reply With Quote
Old 06-09-2008, 12:03 PM   #12
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 13,058
Quote:
I'm still stuck on the "=>" part
It is just array syntax to denote key => value. As such, you see it used for foreach loops, and when initialising an associative array:
PHP Code:
$array = array('key1' => 'value1', 'key2' => 'value2');
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 06-16-2008, 03:33 AM   #13
isam4m
Junior Member
 
Join Date: Jun 2008
Posts: 1
try
array_multisort($array);
isam4m is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 02:34 AM.








Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.