0

I have this array:

Array
(
    [France] => Array
        (
            [0] => Array
                (
                    [city] => Paris
                )
        )
    [Canada] => Array
        (
            [0] => Array
                (
                    [city] => Montreal
                )

            [1] => Array
                (
                    [city] => Ottawa
                )
        )
)

Sometimes, like you can see a country can have one city (case for France) but sometimes the country can have more than one city (case for Canada).

I'm looking to have this final output:

<table>
    <thead>
        <tr>
            <th>Country</th>
            <th>Cities</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td rowspan="1">France</td>
            <td>Paris</td>
        </tr>
        <tr>
            <td rowspan="2">Canada</td>
            <td>Montreal</td>
        </tr>
        <tr>
            <td>Toronto</td>
        </tr>
    </tbody>
</table>

Here's what I have actually:

foreach($countries as $country => $city) {
    $count = count($country) ;

    if($count == 1) {
        echo '
            <tr>
                <td rowspan="1">'.$country.'</td>
                <td>'.$city.'</td>
            </tr>
        '
    }
    else {
        echo '
            <tr>
                <td rowspan="'.$count.'">'.$country.'</td>
                <td>'.$city.'</td>
            </tr>
            <tr>
                <td>'.$city.'</td>
            </tr>
        '
    }      
}

My problem is the loop and how to print the cities.

Thanks for any help.

4
  • 1
    I'm tired of the "I have this array, I want this output, do it all for me" questions. SHOW SOME EFFORT FIRST. Commented Jan 4, 2018 at 15:23
  • You're right. I will update my post to show my example. Commented Jan 4, 2018 at 15:26
  • @IncredibleHat, updated. Commented Jan 4, 2018 at 15:29
  • You should check the count of $cities not country. Because you are deciding based on the number of cities in a country. Commented Jan 4, 2018 at 15:33

4 Answers 4

3

Because you are creating a row for each city, you must loop over each city. Determining the rowspan value and whether to show the first city on the same row is simply based upon whether its the first iteration of the country's cities.

So the following code will produce your desired result:

<?php
$array = [
    'France' => [
        [
            'city' => 'Paris'    
        ]    
    ], 
    'Canada' => [
        [
            'city' => 'Montreal'    
        ], [
            'city' => 'Ottawa'    
        ],
    ],
];
?>
<table>
    <thead>
        <tr>
            <th>Country</th>
            <th>Cities</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($array as $country => $city): ?>
        <?php foreach (array_values($city) as $i => $value): ?>
        <tr>
            <?php if ($i === 0): ?>
            <td rowspan="<?= count($city) ?>"><?= $country ?></td>
            <?php endif ?>
            <td><?= $value['city'] ?></td>
        </tr>
        <?php endforeach ?>
        <?php endforeach ?>
    </tbody>
</table>

https://3v4l.org/vp2bl

Result:

<table>
    <thead>
        <tr>
            <th>Country</th>
            <th>Cities</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td rowspan="1">France</td>
            <td>Paris</td>
        </tr>
        <tr>
            <td rowspan="2">Canada</td>
            <td>Montreal</td>
        </tr>
        <tr>
            <td>Ottawa</td>
        </tr>
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

1 Comment

Why do you use : sign? First time I will use it in php.
0

For rawspan you have to check the count for the city not the count of the country. You have to use two foreach one to country and one to city. Below code is written using php7, for array it uses []. if you are using php 5.6 use the array definition as array().

    <table>
    <thead>
        <tr>
            <th>Country</th>
            <th>Cities</th>
        </tr>
    </thead>
    <tbody>
        <?php $array =  ['france' => ['paris'], 'canada' => ['montreal', 'ottawa']]; ?>
        <?php foreach ($array as $country => $cities) { ?>
            <tr>
                <td rowspan="<?php echo (count($cities)) ?>"><?php echo $country ?></td>
                <?php foreach ($cities as $index => $city) { ?>
                    <?php if ($index === 0) { ?>
                        <td><?php echo $city ?> </td></tr>
                <?php } else { ?>
                    <tr>
                        <td><?php echo $city ?></td>
                    </tr>
                <?php } ?>
            <?php } ?>
        <?php } ?>
    </tbody>
</table>

2 Comments

works as-is, but its not the same array structure as the OPs, and breaks when it is.
Use the arrray as $array = ['France' => [['city' => 'Paris']], 'Canada' => [['city' => 'Montreal'], ['city' => 'Ottawa']]]; For this php can use array_values Refer the next answer added.
0
<thead>
    <tr>
        <th>Country</th>
        <th>Cities</th>
    </tr>
</thead>
<tbody>
    <?php $array = ['France' => [['city' => 'Paris']], 'Canada' => [['city' => 'Montreal'], ['city' => 'Ottawa']]]; ?>
    <?php foreach ($array as $country => $cities) { ?>
        <tr>
            <td rowspan="<?php echo (count($cities)) ?>"><?php echo $country ?></td>
            <?php foreach (array_values($cities) as $index => $value) { ?>
                <?php if ($index === 0) { ?>
                    <td><?php echo $value['city'] ?> </td></tr>
            <?php } else { ?>
                <tr>
                    <td><?php echo $value['city'] ?></td>
                </tr>
            <?php } ?>
        <?php } ?>
    <?php } ?>
</tbody>

Comments

-1

Try this. it should work fine

 <?php
$france = array();
$canada = array();
$city1 = array('city'=>'Paris');
$city2 = array('city'=>'Montreal');
$city3 = array('city'=>'Ottawa');
array_push($france,$city1);
array_push($canada,$city2);
array_push($canada,$city3);
$country = array('France'=>$france,'Canada'=>$canada);
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<th>Country </th><th> City </th>
</thead>
<tbody><?php
$flag=0;
foreach($country as $cN=>$city):?>
<tr><td rowspan="<?php echo count($city);?>"> <?php echo $cN;?></td> 
<?php
    if(count($city)>1)
    {
            foreach($city as $ckey=>$name):
            if($flag)
            {
            ?>
            <tr><td> <?php  echo $name['city'];?></td></tr><?php
            }
            else
            {?>
                <td> <?php  echo $name['city'];?></td><?php
            }
            $flag = 1;
            endforeach; 
    }
    else
    {
        foreach($city as $ckey=>$name):?>
        <td> <?php  echo $name['city'];?></td><?php
        endforeach; 
    }
?></tr><?php
endforeach;
?></tbody></table>

2 Comments

Breaks on second city.
looping city depends on how array is formed. updated the answer with array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.