I am new to php and really could do with a little help please. I have my airtable contected to my site and wish to search a table based on a reference number and return the results. I'm stuck searching a multi level array - I have tried several things I have found on the web but no luck. Any advise please. my array looks like this...
`enter code here`Array
(
[0] => Array
(
[records] => Array
(
[0] => Array
(
[id] => rec0ZZcgDibuWEhKQ
[fields] => Array
(
[email] => [email protected]
[phone] => 07701
[Lastname] => Mouse
[Firstname] => Micky
[ref] => 12
)
[createdTime] => 2022-03-01T20:52:05.000Z
)
[1] => Array
(
[id] => recCAGibN75YZEjI7
[fields] => Array
(
[email] => [email protected]
[phone] => 4545
[Lastname] => Bob
[Firstname] => Sponge
[ref] => 13
)
[createdTime] => 2022-03-03T20:30:20.000Z
)
[2] => Array
(
[id] => recqFZx918TM0P3zi
[fields] => Array
(
[email] => [email protected]
[phone] => 45525
[Lastname] => Frost
[Firstname] => Jack
[ref] => 277
)
[createdTime] => 2022-03-03T22:28:28.000Z
)
[3] => Array
(
[id] => recviRfBrNymoq7qw
[fields] => Array
(
)
[createdTime] => 2022-03-03T22:30:04.000Z
)
)
)
)
CodePudding user response:
You can search the array with array_filter
somthing like this:
$reference_number = "your reference number to compare against";
// just basing this off the array you show in your question
$input_array = $input[0]["records"];
// filter input
$found_records = array_filter($input_array, function($record){
return $record["id"] === $reference_number;
});
// check if any are found then do something with it
if ($found_records) {
$phone = $found_records[0]["fields"]["phone"];
}