Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How output duplicate elements in multi dimensional array ???
Old 09-28-2011, 10:25 PM How output duplicate elements in multi dimensional array ???
Novice Talker

Posts: 10
Trades: 0
Hello Everyone, I been teaching myself PHP off and on for nearly five months after work .. that's what makes the easy PHP hard to learn. Anyway, I been working with this code for days and nothing I tried will output duplicates to screen. Somewhere during the process I got hook on doing it with a *loop*. I found tons of working code while googling but not one of them worked for multi dimensional array and the tutorials don't seem to go into details like it does for everything else PHP. Maybe I just plain slow. Anyway, I hope someone can help me solve this problem. If not, you guys and gals are stuck with me anyway because my PHP forum quest is over and I know for a fact that Google can't find/list everything.

Thanks in advance for any help.

btw: The default code font being used is blinding.

Code:
<html><head><title></title></head>
<body>

<?php

$_ARRAY=array(array("Joye",1111,"Cake"),        //  MULTIDIMENSIONAL ARRAY:
              array("Earl",7777,"Rake"),
              array("Joye",2222,"Pear"),
              array("Ruth",7777,"Rose"),
              array("Joye",3333,"Cake"),
              array("Wong",7777,"Cake"),  );
?>

<pre>
<?php

echo "Printing All Elements with a Loop::";

echo "<ol>";
    for ($row = 0; $row < 6; $row++) {                            #  6 row
          echo "<li><b>         r-$row</b>";                    #  header
          echo "<ul>";

            for ($col = 0; $col < 3; $col++) {                    #  3 col
                  echo "<li>".$_ARRAY[$row][$col]."</li>"; }    #  print list
    echo "</ul>";
    echo "</li>";
}
echo "</ol>";
## ....................................................
## ....................................................
## ....................................................
echo "Printing All Duplicates using a Loop ... No luck:: :( ";
     echo "\n";
     echo "\n";
echo("Testing:; Manually printing a dupicate number (row1-col2){$_ARRAY[1][2]}");
     echo "\n";

?>
</pre>
</body>
</html>

max21b is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-28-2011, 11:46 PM Re: How output duplicate elements in multi dimensional array ???
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
PHP Code:
<?php
$_ARRAY
=array(array("Joye",1111,"Cake"),        //  MULTIDIMENSIONAL ARRAY:
              
array("Earl",7777,"Rake"),
              array(
"Joye",2222,"Pear"),
              array(
"Ruth",7777,"Rose"),
              array(
"Joye",3333,"Cake"),
              array(
"Wong",7777,"Cake"),  );

$exists = array();
$duplicates = array();

foreach(
$_ARRAY as $row)
{
    foreach(
$row as $val)
    {
        
//if the value exists and it isn't already in the dupcliates array
        
if(in_array($val$exists) && !in_array($val$duplicates))
            
$duplicates[] = $val;
        else
            
$exists[] = $val;
    }
}

print_r($duplicates);
?>
Edit:
I can think of at least one way that is a little quicker, but this solution should work for reasonably sized arrays.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 09-28-2011 at 11:52 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-29-2011, 04:48 AM Re: How output duplicate elements in multi dimensional array ???
Novice Talker

Posts: 10
Trades: 0
Thank you NullPointer. It works! I did say all values but suppose I wanted to find duplicates in a single column and output each key and all of it's elements. I been searching and reading so much I'm beginning to sound like the php manual to myself. If I could see an sample of each for numbers and strings anything more should come natural after all of this time I been using functions and php commands that would never work for multidimensional arrays. I found this just hours ago. cmonkey said a mouth full: /tycoontalk-12202-searching-in-multi-dimensional-array Now I understand. [0] => Earl => 7777 => Rake [0] => Ruth => 7777 => Rose [0] => Wong => 7777 => Cake &quot;I can think of at lease one way that is a little quicker,&quot; After I get pass knowing how to loop Multi-D, I'll build up my nerve again and ask you HOW someday What a Great Day! Thanks again the values posted is three lines. I can't use links, etc for 10 days.

Last edited by max21b; 09-29-2011 at 04:50 AM..
max21b is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How output duplicate elements in multi dimensional array ???
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27253 seconds with 12 queries