草庐IT

php - array_search 函数不返回值

coder 2023-10-24 原文

这里有两个表,即 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/

有关php - array_search 函数不返回值的更多相关文章

  1. ruby - 在 Ruby 中实现 `call_user_func_array` - 2

    我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)

  2. ruby-on-rails - 结合 meta_search 与 acts_as_taggable_on - 2

    我在开发的Rails3网站的一些搜索功能上遇到了一个小问题。我有一个简单的Post模型,如下所示:classPost我正在使用acts_as_taggable_on来更轻松地向我的帖子添加标签。当我有一个标记为“rails”的帖子并执行以下操作时,一切正常:@posts=Post.tagged_with("rails")问题是,我还想搜索帖子的标题。当我有一篇标题为“Helloworld”并标记为“rails”的帖子时,我希望能够通过搜索“hello”或“rails”来找到这篇帖子。因此,我希望标题列的LIKE语句与acts_as_taggable_on提供的tagged_with方法

  3. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  4. Ruby Koans about_array_assignment - 非平行与平行分配歧视 - 2

    通过ruby​​koans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John

  5. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  6. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  7. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  8. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  9. ruby - Ruby 中的隐式返回值是怎么回事? - 2

    所以我开始关注ruby​​,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出

  10. arrays - 这是 Ruby 中 Array.fill 方法的错误吗? - 2

    这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]

随机推荐