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.

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

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳