Jump to content


Photo

unset by assigning value?


  • Please log in to reply
2 replies to this topic

#1 seanlim

seanlim
  • Members
  • PipPipPip
  • Advanced Member
  • 355 posts
  • LocationLondon, United Kingdom

Posted 03 June 2006 - 09:32 AM

Hi all,

Just need some help with the handling of an array.

My code is something like that after simplifying it quite abit.

<?
function getValue($str){
if($str == "Value 2") return ""; // how to unset variable?
else return $str." - Test String";
}

$arrayname = array();
$arrayname[] = getValue("Value 1");
$arrayname[] = getValue("Value 2");
$arrayname[] = getValue("Value 3");

print_r($arrayname);
?>

The problem is in the 3rd line 'return "";'.

I want to be able to unset the value when getValue("Value 2") is called. I have tried returning NULL, ""[blank], false.. all doesn't (and shouldn't, in the first place) work.

In other words, what i get now is
Array
(
    [0] => Value 1 - Test String
    [1] => 
    [2] => Value 3 - Test String
)

what i want is
Array
(
    [0] => Value 1 - Test String
    [1] => Value 3 - Test String
)

Is this possible at all? the function unset would be able to do this, but i can't think of any way to insert the unset function here.

Any help would be much appreciated.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 June 2006 - 01:05 PM

What you want to do cannot be done in one function. Also, when unsetting an element in an array, the indices are not compressed as this example shows:
<?php
$test = array(0,1,2,3,4,5,6,7,8);
echo '<pre> Before unset: ' . print_r($test,true) . '</pre>';
unset($test[4]);
echo '<pre> After   unset: ' . print_r($test,true) . '</pre>';
?>

Here's how I would solve your problem:
<?php
function getValue($str){
if($str == "Value 2") return false;
return $str." - Test String";
}

$arrayname = array();
$arrayname[] = getValue("Value 1");
$arrayname[] = getValue("Value 2");
$arrayname[] = getValue("Value 3");

echo '<pre> Before: ' . var_export($arrayname,true) . '</pre>';

$tmp = array();
foreach($arrayname as $v)
     if ($v !== false) $tmp[] = $v;

$arrayname = $tmp;

echo '<pre>  After: ' . var_export($arrayname,true) . '</pre>';
?>

The "!==" operator in the "if" statement is not a typo. It makes sure that the "if" will only succeed if the value is the boolean "false". It will not succeed if the value is a numeric zero.

Ken

#3 seanlim

seanlim
  • Members
  • PipPipPip
  • Advanced Member
  • 355 posts
  • LocationLondon, United Kingdom

Posted 03 June 2006 - 03:46 PM

Ok, that's good. Your script work very well.

Come to think of it, its quite a simple solution, Just couldn't think of this solution somehow..

I changed the code slightly and the ending now looks like this

while($pnt = array_search(false, $arrayname))
    unset($arrayname[$pnt]);
$arrayname = array_values($arrayname);

Its working fine now. Thanks alot kenrbnsn!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users