How to Get Array Key from Corresponding Array Value

How to get array key from corresponding array value?

You could use array_search() to find the first matching key.

From the manual:

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;

Find the key of a corresponding array value

array_search function is meant for that usage

snipet:

$index = array_search('UD', $yourarray);
if($index === false){ die('didn\'t found this value!'); }
var_dump($index);

Get values corresponding to key inside array of objects

function findValueByKey(obj, key, out) {
var i, proto = Object.prototype,ts = proto.toString,
hasOwn = proto.hasOwnProperty.bind(obj);
if ('[object Array]' !== ts.call(out)) out = [];
for (i in obj) {
if (hasOwn(i)) {
if (i === key) {
out.push(obj[i]);
} else if ('[object Array]' === ts.call(obj[i]) ||
'[object Object]' === ts.call(obj[i])) {
findValueByKey(obj[i], key, out);
}
}
}
return out;
}

worked for me.

Get the maximum value and the key of the corresponding array from an array of arrays in PHP

Check with following code, it will find max value then max array based on value:

$maxval=max(array_column($array,"bid")); 
$maxarray=array_keys(array_column($array,"bid"),$maxval);
print_R($array[$maxarray[0]]);

$array = your array,

PHP - Get key name of array value

If you have a value and want to find the key, use array_search() like this:

$arr = array ('first' => 'a', 'second' => 'b', );
$key = array_search ('a', $arr);

$key will now contain the key for value 'a' (that is, 'first').

Get array values by keys

I think you are searching for array_intersect_key. Example:

array_intersect_key(array('a' => 1, 'b' => 3, 'c' => 5), 
array_flip(array('a', 'c')));

Would return:

array('a' => 1, 'c' => 5);

You may use array('a' => '', 'c' => '') instead of array_flip(...) if you want to have a little simpler code.

Note the array keys are preserved. You should use array_values afterwards if you need a sequential array.

how to get the respective array key value if it meets the if condition in for loop

You have 2 problems in your if statement :

Replace if ($x=blue) { by if ($x == "blue") {

  1. Blue is a string so you need some quote
  2. = is to assign value, == or === are for comparaison.

But you shouldn't do a foreach loop to get your answer. If you just do $group_id = $grouparray['blue']; you will get what you want, not sure why you need a loop ?

Get Array Index Key - Javascript

I'd iterate over the actual states, the ones which have the correct values, and put them in a map.

Then iterate the states array and just check if your desired value exists in said map.

let states = [['AL', 0],
['AK', 0],
['AZ', 0],
['AR', 0],
['CA', 0]];

let actualStates = [['AL', 1],
['AK', 2],
['AZ', 3],
['CA', 4]];

function merge(states, actualStates){
let map = {};
for(let actualState of actualStates) map[actualState[0]] = actualState[1];
for(let state of states) state[1] = map[state[0]] || state[1];
return states;
}

console.log(merge(states, actualStates));

Select a value from an array based on another array in php

You can do it with this line where array_keys gives us an array of keys of our filtered array. end and max do the same job in this case because we have a sorted array and want to return its last index. So finally $percentiles[$lastIndexKeyOfOurFilteredArray] is what we print out:

echo $percentiles[max(array_keys(array_filter($percentile_bounds,function ($x) use ($total_score){return $x<$total_score; })))]


Related Topics



Leave a reply



Submit