1
<?php
    $MCA2DIVA=array('Sourabh','Akash','Anand','Karan','Amit');
    $MCA2DIVB=array('Komal','Ruchita','Akash','Amit','Pratik');
?>
<html>
    <head>
        <style>
            table
            {
                border:2px solid;
                background-color:#acbe4a;
                position:absolute;
                top:25%;
                left:30%;
                height:40%;
                width:10%;
                border-radius:10px;
                font-weight:bold;
                font-size:17px;
            }
            td
            {
                border:2px solid;
                text-align:center;
                border-radius:8px;
            }
            p
            {
                position:absolute;
                font-weight:bold;
                font-size:25px;
                left:10%;
            }
            p#p1
            {
                top:5%;
            }
            p#p2
            {
                top:8%;
            }
        </style>
    </head>
    <body bgcolor='#daf05e'>
        <p id=p1>
            <?php
                echo "Array 1 : ";
                print_r($MCA2DIVA);
            ?>
        </p>
        <p id=p2>
            <?php
                echo "Array 2 : ";
                print_r($MCA2DIVB);
            ?>
        </p>
        <?php
            $final=array_unique(array_merge($MCA2DIVA,$MCA2DIVB));
        ?>
        <br>
        <table>
            <?php
                for($i=0;$i<sizeof($final);$i++)
                {
            ?>
        <tr><td>
            <?php
                print_r($final[6]);
            ?>
        </tr></td>
            <?php
                }
            ?>
        </table>
    </body>
</html>

This is my php code. I want to merge two arrays removing the duplicates and show it in a tabular format. But the array_unique function is displaying the necessary output except the last element in the array. When I try printing single array values, then it displays the desired values. Please help me find the problem.

1
  • 2
    What do you expect? Your arrays contain the duplicate value Amit, so it will be removed: the merged array will then contain just 9 items instead of 10 after being passed through array_unique() Commented Oct 10, 2017 at 6:32

2 Answers 2

2

Output of array_merge() with array_unique():- https://eval.in/876892

So you will see that last value have index 9 not 6.

So Instead of using for loop use foreach() because it takes care of indexes itself.

print all values one-by-one:-

 <table>
    <?php
        foreach($final as $value)
        {
    ?>
    <tr><td>
        <?php
            echo $value;
        ?>
    </tr></td>
    <?php
        }
    ?>
</table>

And if you want to only show last value then (no need of loop):-

 <table>

    <tr><td>
        <?php
            echo $final[count($final)-1];
        ?>
    </tr></td>

</table>

Note:-

If you want to use for loop then you have to re-index your array using array_values() like this:- https://eval.in/876905

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot man!! This was the only code due to which my assignment was pending. It worked well!!
foreach works. But do you have any idea that due to the for loop why was the index of the last element 9, whereas it should be 7 if you count from 0?
it's because of array_merg() . you can use array_values() there to re-index your array like :- array_values(array_unique(array_merge( your two array))):- eval.in/876905
array_values(array_unique(array_merge( your two array))) also worked perfect using for loop. Thanks!
0
<?php

$foo = array('Sourabh','Akash','Anand','Karan','Amit');
$bar = array('Komal','Ruchita','Akash','Amit','Pratik');

$names = array_unique(array_merge($foo, $bar));
sort($names);

?>   
<table>
    <thead>
        <tr>
            <th>Names</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($names as $name) { ?>
            <tr>
                <td><?= $name ?></td>
            </tr>
        <?php } ?>
    </tbody>
</table>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.