Reputation: 4879
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_lang
and 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>
Upvotes: 0
Views: 91
Reputation: 101
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.
Upvotes: 1