Skip to content
Snippets Groups Projects
Commit 9c2a1d37 authored by Akex's avatar Akex
Browse files

add func search | add test data

parent 2a7f91b8
Branches PTPS|Function_2
No related tags found
1 merge request!12Ptps|function 2
<?php
namespace App\Actions;
class IdSearchAction
{
/**
* Найдет элемент с указаным id
* @param array $array - массив, содержащий элементы со структурой
* [
* 'id' => 30,
* 'name' => 'Jhon',
* 'age' => 23,
* ]
* @param $id - ид искомого элемента
* @return array|null - найденный элемент/ вернет null при его отсутствии
*/
public static function search(array $array, $id): ?array
{
foreach ($array as $item){
if ($item['id'] === $id){
return $item;
}
}
return null;
}
}
......@@ -8,5 +8,18 @@ namespace App\TestData;
class TestData
{
public const ARRAY_FOR_SEARCHING = [
[
'id' => 30,
'name' => 'Sasha',
],
[
'id' => 542,
'name' => 'Adlan',
],
[
'id' => 3234,
'name' => 'Pavel',
],
];
}
......@@ -2,6 +2,7 @@
require_once __DIR__ . '/../vendor/autoload.php';
use App\Actions /*placeholder for a class */;
use App\Actions\IdSearchAction;
use App\TestData\TestData;
var_dump(IdSearchAction::search(TestData::ARRAY_FOR_SEARCHING));
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment