tags:

views:

202

answers:

8

Do I really have to do this to reset an array ??

foreach ($array as $i => $value) {
    unset($array[$i]);
}

EDIT:

this one makes more sense, as the previous one is equivalent to $array=array();

foreach ($array as $i => $value) {
    $array[$i]=NULL;
}
A: 

unset would delete the key, You need to set the value to null or 0 as per your requirement.

Example

Sairam Kunala
+1  A: 

There is no build-in function to reset an array to just it's keys.

An alternative would be via a callback and array_map():

$array = array( 'a' => 'foo', 'b' => 'bar', 'c' => 'baz' );

With regular callback function

function nullify() {}
$array = array_map('nullify', $array);

Or with a lambda with PHP < 5.3

$array = array_map(create_function('', ''), $array);

Or with lambda as of PHP 5.3

$array = array_map(function() {}, $array);

In all cases var_dump($array); outputs:

array(3) {
  ["a"]=> NULL
  ["b"]=> NULL
  ["c"]=> NULL
}
Gordon
Yup, that looks good. +1
Mr-sk
actually, you don't need to define any function body at all, array_map(create_function('', ''), $array); would work too
stereofrog
@stereofrog Right. I've changed the functions. Not that it is wrong with body and argument, but why add clutter... Thanks.
Gordon
A: 

Just do this:

$arrayWithKeysOnly = array_keys($array);

http://php.net/manual/en/function.array-keys.php

EDIT: Addressing comment:

Ok, then do this:

$arrayWithKeysProper = array_flip(array_keys($array));

http://www.php.net/manual/en/function.array-flip.php

EDIT: Actually thinking about it, that probably won't work either.

Mr-sk
This would return an array with keys being the values and is not what the OP wants. He wants to keep the keys and just purge the values
Gordon
`array_flip` won't do either, because the array values would be ascending then, e.g. 'a' => 0, 'b' => 1 and so on
Gordon
yeah, I'd say you gotta walk it.
Mr-sk
+4  A: 
$keys = array_keys($array);
$values = array_fill(0, count($keys), null);
$new_array = array_combine($keys, $values);

Get the Keys

Get an array of nulls with the same number of elements

Combine them, using keys and the keys, and the nulls as the values

As comments suggest, this is easy as of PHP 5.2 with array_fill_keys

$new_array = array_fill_keys(array_keys($array), null);
Chacha102
+1, these functions come in handy quite often. As of PHP 5.2 there is even `array_fill_keys()`.
Inshallah
+1  A: 

I don't get the question quite well, but your example

foreach ($array as $i => $value) {
    unset($array[$i]);
}

is equivilent to

$array = array();
Emil Ivanov
Yes, that's true, The keys get deleted also...I wanted a built-in function to do this : foreach ($array as $i => $value) { $array[$i]=NULL; }
jfoucher
+1  A: 
foreach($a as &$v)
   $v = null;

The reasoning behind setting an array item to null is that an array needs to have a value for each key, otherwise a key makes no sense. That is why it is called a key - it is used to access a value. A null value seems like a reasonable choice here.

Edited:

Reusable version:

function array_purge_values(&$a)
{
    foreach($a as &$v)
       $v = null;
}

Keep in mind though that PHP version starting from 5.3 pass values to functions by reference by default, i.e. the ampersand preceding argument variable in the function declaration is redundant. Not only that, but you will get a warning that the notion is deprecated.

amn
easy, nice and clean
Gordon
Easy yes, but it's not reusable. Code like this stinks. Sorry.
aefxx
@aefxx Then just wrap it into a function and pollute the global scope with the smell of just another userland function
Gordon
aefxx, you are very welcome to explain why this stinks. This is not a review board, people usually do explain their arguments here. I have edited my post to include exactly what you perhaps felt was missing.
amn
@Gordon He could easily put this in a utility class and call it statically from there - no global scope pollution whatsoever.@amn Well, your code does the trick, for sure. But most likely the programmer won't comment on why he is doing the nullification EVERY time he needs to. So, implementing a reusable function and commenting what it is for (NOT what it does, that's obvious) is best practice. Besides that, it's more error prone as the programmer could have an error EVERY single time he writes that piece of code (unlikely here, but you grasp the concept).
aefxx
@aefx Yes. He could have done that. So could you in your answer. But you didn't. The point is: @amn gave a working solution to the problem at hand. He *ockham'ly* omitted everything not relevant for the solution. And you tell him his solution stinks. That's ridiculous. The OP didn't ask for reusability and used a `foreach` himself.
Gordon
@Gordon How would he ask for what he obviously has no clue about? It's important to tell people to look beyond their actual problem and learn to write code that's readable, reusable and maintainable. That's my point, nothing more, nothing less.
aefxx
@aefxx If you want to share insight beyond the actual problem, then you are encouraged to do so, but don't tell people their answers stink just because they choose not to. Especially, when the answers are perfectly valid. Also, I find it rather offensive and arrogant to insinuate the OP is *obviously* unable to wrap a foreach loop into a function or that he is not capable of expressing a wish for an everyday concept like reusability.
Gordon
A: 

Define this function and call it whenever you need it:

function erase_val(&$myarr) {
    $myarr = array_map(create_function('$n', 'return null;'), $myarr);
}

// It's call by reference so you don't need to assign your array to a variable.
// Just call the function upon it
erase_val($array);

That's all!

aefxx
A: 

Why not making an array with required keys and asinging it to variable when you want reset it?

function resetMyArr(&$arr)
{
 $arr = array('key1'=>null,'key2'=>null); 
}
dev-null-dweller