Get item in array according to the value of a key within the item

Related searches

Suppose I have an array like this:

$myArray = [
    [ 'id' => 1, 'name' => 'Some Name' ],
    [ 'id' => 2, 'name' => 'Some Other Name ] 
]

Now if want to get the second item without using the index ($myArray[1]), but instead by using the value of name which is Some Other Name, how do I do that?

I don't want to loop through the entire array just for one value, so how can I write this in a way that I don't explicitly loop through the array myself? I looked into array_search(), but couldn't find examples where $myArray contains additional arrays as items.

you can use array_filter() function:

<?php

$myArray = [
    ['id' => 1, 'name' => 'Some Name'],
    [
        'id' => 2, 'name' => 'Some Other Name'
    ]
];


$filterData = array_filter($myArray, function ($data) {
    return $data['name'] == "Some Other Name";
});
var_dump($filterData);
die;
?>

Array.prototype.find(), Object to use as this inside callback . Return value. The value of the first element in the array that satisfies the provided testing function. Otherwise� When I'm Filtering an Array, I use Select to get the item I'm looking for. It's outputting something like: { "Time Off Start Date": "43612" } When I go to compose or set a variable from this, it gives me the entire string. How do I set just the value '43612'?

Not entirely sure what you're trying to do, but you can index the array on name:

echo array_column($myArray, null, 'name')['Some Other Name']['id'];  //echos 2

To do it once and have access to all name:

$result = array_column($myArray, null, 'name');

echo $result['Some Other Name']['id'];  // echos 2
echo $result['Some Name']['id'];        // echos 1

Work with object and array data, Use these methods to make observable changes to arrays and object subproperties. Use the get method to retrieve a value based on its path. To retrieve an array item by key, you can simply use the get method described in Get a value by� Find one element. To find the element with a given name in an array you can use find:. arr.find(item=>item.name=="k1"); Note that find will return just one item (namely the first match):

This is the simpliest solution:

$requiredItem = null;
foreach ($myArray as $item) {
    if ($item['name'] === 'Some Other Name') {
        $requiredItem = $item;
        // `break` allows you to STOP iterating over array
        break;
    }
}
print_r($requiredItem);

All other solutions will include full loop over your array at least once.

Array Functions - Manual, array_change_key_case — Changes the case of all keys in an array callback function; array_flip — Exchanges all keys with their associated values in an array I need to take an element from the Array and change its position within the Array Here is a function to find out the maximum depth of a multidimensional array. The filter() method creates a new array with all the elements that pass the test implemented by the callback() function. Internally, the filter() method iterates over each element of the array and pass each element to the callback function. If the callback function returns true, it includes the element in the return array.

Arrays - Manual, As array values can be other arrays, trees and multidimensional arrays are also possible. PHP arrays can contain integer and string keys at the same time as PHP does not "If you convert a NULL value to an array, you get an empty array. " I'm using ListTile to create each item in the list. Each item is created dynamically from the data array. ListTile provides onTap, but it's insufficient for me, because I need to find out which item is clicked by either getting the key or index. ListTile:

Array methods, That's natural, because delete obj.key removes a value by the key . It's all it does. How do we find an object with the specific condition? Here the They are used to calculate a single value based on the array. The syntax is:. The Get Items action is configured with a "maximum get count" of one, but of course that one item is still in an array. Since I can't send an approval email inside of a for each, I need to somehow get access to that one element outside the array. As an alternative, I need to query a sharepoint list for a single list item. "Get item" only allows

key − This is the Key to be searched in the dictionary. default − This is the Value to be returned in case key does not exist. Return Value. This method return a value for the given key. If key is not available, then returns default value None. Example. The following example shows the usage of get() method.

Comments
  • You can’t do that with array_search alone, that will only compare the complete items. You can use f.e. array_filter to filter out all elements that do not match. Or you get only the name values first, using array_column, then you search your name in that using array_search again, and then use the found key to access the item in the original array …
  • array_search uses loop too, shock!
  • @u_mulder The point is that I can't write a loop as it won't go through code review then. But thanks for mentioning it, really helped.
  • The person who does codereview is not very smart?
  • @u_mulder That is another factor that weighs a bit heavily as well tbh :P But no, it's a huge codebase with many rules and conventions and it's a long story to try to explain now, but I understand the question was a bit vague with the whole looping story.