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

$("#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 2 arrays in a single for loop?

Hi there, you could use a regular for loop like this:

$max = ceil(count($Forms,$sharedForms);
for ($i = 0; $i < $max; $i++)
{
$outstring1.= " $(\"#shareform".$Forms[$i]['Form']['id']."\").hide();
         $(\".Share".$Forms[$i]['Form']['id']."\").click(function(){
         $(\"#shareform".$Forms[$i]['Form']['id']."\").toggle(\"show\");
    });";

$outstring2.= " $(\"#shareform".$sharedForms[$i]['Form']['id']."\").hide();
        $(\".Share".$sharedForms[$i]['Form']['id']."\").click(function(){
                $(\"#shareform".$sharedForms[$i]['Form']['id']."\").toggle(\"show\");
         });";
}

echo $outstring1;
echo $outstring2;

I have found a solution for the question I asked.

I concatenated the arrays using the array_merge function, and then used a singe for to loop through the concatenated array and here's the code for it.

<?php foreach (array_merge((array)$Forms,(array)$formsShared) as $r): ?>
                $(".Share<?=$r['Form']['id'];?>").click(function(){
					$("#shareform<?=$r['Form']['id'];?>").toggle("show");
		 });	 		
<?php endforeach; ?>

@Menster : Thank you for your help.

No prob job :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.