I have two arrays: $Forms
and $formsShared
.
<?php foreach ($Forms as $r): ?>
$("#shareform<?=$r['Form']['id'];?>").hide();
$(".Share<?=$r['Form']['id'];?>").click(function () {
$("#shareform<?=$r['Form']['id'];?>").toggle("show");
});
<?php endforeach; ?>
Currently, I have this hide and toggle function for each Form
in the $Forms
array. I want these functions to be enabled for the forms in the $formsShared
array also.
If I add another for loop for $formsShared
, like this:
<?php foreach ($formsShared as $r): ?>
$("#shareform<?=$r['Form']['id'];?>").hide();
$(".Share<?=$r['Form']['id'];?>").click(function () {
$("#shareform<?=$r['Form']['id'];?>").toggle("show");
});//.Share click
<?php endforeach; ?>
I achieve what I want, but it seems to be a repetition of the same code.
Is there any way in cakePHP to loop through two arrays in a single foreach
loop?
Solution: array_merge() only accepts parameters of type array. So use typecasting to merge other types.
<?php foreach (array_merge((array)$Forms,(array)$formsShared) as $r): ?>
$("#shareform<?=$r['Form']['id'];?>").hide();
$(".Share<?=$r['Form']['id'];?>").click(function () {
$("#shareform<?=$r['Form']['id'];?>").toggle("show");
});//.Share click
<?php endforeach;?>