views:

86

answers:

5

I'd like to be able to extract some array elements, assign each of them to a variable and then unset these elements in the array.

Let's say I have

$myarray = array ( "one" => "eins", "two" => "zwei" , "three" => "drei") ;

I want a function suck("one",$myarray)as a result the same as if I did manually:

$one = "eins" ;
unset($myarray["one"]) ;

(I want to be able to use this function in a loop over another array that contains the names of the elements to be removed, $removethese = array("one","three") )

function suck($x, $arr) {
$x = $arr[$x] ;
unset($arr[$x]) ;
}

but this doesn't work. I think I have two prolbems -- how to say "$x" as the variable to be assigned to, and of function scope. In any case, if I do

suck("two",$myarray) ;

$two is not created and $myarray is unchanged.

+1  A: 

You can do

function suck($x, $arr) {
    $$x = $arr[$x] ;
    unset($arr[$x]) ;
}

, using variable variables. This will only set the new variable inside the scope of "suck()".

You can also have a look at extract()

gnud
Stefan Gehrig
No - it won't work outside the scope of the function either way.
gnud
No, but at least it would remove the element from the array. By using `global $$x' as cletus suggests you can merge the global with the local scope for the variable.
Stefan Gehrig
+2  A: 

I'd build an new array with only the key => value pairs you want, and then toss it at extract().

Amber
+3  A: 

Try this:

$myarray = array("one" => "eins", "two" => "zwei" , "three" => "drei");

suck('two', $myarray);
print_r($myarray);
echo $two;

function suck($x, &$arr) {
  global $$x;
  $$x = $arr[$x];
  unset($arr[$x]);
}

Output:

Array
(
    [one] => eins
    [three] => drei
)
zwei
cletus
A: 

Why not this:

foreach ($myarray as $var => $val) {
    $$var = $val;
    unset($myarray[$var]);
    echo "$var => ".$$var . "\n";
}

Output

one => eins
two => zwei
three => drei
Peter Lindqvist
A: 

If I've understood the question, you have two problems

The first is that you're setting the value of $x to be the value in the key-value pair. Then you're unsetting a key that doesn't exist. Finally, you're not returning anything. Here's what I mean:

Given the single element array $arr= array("one" => "eins") and your function suck() this is what happens:

First you call suck("one", $arr). The value of $x is then changed to "eins" in the line $x=$arr[$x]. Then you try to unset $x (which is invalid because you don't have an array entry with the key "eins"

You should do this:

function suck($x, $arr)
{
$tmp = $arr[$x];
unset($arr[$x]);
return $tmp
}

Then you can call this function to get the values (and remove the pair from the array) however you want. Example:

<?php
/* gets odd numbers in german from
$translateArray = array("one"=>"eins", "two"=>"zwei", "three"=>"drei");
$oddArray = array();

$oddArray[] = suck($translateArray,"one");
$oddArray[] = suck($translateArray, "three");

?>

The result of this is the array called translate array being an array with elements("eins","drei");

HTH JB

James Booker