get value and unset variable

[php]$test = ‘teeeessst’;
$zm = ‘test’;

echo ‘1-value: ’ . $$zm.’
’;
unset($$zm);
echo ‘2-value: ’ . $$zm.’
’;[/php]

return :
1-value: teeeessst
2-value:
working, but:

$za[1] - teeest-1

[php]$za = array(‘1’=>‘teeest-1’, ‘2’=>‘teeest-2’);

$zm = ‘za[1]’;

echo ‘1-value: ’ . print_r($$zm,1).’
’;
unset($$zm);
echo ‘2-value: ’ . print_r($$zm,2).’
’;[/php]

return:
1-value:
2-value:
so doesn’t work

How can I get to array?

[php]<?php

//http://us1.php.net/manual/en/book.array.php —> I suggest you go here to learn about arrays.

$za = array(‘1’ => ‘teeest-1’, ‘2’ => ‘teeest-2’);

$zm = $za[1];

echo '1-value: ’ . print_r($za, 1) . ‘
’;
unset($za[2]);
echo '2-value: ’ . print_r($za, 2) . ‘
’;
[/php]

Well from my understanding, I think you can’t use an array key directly in the string. This would cause the PHP to look for a variable za[1], hence you get an undefined variable error. You should use the array the name only and then add the index key afterwards. You can do the following:

[php]<?php
$za = array(‘1’=>‘teeest-1’, ‘2’=>‘teeest-2’);

$zm = ‘za’;

echo ‘1-value: ’ . ${$zm}[1].’
’;
unset($$zm);
echo ‘2-value: ’ . ${$zm}[2].’
’;
?>[/php]

There are better ways of doing this than using unset, such as using http://us2.php.net/manual/en/function.array-pop.php for example. I still say brushing up on arrays is the only way to learn and I’ll go even one further reading up on the php language (or any programming language) is the BEST way to go. Get a current book or current tutorial is my advice.

[php]<?php

//http://us1.php.net/manual/en/book.array.php —> I suggest you go here to learn about arrays.

$za = array(‘1’ => ‘teeest-1’, ‘2’ => ‘teeest-2’);

$zm = $za[1];

echo '$za array start: ’ . print_r($za, 1) . ‘
’;
//unset($za[2]);
$last = array_pop($za);

echo '$za array after pop: ’ . print_r($za, 2) . ‘
’;
echo '$last value of array $za: ’ . print_r($last, 2) . ‘
’;[/php]

To me unset by reference gets to confusing, but that is just my opinion. Specially when you can manipulate arrays very easily will all different type of function calls php offers. :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service