如何通过搜索从PHP中的数组获得结果?

时间:2017-05-24 06:40:24

标签: php arrays

我是一个阵列。

Array
(
    [0] => Array
        (
            [0] => <img src='http://dev-gym-management.pantheonsite.io/wp-content/uploads/2017/03/images-5
.jpg' height='50px' width='50px' class='img-circle'/>
            [1] => Anna Khan
            [2] => Personal Trainer
            [3] => anna@gmail.com
            [4] => 990099000
            [5] => <a href='?page=gmgt_staff&tab=add_staffmember&action=edit&staff_member_id=5 class
='btn btn-info'>Edit</a><a href='?page=gmgt_staff&tab=staff_memberlist&action=delete&staff_member_id
=5' class='btn btn-danger'>Delete</a>
        )

    [1] => Array
        (
            [0] => <img src='http://dev-gym-management.pantheonsite.io/wp-content/uploads/2017/03/images-4
.jpg' height='50px' width='50px' class='img-circle'/>
            [1] => Jobaer Rahman
            [2] => Personal Trainer
            [3] => jobaer@gmail.com
            [4] => 55555555
            [5] => <a href='?page=gmgt_staff&tab=add_staffmember&action=edit&staff_member_id=3 class
='btn btn-info'>Edit</a><a href='?page=gmgt_staff&tab=staff_memberlist&action=delete&staff_member_id
=3' class='btn btn-danger'>Delete</a>
        )

    [2] => Array
        (
            [0] => <img src='http://dev-gym-management.pantheonsite.io/wp-content/uploads/2017/03/images-3
.jpg' height='50px' width='50px' class='img-circle'/>
            [1] => Milon Khan
            [2] => Fitness Instructors
            [3] => milon@gmail.com
            [4] => 7777777888
            [5] => <a href='?page=gmgt_staff&tab=add_staffmember&action=edit&staff_member_id=4 class
='btn btn-info'>Edit</a><a href='?page=gmgt_staff&tab=staff_memberlist&action=delete&staff_member_id
=4' class='btn btn-danger'>Delete</a>
        )

)

现在如果我想通过&#34; Anna Khan&#34;我需要结果如下:

Array
(
    [0] => Array
        (
            [0] => <img src='http://dev-gym-management.pantheonsite.io/wp-content/uploads/2017/03/images-5
.jpg' height='50px' width='50px' class='img-circle'/>
            [1] => Anna Khan
            [2] => Personal Trainer
            [3] => anna@gmail.com
            [4] => 990099000
            [5] => <a href='?page=gmgt_staff&tab=add_staffmember&action=edit&staff_member_id=5 class
='btn btn-info'>Edit</a><a href='?page=gmgt_staff&tab=staff_memberlist&action=delete&staff_member_id
=5' class='btn btn-danger'>Delete</a>
        )   

)

我尝试使用搜索阵列但无法正常工作。为了降低复杂性,我不使用原始代码。这样做最简单的方法还是php数组函数?

1 个答案:

答案 0 :(得分:0)

$data = [array here];
$search_str = 'Anna Khan';

array_walk_recursive($data, function($item, $key) use ($search_str) {
   if (stripos($item, $search_str) !== false) {
       echo $key; // or any action you want.
   }
});