|
Posted by Steve on 07/18/07 23:44
both functions ARE working!!!
each element of BOTH arrays are different objects. they all have memory
addresses that are different. if you expect php to use esp to somehow know
that 'id' is the interface of each object by which it should determine merge
or uniqueness, think again.
try this:
class foo
{
var $bar = 'hello world';
function foo($message = null)
{
if (is_null($message)){ return; }
$bar = $message;
}
}
$sameFoo = new foo();
$arrayOne[] = $sameFoo;
$arrayOne[] = new foo('sup doc');
$arrayTwo[] = $sameFoo;
$arrayTwo[] = new foo('y i odda');
$mergedArray = array_merge($arrayOne, $arrayTwo);
echo "<pre>" . print_r($mergedArray, true) . "</pre>";
and so on...
you'll see everything works just fine.
"artev" <mailnotspammm@notspamm.nn> wrote in message
news:1hs6nv5pqln89$.1t6awdy18p5t6$.dlg@40tude.net...
| if I have 2 array a1 and a2 that are array object: array_intersect not
| work; is there a solution? also array_unique not work
| (vers. php: 4.3.9)
|
|
| code
|
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
| "http://www.w3.org/TR/html4/loose.dtd">
| <html> <head> <meta http-equiv="Content-Type" content="text/html;
| charset=iso-8859-1">
|
| <?php
| $array1[0]->id='a';
| $array1[1]->id='b';
| $array1[2]->id='c';
| $array1[3]->id='d';
|
|
| $array2[0]->id='b';
| $array2[1]->id='e';
|
|
|
|
| $arr_merge=array();
| $arr_merge=array_merge($array1, $array2 );
|
| $arr_merge_uniq=array();
| $arr_merge_uniq=array_unique($arr_merge);
|
|
| $arr_intersez_a1_a2=array();
| $arr_intersez_a1_a2=array_intersect($array1, $array2 );
|
|
| $arr_intersez_a2_a1=array();
| $arr_intersez_a2_a1=array_intersect($array2, $array1 );
|
|
|
|
| echo "<table border='1' cellpadding='4' cellspacing='0' >";
| echo "<tr>";
| echo "<td>ARRAY a1</td>";
| echo "<td>ARRAY a2</td>";
| echo "<td>MERGE</td>";
| echo "<td>MERGE <br> UNIQUE</td>";
|
| echo "<td>INTERSECT a1 a2</td>";
| echo "<td>INTERSECT a2 a1</td>";
| echo "</tr>";
|
| echo "<tr>";
| //ARRAY 1
| echo "<td valign='top'>";
| foreach($array1 as $value) {echo "($value->id)"."<br>";}
| echo "</td>";
| //ARRAY 2
| echo "<td valign='top'>";
| foreach($array2 as $value) {echo "($value->id)"."<br>";}
| echo "</td>";
| //MERGE
| echo "<td valign='top'>";
| foreach($arr_merge as $value) {echo "($value->id)"."<br>";}
| echo "</td>";
|
| //MERGE UNIQUE
| echo "<td valign='top'>";
| foreach($arr_merge_uniq as $value) {echo "($value->id)"."<br>";}
| echo "</td>";
|
| //INTERSECT a1 a2
| echo "<td valign='top'>";
| foreach($arr_intersez_a1_a2 as $value) {echo "($value->id)"."<br>";}
| echo "</td>";
|
| //INTERSECT a2 a1
| echo "<td valign='top'>";
| foreach($arr_intersez_a2_a1 as $value) {echo "($value->id)"."<br>";}
| echo "</td>";
|
| echo "</tr>";
| echo "</table>";
|
| ?>
| </head>
| <BODY>
|
| </BODY>
| </html>
[Back to original message]
|