# comparing two dimensional array

### #1 Endrew

Endrew
• Members
• Member
• 15 posts

Posted 01 May 2006 - 02:05 AM

I'm having difficulty comparing 2 dimensional arrays.

coding :-
foreach(\$ary1 as \$row)
foreach(\$row as \$col=>\$val)
.............
.............

I like to add one more array to make comparison. But foreach takes only one array. Is there anyway I can accomplish this.

### #2 kenrbnsn

kenrbnsn
• Staff Alumni
• 8,235 posts
• LocationHillsborough, NJ, USA

Posted 01 May 2006 - 03:30 AM

The foreach() statement takes one dimension at a time.

One way to compare two 2-d arrays is to create two temporary arrays consisting of the indices from the original arrays and the serialized content of the 2nd dimension. The compare the temporary arrays.

```<?php
\$ary1 = array(array('1','2','3','4'),array('one','two','three','four'),array('1','one','two','2','3','three'));
\$ary2 = array(array('1','one','two','2','3','three'),array('one','two','three','four'),array('1','2','3','4','5'));
\$tmp1 = array();
\$tmp2 = array();
foreach (\$ary1 as \$k => \$v)
\$tmp1[\$k] = serialize(\$v);
foreach (\$ary2 as \$k=> \$v)
\$tmp2[\$k] = serialize(\$v);
\$result = array_diff(\$tmp1,\$tmp2);
?>```

Ken

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users