pass keyname in array_filter [duplicate] - php

This question already has answers here:
How to filter an associative array comparing keys with values in an indexed array?
(12 answers)
Closed 6 years ago.
array_filter only passes array values, not keynames... how to access keyname?
for example:
$array= array('country'=>'Spain');
array_filter($array,'my_func');
with my_func i want to check:
if KEYNAME == 'country' then ...

array_filter can pass them.
array_filter($array, 'my_func', ARRAY_FILTER_USE_BOTH);
and my_func will then be passed two params; the key, and the value.
array_filter doc page

Related

Set Array Equal to Another Array without Specified Key/Values [duplicate]

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.

How do I append a value to an array within an array in PHP? [duplicate]

This question already has answers here:
How to add elements to an empty array in PHP?
(8 answers)
Closed 5 years ago.
Given this PHP array:
$options['systems'] = array(1, 2, 3)
How would I append the value 4 to the $systems array within the $options array?
You could use array_push to push additional items like so:
array_push($options['systems'], 4);
Or the shorthand version:
$options['systems'][] = 4;
You can use php array_push function. Like this. array_push($options['systems'],4);
you can read the detail of array_push from below link.array_push manual

Obtaining all instances of a specific field from json in PHP [duplicate]

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.

Get nth key of associative php array [duplicate]

This question already has answers here:
Accessing an associative array by integer index in PHP
(6 answers)
Closed 7 years ago.
I want to get the value of the KEY of an associative PHP array at a specific entry. Specifically, I know the KEY I need is the key to the second entry in the array.
Example:
$array = array('customer' => 'Joe', 'phone' => '555-555-5555');
What I'm building is super-dynamic, so I do NOT know the second entry will be 'phone'. Is there an easy way to grab it?
In short, (I know it doesn't work, but...) I'm looking for something functionally equivalent to: key($array[1]);
array_keys produces a numerical array of an array's keys.
$keys = array_keys($array);
$key = $keys[1];
If you're using PHP 5.4 or above, you can use a short-hand notation:
$key = array_keys($array)[1];

Count number of non-empty items in array? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to count non-empty entries in php array?
I want to count the number of keys in an array that aren't empty/null.
EG:
array(1>'asdf',2>'fdas',3>'');
count($array) would return 3, I want a function that returns 2 as 3 is empty
Are there any php built in functions to do this? How can it be done?
Use:
count(array_filter($array));
array array_filter ( array $input [, callable $callback = "" ] )
If no callback is supplied, all entries of input equal to FALSE (see
converting to boolean) will be removed.

Categories