如何过滤PHP2D数组以返回带有名称属性的元素,该属性包含作为通配符的搜索查询?
我的二元数组看起来像:如何过滤PHP2D数组以返回带有名称属性的元素,该属性包含作为通配符的搜索查询?,php,arrays,codeigniter,search,Php,Arrays,Codeigniter,Search,我的二元数组看起来像: $users = [ [name: "John Doe", email: 'e@e.com'], [name: 'Patrice', email: 'pt@ex.com'] ]; 我正在尝试编写一个函数,该函数只返回包含name属性中的查询的元素数组 例如: //if $q= 'e', the function will r
$users = [
[name: "John Doe", email: 'e@e.com'],
[name: 'Patrice', email: 'pt@ex.com']
];
我正在尝试编写一个函数,该函数只返回包含name属性中的查询的元素数组
例如:
//if $q= 'e', the function will return the original array as both array elements have the substring 'e' in the value of the name attribute.
//if an empty string or no parameter for the query search , return the unadulterated original array.
基本上,我正在尝试一个类似于以下的函数,只是它使用预定义的数组,而不是数据库
// Similar Function but with database
function fetch_data($query)
{
$this->db->like('student_name', $query);
$query = $this->db->get('tbl_student');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$output[] = array(
'name' => $row["student_name"],
'image' => $row["image"]
);
}
echo json_encode($output);
}
}
}
我不熟悉php和CodeIgniter,我正在尝试用它来自动完成。你可以这样做
$search_string = "e";
$keys = preg_grep("/$search_string/i", array_column($users, 'name'));
$result = [];
foreach($keys as $key => $value)
{
$result[] = $users[$key];
}
//return $result to your autocomplete.
关于和的PHP文档
我已经测试了代码
你可以这样做
$search_string = "e";
$keys = preg_grep("/$search_string/i", array_column($users, 'name'));
$result = [];
foreach($keys as $key => $value)
{
$result[] = $users[$key];
}
//return $result to your autocomplete.
关于和的PHP文档
我已经测试了代码
您可以使用so不区分大小写对数组的元素进行排序,以查找搜索字符串:
$users = [
['name' => "John Doe", 'email' => 'e@e.com'],
['name' => 'Patrice', 'email' => 'pt@ex.com']
];
function fetch_data($data, $search) {
return array_filter($data, function ($v) use ($search) { return stripos($v['name'], $search) !== false; });
}
print_r(fetch_data($users, 'e'));
输出:
Array (
[0] => Array ( [name] => John Doe [email] => e@e.com )
[1] => Array ( [name] => Patrice [email] => pt@ex.com )
)
print_r(fetch_data($users, 'p'));
Array (
[1] => Array ( [name] => Patrice [email] => pt@ex.com )
)
输出:
Array (
[0] => Array ( [name] => John Doe [email] => e@e.com )
[1] => Array ( [name] => Patrice [email] => pt@ex.com )
)
print_r(fetch_data($users, 'p'));
Array (
[1] => Array ( [name] => Patrice [email] => pt@ex.com )
)
您可以使用so不区分大小写对数组的元素进行排序,以查找搜索字符串:
$users = [
['name' => "John Doe", 'email' => 'e@e.com'],
['name' => 'Patrice', 'email' => 'pt@ex.com']
];
function fetch_data($data, $search) {
return array_filter($data, function ($v) use ($search) { return stripos($v['name'], $search) !== false; });
}
print_r(fetch_data($users, 'e'));
输出:
Array (
[0] => Array ( [name] => John Doe [email] => e@e.com )
[1] => Array ( [name] => Patrice [email] => pt@ex.com )
)
print_r(fetch_data($users, 'p'));
Array (
[1] => Array ( [name] => Patrice [email] => pt@ex.com )
)
输出:
Array (
[0] => Array ( [name] => John Doe [email] => e@e.com )
[1] => Array ( [name] => Patrice [email] => pt@ex.com )
)
print_r(fetch_data($users, 'p'));
Array (
[1] => Array ( [name] => Patrice [email] => pt@ex.com )
)
如果你想要这样的数组
$users = [
name: "John Doe",
name: 'Patrice',
];
则无需额外循环,直接使用如下:
$this->db->select("student_name as name,image");
$this->db->like('LOWER(student_name)',strtolower($query['name']));
$data = $this->db->get('tbl_student')->result_array();
$student_name=array();
if(count($data)>0)
{
$student_name=array_column($data,'name');
}
return $student_name;
如果你想要这样的数组
$users = [
name: "John Doe",
name: 'Patrice',
];
则无需额外循环,直接使用如下:
$this->db->select("student_name as name,image");
$this->db->like('LOWER(student_name)',strtolower($query['name']));
$data = $this->db->get('tbl_student')->result_array();
$student_name=array();
if(count($data)>0)
{
$student_name=array_column($data,'name');
}
return $student_name;
这段代码实际上不起作用。为什么这是公认的答案?@Nick我已经更新了我的答案。我刚才没有检查密码。数组_键应该用于完全匹配,而不是部分匹配。现在看起来不错。我仍然很惊讶,有人会接受一个甚至没有编译的答案……哦,这是一个错误。事实上,我本想接受尼克的回答,这对我来说很有效。我们可以改变被接受的答案吗?@jungleMan你可以改变被接受的答案。只要勾选你想要的,这个代码实际上不起作用。为什么这是公认的答案?@Nick我已经更新了我的答案。我刚才没有检查密码。数组_键应该用于完全匹配,而不是部分匹配。现在看起来不错。我仍然很惊讶,有人会接受一个甚至没有编译的答案……哦,这是一个错误。事实上,我本想接受尼克的回答,这对我来说很有效。我们可以改变被接受的答案吗?@jungleMan你可以改变被接受的答案。只要勾选你想要的。