Jump to content

comparing two dimensional array


Endrew

Recommended Posts

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.
Link to comment
Share on other sites

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.

[code]<?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);
?>[/code]

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.