gpt4 book ai didi

php - array_search 函数不返回值

转载 作者:可可西里 更新时间:2023-11-01 08:39:26 26 4
gpt4 key购买 nike

这里有两个表,即 tools 和 tool_use。

工具表看起来像这样

  id   name               tools_names                                   quantity    type

13 cutting player cutting playerA,cutting playerB,cutting playerC 3 engineer
12 REFLECTORS REFLECTORSA,REFLECTORSB 2 team

tool_use 表看起来像这样

 id     user_id   type        tools                 
8 siraj engineer cutting playerA,cutting playerB
7 siraj team REFLECTORSB
6 siraj team REFLECTORSA

我想显示 tools_names 除了在插入时插入到 tool_use 表中,但整个 tools_names 都显示,即使结果看起来像在表中。这是我的控件

public function ajax_tools()
{
$data['tools']=$this->Tools_model->view_available_tools($_POST['type']);
foreach ($data['tools'] as $key=>$val) {
$data['toolss'][] =explode(',',$val['tools_names']);

}
$data['tools_names'] = $this->Tools_model->get_tool_names($_POST['type'])->result();
foreach ($data['tools_names'] as $row)
{
if (($key =array_search($row->tools,$data['toolss'])) !== false)
{
unset($data['toolss'][$key]);
$data['toolss'] = array_values($data['toolss']);

}
}
return $data['toolss'];
$this->load->view('ajax_tools',$data);
}

这是我的模型

public function view_available_tools($type)
{
$this->db->order_by('id','desc');
$this->db->where('status',1);
$this->db->where('type',$type);
$query=$this->db->get('tools');
return $query->result_array();
}

public function get_tool_names($type)
{
return $this->db->get_where('tool_use',array('type'=>$type));
}

这是我的看法

<div class="form-group">
<label for="type" class="control-label">Type:</label>
<select name="type" id="type" class="form-control" required>
<option value="">please select</option>
<option value="team" <?php echo set_select('type','team'); ?>>Team</option>
<option value="engineer" <?php echo set_select('type','engineer'); ?>>Engineer</option>
</select>
</div>

<div class="form-group ">
<label for="tools" class="control-label">Tools:</label>
<select name="tools[]" id="tools" multiple="multiple" required>
<option value="">please select</option>

</select>
</div>

<script>
$('#type').change(function(){
var type=$('#type').val();
var url='<?php echo base_url(); ?>tools/tools_control/ajax_tools';
$.post(url, {type:type}, function(data)
{

$('#tools').html(data);
});
});
</script>

请帮我解决我的问题

最佳答案

当您array_search 时,您正在尝试搜索$row->tools,它应该包含cutting playerA,cutting playerB。然后您在一个数组中搜索它,该数组不包含相同类型的逗号分隔值列表,而是包含它们的展开版本(就像您在第 3 行执行的 explode 一样)。

关于php - array_search 函数不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com