Reham Fahmy
Reham Fahmy

Reputation: 5063

PHP: How to replace last X elements in array

I have an array as follow

$myArr = array("red", "green", "blue", "yellow");

and want to replace the last X (number of its elements) of it with the word HIDDEN

for example, if I want to replace the last 2 elements then

foreach ($myArr as $color){
echo $color . '<br />';
}

it should be like this way

red
green
HIDDEN
HIDDEN

I'm thinking about function with three arguments

function hide_elm($array, $howmany = 0, $hide_msg = 'HIDDEN'){
}

but can not handle the array to do so.

Upvotes: 2

Views: 141

Answers (4)

Noman Shaikh
Noman Shaikh

Reputation: 170

what about this

function hide_elm($array, $howmany = 0, $hide_msg = 'HIDDEN'){
$start=sizeof($array)-$howmany;
for($i=$start;$i<sizeof($array);$i++){
$array[$i]=$hide_msg;
}
return $array;
}

Upvotes: 2

rjdown
rjdown

Reputation: 9227

Here's another option.

function hide_elm($array, $howMany = 0, $hide_msg = 'HIDDEN')
{
    // remove elements
    $array = array_slice($array, 0, -$howMany);
    // add them back
    for ($i = 0; $i < $howMany; $i++) {
        $array[] = $hide_msg;
    }
    return $array;
}

Upvotes: 2

Majed Badawi
Majed Badawi

Reputation: 28424

You can iterate reversely through the array and change the values until reaching the specified $howmany:

function hide_elm($array, $howmany = 0, $hide_msg = 'HIDDEN'){
     $len = count($array);
     if($howmany < 0 || $howmany > $len) return $array;
     $result = array_merge(array(), $array);
     $count = 0;
     for($i = $len-1; $count != $howmany; $i--) {
          $result[$i] = $hide_msg;
          $count++;
     }
     return $result;
}

$myArr = array("red", "green", "blue", "yellow");
print_r( hide_elm($myArr, 2, 'HIDDEN') );
echo "<br>";
print_r( hide_elm($myArr, 3, 'HIDDEN') );
echo "<br>";
print_r( hide_elm($myArr, 4, 'HIDDEN') );
echo "<br>";
print_r( hide_elm($myArr, -1, 'HIDDEN') );
echo "<br>";
print_r( hide_elm($myArr, 5, 'HIDDEN') );

Output:

Array ( [0] => red [1] => green [2] => HIDDEN [3] => HIDDEN )
Array ( [0] => red [1] => HIDDEN [2] => HIDDEN [3] => HIDDEN )
Array ( [0] => HIDDEN [1] => HIDDEN [2] => HIDDEN [3] => HIDDEN )
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
Array ( [0] => red [1] => green [2] => blue [3] => yellow )

Upvotes: 1

azibom
azibom

Reputation: 1944

function hide_elm($array, $howMany = 0, $hide_msg = 'HIDDEN'){
    $elementCounter = 0;
    for ($i=0; $i < $howMany; $i++) { 
        $array[count($array) - $elementCounter - 1] = $hide_msg;
        $elementCounter ++;
    }

    return $array;
}

example

$myArr = array("red", "green", "blue", "yellow");
print_r(hide_elm($myArr, 3));


function hide_elm($array, $howMany = 0, $hide_msg = 'HIDDEN'){
    $elementCounter = 0;
    for ($i=0; $i < $howMany; $i++) { 
        $array[count($array) - $elementCounter - 1] = $hide_msg;
        $elementCounter ++;
    }

    return $array;
}

result

Array
(
    [0] => red
    [1] => HIDDEN
    [2] => HIDDEN
    [3] => HIDDEN
)

Upvotes: 1

Related Questions