This question already has answers here:
For cleared or unset php arrays, are elements garbage collected?
(3 answers)
Closed 8 years ago.
My question might seems basic but still, can't figure how to works this out.
Consider an array of my favorite fruits
$array = array("Banana","Rasberry","Blackberry")
I'm looking to clear this array so that all keys and values would be erased. My array would be empty just like if I had wrote
$array = array();
Then, I could array_push some new data in.
I thought that I could array_walk($array, unset($array[$key]) but it's not working properly.
Your question includes the best solution for your situation:
$array = array();
This is the fastest way to make the $array variable point to an empty array.
Related
This question already has answers here:
How to delete a key and return the value from a PHP array?
(4 answers)
Closed 4 years ago.
Is there a native PHP function for removing an element from an associative array, and returning the value?
Like unset() but with a return value, or array_shift() where you can specify the index to shift?
$element = unset($array['index']);
$element = array_shift($array, 'index');
I know it's easy to do, I'm just curious if there's an elegant one-liner for doing this.
Looking quickly at the official PHP documentation, in the current version (7.2) doesn't have a function that removes and returns an element by the key.
But as you mentioned there are several ways to solve this problem. As you can see at: https://stackoverflow.com/a/10898827/4214312
This question already has answers here:
Using a string path to set nested array data [duplicate]
(8 answers)
Closed 4 years ago.
Edit (after downvote): There is a similar question here > Using a string path to set nested array data
However I didn't find that question when searching for an answer due to the way it's worded, and I'm sure this will happen for other people, so this question may act as a useful gateway to that question and it's answers.
I'm sure I'm missing something obvious, but I can't think how to do this: I have an array containing one or more items:
array('value1', 'value2');
I need to use these values as the keys in a multidimensional array :
array['value1']['value2'] = 'somevalue';
How do I do this?
You can use a nice recursion here:
function nestArray($items, $value) {
return $items ?
array($items[0] => nestArray(array_slice($items, 1), $value))
: $value;
}
$array = array('value1', 'value2');
print_r(nestArray($array, 'somevalue'));
This question already has answers here:
php array difference against keys of an array and an array of keys?
(3 answers)
Unset elements using array keys
(2 answers)
Closed 5 years ago.
I am attempting to mirror the behavior of newArray = oldArray, with the caveat of excluding some key/values of the oldArray, so something like newArray = oldArray - undesiredOldKeyValue. I realize this is fully doable with a foreach on the oldArray and using an if to see if the encountered key is desired or not, but I am interested in a simpler or more concise approach if possible.
A couple of things to keep in mind, I need to exclude key/value pairs based on key, not value. I do not want to modify the oldArray in the process of doing this.
You may try to use array_filer. Something like:
$new_array = array_filter($old_array, function ($value, $key) {
// return false if you don't want a value, true if you want it.
// Example 1: `return $value != 'do not keep this one';`
// Example 2: `return !in_array($key, ['unwanted-key1', 'unwanted-key2', 'etc']);`
}, ARRAY_FILTER_USE_BOTH);
It will filters elements of an array using a callback function.
This question already has answers here:
Is it necessary to declare PHP array before adding values with []?
(13 answers)
Closed 5 years ago.
I dont know why people declare array before loops and etc..:
$new= array(); // <----- why this is needed ?
foreach($something as $v){
$new[] = $v;
}
Why to declare the array before setting its value? (In other languages, i.e. C# and JAVA it is needed, but why in PHP?)
You're not setting its value, you're pushing a new element onto the array. But there needs to be an empty array to push onto.
This question already has answers here:
comparing arrays in php, without caring for the order
(6 answers)
Closed 9 years ago.
I'm trying solving a problem where i need to check if the arrays are same no matter how they are sorted i cannot use sorting because it add extra over head to time this function is taking in answering.
I am currently using array_diff_assoc
$arr1 = array(1,2,3);
$arr2 = array(3,2,1);
$result = array_diff_assoc($arr1,$arr2);
print_r($result);
Array
(
[0] => 1
[2] => 3
)
But the above arrays are same!! The human way.
Any idea for comparing two arrays.
Well interpreter is not human right ? ;)
Even if you do a simple var_dump($arr1==$arr2) on your existing array, it will return false.
This below code returns true !
$arr1 = array(1,2,3);
$arr2 = array(2=>3,1=>2,0=>1);//position is same as yours., i've just set a key
var_dump($arr1==$arr2); //true