Sourabh Karmarkar
Sourabh Karmarkar

Reputation: 87

PHP array_unique() function

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

Upvotes: 1

Views: 157

Answers (2)

Progrock
Progrock

Reputation: 7485

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

Upvotes: 0

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

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

Upvotes: 2

Related Questions