Current location: Hot Scripts Forums » Programming Languages » PHP » Make A New Array From An Old Array (Excluding 1 Array Element)


Make A New Array From An Old Array (Excluding 1 Array Element)

Reply
  #1 (permalink)  
Old 08-06-07, 01:56 PM
w2n's Avatar
w2n w2n is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Question Make A New Array From An Old Array (Excluding 1 Array Element)

Hi,

What I am trying to achieve is...

• I have an array of web site names (like, siteA, siteB, siteC, siteD, siteE, etc), for example.

• I have a variable, like $site = "siteC"

• Now, the function will create a NEW array from the WHOLE contents of the old array, and only EXCLUDE the variable array element from the old array element list.


For example usage...

"newArray($site)" function will print all the array elements of "oldArray()", and will NOT include the element of $site (that is 'siteC' in this example). So, the new array will contain siteA, siteB, siteD, siteE, etc.


I don't know, how to exclude an element from a particular array. That's why, I am not being able to make this simple function.

Please help! Thanks in advance!

__________________
SWAGATO GANGOPADHYAY
Founder/Owner/CEO
The Rozaleenda Group, Inc.


Reply With Quote
  #2 (permalink)  
Old 08-06-07, 05:39 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
PHP Code:

$new_array $old_array;
unset(
$new_array[array_search($site$new_array)]); 
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 08-06-07, 05:43 PM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

$array = array('1','2','3','4','5');

function 
removeValue($string$array)
    {
    foreach(
$array as $key => $value)
        {
        if(
stripos($value$string) !== false)
            {
            unset(
$array[$key]);
            }
        }
    return 
$array;
    }
$new_array removeValue('2'$array); 
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Reply With Quote
  #4 (permalink)  
Old 08-07-07, 12:36 AM
w2n's Avatar
w2n w2n is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks both of you, NeverMind and nova912. But, I am confused as which one to choose? Both of these codes will produce the exact same result? My array will have the web site names, not the numbers only. Please let me know, which one is better, faster and easier? I guess, the first one.
__________________
SWAGATO GANGOPADHYAY
Founder/Owner/CEO
The Rozaleenda Group, Inc.


Reply With Quote
  #5 (permalink)  
Old 08-07-07, 04:25 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
The problem with Nova's code is, if there are values in the array like "site", "site2", etc.. and you want to remove "site"... it'd remove site2 as well, because stripos() wouldn't return false. So a mix of both solutions should work:
PHP Code:

function array_remove_val($array$value)

{
    unset(
$array[array_search($value$array)]);
    return 
$array;
}

// Example:

$array = array(1'foo'2'bar''one''two');

$new_array array_remove_val($array'bar');

echo 
'<pre>' print_r($new_arraytrue) . '</pre>'
Reply With Quote
  #6 (permalink)  
Old 08-07-07, 04:30 AM
w2n's Avatar
w2n w2n is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Nico,

Thanks for the detailed help! I was about to use the first code, done by NeverMind, since it's very simple! Do you think, it will show wrong output, if the elements are different in looks, as you have said, like 'site' and 'siteB'? You said it for nova912's code, but didn't say about NeverMind one. Please let me know.

Thanks once again!

__________________
SWAGATO GANGOPADHYAY
Founder/Owner/CEO
The Rozaleenda Group, Inc.


Reply With Quote
  #7 (permalink)  
Old 08-07-07, 04:36 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
NeverMind's code should work just fine. It would remove the exact value from the array.
Reply With Quote
  #8 (permalink)  
Old 08-07-07, 05:11 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Sorry I couldn't elaborate more. I clicked on the edit button to explain but my laptop died on me
Anyway, the code provided by me, enhanced by Nico, will do case-sensitive search and if the value you are searching for doesn't exist, it will not notify you but rather a php notice will generated. So a final enhanced version would be:
PHP Code:

function array_remove_val($array$value)
{
    if (!
in_array($value$array))
       return 
false;

    unset(
$array[array_search($value$array)]);
    return 
$array;
}

// Usage:

$array = array(1'foo'2'bar''one''two');

$new_array array_remove_val($array'bar');

if (
$new_array)
    echo 
'<pre>' print_r($new_arraytrue) . '</pre>'
or if you want to return the old array instead of false in case the value wasn't found, replace "return false;" with "return $array;"
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #9 (permalink)  
Old 08-08-07, 12:35 AM
w2n's Avatar
w2n w2n is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks everyone here! I have tested the final code and it's working like a charm! Cheers to PHP and PHP developers!
__________________
SWAGATO GANGOPADHYAY
Founder/Owner/CEO
The Rozaleenda Group, Inc.


Reply With Quote
  #10 (permalink)  
Old 08-08-07, 05:07 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
You are welcome
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
Accessing PHP array in HTML to make radio buttons Newbie2005 PHP 35 10-20-05 01:32 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM
move array element up or down Perry JavaScript 4 09-27-03 04:23 PM


All times are GMT -5. The time now is 06:08 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.