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.
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:
How to filter an array by a condition
(9 answers)
Closed 6 years ago.
I have a webservice that returns a very large json object. I decode this to an array for parsing.
What I'm trying to do though is extract all array members from the json array where there is [id] => 21 at the start of the individual array object.
A nice simple task using LINQ, but not an idea how to do this in PHP. There has to be a way to filter through and extract.
I'd use something like array_filter for this
function find_id($var)
{
// returns whether the is is 21
return($var['id'] === 21);
}
$result = array_filter($jsondata, "find_id"));
array_filter Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.
This question already has answers here:
String to multidimensional array path
(1 answer)
Nested numbering to array keys
(1 answer)
Closed 8 years ago.
I would like a multiple child 'key' as a 'lookup' to get a object out of various associative arrays. For example:
$lookup_key = "['objects'][0]['object2']";
// this will be stored so when I need to get an object's value from various different arrays I can use these string to form a lookup key to get certain objects
$object_array= array();
$object_array= ["objects"=>[["object1"=>"boot", "object2"=>"shoe"],"object"], "flowers"];
// using that key get the value
$object_value= $object_array->lookup($lookup_key);
Now does php have a method that already does this type of lookup or I suppose its an multidimensional key?
Any help would be great! Thanks in advance. This is part of an object lookup table.
I approached this in a slightly different way by storing a string of the objects then exploding that into an array from that travelling across that array to get the value:
$object_array= array();
$object_array= ["objects"=>[["object1"=>"boot", "object2"=>"shoe"],"object"], "flowers"];
$lookup=array();
$lookup_key="objects,0,object2";
$lookup=explode(',',$lookup_key);
$temp_object=array();
$new_object_array=array();
$new_object_array=$object_array;
$count=count($lookup);
for($i=0; $i<$count; $i++) {
$temp_object=$new_object_array[$lookup[$i]];
$new_object_array=$temp_object;
}
echo "\n value: $new_object_array";
As I was search for a quick implementation this seemed to be the best way within my time constraints and php lose variable casting.
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.