Honesty Rocks! truth rules.

Remove A Value From A Php Array Based On Its Value

HOME      >>       Programming

sonesay

I have an index array

$array[0] = 10;$array[1] = 12;...

Its kinda late and I'm falling asleep now but I had a quick look through PHP.net, and could not find what I'm after, Maybe I'm too sleepy, sorry, but if anyone knows off the top of their head a way to removed members in that array based on its value, i.e. I wanna remove values 12. I don't really want to do a loop but if I cant find any existing function to do it, then I may have to.

Thanks for any help.

truefusion

You may have to make your own function or set up for this. But i did a quicky function that should work fine for your needs:

function remove_element($arr, $val){foreach ($arr as $key => $value){if ($arr[$key] == $val){unset($arr[$key]);}}return $arr = array_values($arr);}
You can rename the function if you want.

sonesay

Thanks again for the help Truefusion. Its too bad PHP does not have a array function for this specific task itself, they seem to have many other functions for other kinds of array modifications.


iGuest

thanks

Remove A Value From A Php Array Based On Its Value

 

Replying to truefusion

This is really good... I needed a script to find the only file in a folder, but it returned '.' and '..' and then the file... Now I just do this:

<?php

$return = delete_value($return, '.');

$return = delete_value($return, '..');

?>

Thanks...

 

 

-reply by php-freak!


iGuest

strip element from array()

Remove A Value From A Php Array Based On Its Value

 

Replying to truefusion

Is there away to chop an array depending on the value of a sub-array?

 

I.E:

$array[0] = 10;

$array[1] = 12;

$allcontent = array("page1", "page2", array("page3a", "page3b"), "page4", "page5");

 

If my function finds "page3b" I want it to end the parent array at "page2"/

I.E.:

$allcontent = array("page1", "page2");

 

If this can't be done how about just removing the sub array?

I.E.: $allcontent = array("page1", "page2", "page4", "page5");

 

 

-reply by Scoutman57


iGuest

Replying to sonesayA. $key = array_search()B. Unset($key)

iGuest

PHP 5 has a great function in 'scandir()'

however, to counter the problem noted with '.' and '..' becoming array values, if I simply unset() them, it left array keys with a null value.  so I built the array manually instead of using scandir() like so;

$awards_array = array();$dp = opendir ('dir_location');       While ( $item = readdir($dp) ){         if ( $item != '.' && $item != '..' )         {           $awards_array[] = $item;         }

}

Replying to FeedBacker-reply by Ramsonne

iGuest

pls help meRemove A Value From A Php Array Based On Its Value

hi there... Have been trying to create a 2D array for the Vignere cipher but still I encounter so many errors... Can anyone help... Here is the code have typed so far. 

String keyword = "VIGNERE";

char[] keychar = keyword.ToCharArray();

int lenght = keychar.Length;

char  [] alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','and','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

int len = alphabet.Length;

char [][]vc =new char [lenght][len];

for(int keylenght=0; keylenght<vc.Length; keylenght++){

for(int alpha=0; alpha<vc[keylenght].Length; alpha++){

if(vc[lenght][len]==keychar[lenght]){

vc[lenght][len] =alphabet[len];

vc[lenght][len]++;

System.Err.Print(vc[lenght][len]);

 

}

}

}

-reply by rolly

iGuest

you can use array_unshift to push it to the front of the array and then remove with array_shift the first value


rubikcode

You can try unsetting the values that you want to remove.

<?php$array[0] = 10;$array[1] = 12;echo 'At first: ' . $array[0] . ', ' . $array[1];unset($array[1]);echo 'After: ' . $array[0] . ' ' . $array[1] . 'only!';?>


iGuest

Remove A Value From A Php Array Based On Its ValueRemove A Value From A Php Array Based On Its Value

hi,

Why don't do this :) : ?

$search_array = array('gif', 'jpg', 'png');$item = 'png';$rez= removeItemFromArray($search_array,$item);var_dump($rez);function removeItemFromArray($list, $item) {  return  array_diff($list, (array)$item);}

Fab

-reply by __fabrice