array_search_col()
Find the first row in a list of rows (e.g. query results or a parsed JSON array) whose column equals a value.
Syntax
array_search_col(data, column, value)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
data |
array | yes | A list of associative arrays (rows). |
column |
string | yes | The key to compare in each row. |
value |
any | yes | The value to match. |
Returns
The first matching row (array), or false if none match.
Example
$items = fromjson($resp["body"]); // [{"sku":"A1","qty":3}, {"sku":"B2","qty":7}]
$row = array_search_col($items, "sku", "B2");
if ( $row ) sys_log("Qty: " . $row["qty"]);
Example output
["sku" => "B2", "qty" => 7]
Notes
- Returns the first match only. Comparison is loose (
==). - To search your own tables, prefer records_query(); this is for in-memory arrays.
See also: array_sort_col()