0

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>
2
  • 1
    1. you should have joined at query time; 2. use array_filter [which would use a loop inside for sure. Commented Feb 24, 2014 at 16:38
  • 1. I need the array to be indepented so I can't do a join in the query 2. Is there no other way around instead of using a second loop? Commented Feb 24, 2014 at 16:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you @Mateusz Drankowski, yes I think it'll have to be something like that but I was looking for a more intuitive way of getting the information needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.