Asked  7 Months ago    Answers:  5   Viewed   46 times

I have an array that looks like this:

$array = array(
    array(
        "http://google.com",
        "Google"
    ),

    array(
        "http://yahoo.com",
        "Yahoo"
    )
);

What is the simplest way to loop through it. Something like:

foreach ($array as $arr) {
    // help
}

EDIT: How do I target keys, for example, I want to do:

foreach ($array as $arr) {
    echo '<a href" $key1 ">';
    echo ' $key2 </a>';
}

 Answers

78

In order to echo out the bits you have to select their index in each array -

foreach($array as $arr){
    echo '<a href="'.$arr[0].'">'.$arr[1].'</a>';
}

Here is an example.

Wednesday, March 31, 2021
 
StampyCode
answered 7 Months ago
32

Move the table tags inside the IF statements

<?php foreach($credits as $credit) : ?>
    <?php if($credit['credit_type'] == "short") : ?>
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td><?php echo $credit['category_position']; ?></td>
            <td><?php echo $credit['category_title']; ?></td>
        </tr>
        <tr>
            <td><?php echo $credit['credit_heading']; ?></td>
            <td><a href="">Edit</a></td>
        </tr>
      </table>
    <?php endif; ?>
    <?php if($credit['credit_type'] == "long") : ?>
       <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td><?php echo $credit['category_position']; ?></td>
            <td><?php echo $credit['category_title']; ?></td>
            <td><strong>Title</strong></td>
            <td><strong>Role</strong></td>
            <td><strong>Director</strong></td>
        </tr>
        <tr>
            <td><?php echo $credit['credit_position']; ?></td>
            <td><?php echo $credit['credit_heading']; ?></td>
            <td><?php echo $credit['credit_title']; ?></td>
            <td><?php echo $credit['credit_role']; ?></td>
            <td><?php echo $credit['credit_director']; ?></td>
        </tr>
       </table>
    <?php endif; ?>
<?php endforeach; ?>

Note this is only going to work if you only ever have 2 credit_type values

Wednesday, March 31, 2021
 
weegee
answered 7 Months ago
92

You're looking for a recursive function that scans your array to a depth of n. Something like this could work:

function findPagesInArray($myArray) {
    foreach($myArray as $index => $element) {
        // If this is an array, search deeper
        if(gettype($element) == 'array') {
            findPagesInArray($element);
        }

        // Reached the Pages..
        if($index == 'Pages') {
            // Do your task here
        }
    }
}

And you would now use it by calling findPagesInArray($json_object)

Saturday, May 29, 2021
 
BartmanEH
answered 5 Months ago
47

If you define your array like this:

string[][] table = new string[][] {
                       new string[] { "aa", "aaa" },
                       new string[]{ "bb", "bbb" }
};

Then you can use a foreach loop on it.

Wednesday, July 28, 2021
 
Dunc
answered 3 Months ago
25

Do you mean something like:

foreach($_POST['something'] as $key => $something) { 
    $example = $_POST['example'][$key];
    $query = mysql_query("INSERT INTO table (row, row2) VALUES ('{$something}','{$example}')"); 
} 
Wednesday, August 11, 2021
 
PeanutsMcgee
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share