dongqiao1158 2014-02-24 16:35
浏览 84
已采纳

PHP - 从数组中的某个索引获取元素,其中一个元素具有特定值

Is there a simple way of getting a set of values inside an array when one of it's value is equal to a specified value?
For example, I retrive this array from the database where it comes with the active languages on the site: [$active_lang]

Array
(
    [0] => Array
        (
            [id] => 1
            [code] => pt
            [idiom] => Português
        )

    [1] => Array
        (
            [id] => 2
            [code] => es
            [idiom] => Espanhol
        )

    [2] => Array
        (
            [id] => 3
            [code] => en
            [idiom] => Inglês
        )

)

Then I get the contents for the active languages: [$records]

Array
(
    [0] => Array
        (
            [id] => 1
            [page_base_id] => 1
            [language] => pt
            [title] => Title pt
            [subtitle] => Subtitle pt
            [description] => Description pt
        )

    [1] => Array
        (
            [id] => 2
            [page_base_id] => 1
            [language] => es
            [title] => Title es
            [subtitle] => Subtitle es
            [description] => Description es
        )

    [2] => Array
        (
            [id] => 3
            [page_base_id] => 1
            [language] => en
            [title] => Title en
            [subtitle] => Subtitle en
            [description] => Description en
        )

)

What I need now is a way of echoing the right content in a foreach inside the array of the $active_langand avoid doing a second loop to reach the right language to display inside the $records array. I'm currently using this code:

<div class="tab-content">
  <?php
  foreach ($active_lang as $lang) { ?>
  <div class="tab-pane" id="tab_<?php echo $lang['code']; ?>">
    <div class="row-fluid ">
      <div class="span12">
        <div class="control-group">
          <div class="controls">
            <label class="control-label">Ttile</label>
            <input id="title_1_pt" type="text" class="span12 " value="
            <?php 
            foreach($records_languages as $record){
              if($record['language']==$lang['code']){
                echo $record['title'];
              }
            }
            ?>
            " />
          </div>
        </div>
      </div>
    </div>
  </div>
  <?php } ?>
</div>

Is there a way of doing something like this?

<div class="tab-content">
  <?php
  foreach ($active_lang as $lang) { ?>
  <div class="tab-pane" id="tab_<?php echo $lang['code']; ?>">
    <div class="row-fluid ">
      <div class="span12">
        <div class="control-group">
          <div class="controls">
            <label class="control-label">Ttile</label>
            <input id="title_1_pt" type="text" class="span12 " value="
            <?php 
               // WHERE $records[language] == $lang['code'] ECHO THE [title] FROM THE SAME INDEX IN ARRAY
            ?>
            " />
          </div>
        </div>
      </div>
    </div>
  </div>
  <?php } ?>
</div>
  • 写回答

1条回答 默认 最新

  • drevls8138 2014-02-24 16:47
    关注

    You can create new array at the very beginning:

    $records = array();
    foreach ($records_languages as $record) {
        $records[$record['language']] = $record;
    }
    

    and then use

    $records[$lang['code']]['title']
    

    That will save you some looping.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?