Jump to content

[SOLVED] array_slice : removing element in array


sohrab_ark

Recommended Posts

Hi

I'm newbie in php.

I have an array that contain some blank data.I want to remove these blank elements.

I tried to do this with array_slice, but I completely confused ???

also I should say my array every time will change.

 

any help is my appreciate

sohrab

<?php
foreach($array as $key=>$value) {
     if ($value=='') {
         unset($array[$key]);
     }
}
?>

thanks for your help

but it didn't work

<?php
$input = array("a", "b", "c", "d", "e");

foreach($array as $key=>$value) {
     if ($value=='d') {
         unset($input[$key]);
     }
}
$input=array_keys($input);
for($i0=0;$i0<count($input);$i0++)
{
echo $input[$i0]."<br/>";  // result is 0 1 2 3 4
}
  ?>

I want result be a b c e

;) thanks about $input....

but it doesn't work yet.

<?php
$input = array("a", "b", "c", "d", "e");

foreach($input as $key=>$value) {
     if ($value=='d') {
         unset($input[$key]);
     }
}
$input=array_keys($input);
for($i0=0;$i0<count($input);$i0++)
{
echo $input[$i0]." ";// result 0 1 2 4
}
?>
result is 0 1 2 4
and without [i]$input=array_keys($input);[/i] result is a b c

solved

<?php
$input = array("a", "b", "c", "d", "e");

foreach($input as $key=>$value) {
     if ($value=='d') {
         unset($input[$key]);
     }
}
$input=array_values($input);
for($i0=0;$i0<count($input);$i0++)
{
   echo $input[$i0]." ";// result 0 1 2 4
}
?>

I should use array_values($input);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.