Angeline
Angeline

Reputation: 2379

Is it possible to loop through all the items of two arrays using foreach?

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;?>

Upvotes: 1

Views: 1503

Answers (3)

usoban
usoban

Reputation: 5478

You can do that using for() loop, though it looks pretty nasty:

<?php

$arr1 = array('first 1', 'first 2', 'first 3');
$arr2 = array('second 1', 'second 2', 'second 3');

for($c1 = 0, $c2 = 0; $c1 < count($arr1), $c2 < count($arr2); $c1++, $c2++ )
{
    echo $arr1[$c1] . '<br />';
    echo $arr2[$c2] . '<br />';
}

?>

That's just PHP example, but you know how to edit it ;)

Edit:

Karl is right, duplicating indexes is not so great, so another solution, though you have to use @ to avoid warnings:

<?php

$arr1 = array('first 1', 'first 2', 'first 3', 'first 4');
$arr2 = array('second 1', 'second 2', 'second 3');

for($c = 0; $c < max( count($arr1), count($arr2) ); $c++ )
{
    echo @$arr1[$c] . '<br />';
    echo @$arr2[$c] . '<br />';
}

?>

Upvotes: 1

Blixt
Blixt

Reputation: 50187

Instead of repeating so much JavaScript, you could make use of jQuery's selectors. Instead of using unique classes on your form elements (or in addition to), use a generic class name like form-field. Then you can add a click event to all of them at the same time:

$('.form-field').click(function () {
    // Without knowing your HTML structure, I can't make this accurate, but maybe
    // this will work, assuming #shareform... is the parent <form> element:
    $(this).closest('form').toggle('show');
});

This JavaScript would only need to be output once and would apply to all elements with class="form-field". Note that you can have multiple classes on an element too: class="Share123 form-field" gives the element class Share123 and class form-field.

Upvotes: 2

newacct
newacct

Reputation: 122518

It sounds like you don't want to loop over two lists at the same time. You want to loop over two lists separately, executing the same code for each element in either list. So why not concatenate the lists:

foreach (array_merge($Forms, $FormsShared) as $r)
    // do stuff

Upvotes: 4

Related Questions