Home > Archive > PHP Language > August 2007 > intersect not work on array object; is there a solution
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
intersect not work on array object; is there a solution
|
|
|
| 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($arr
ay1, $array2 );
$arr_intersez_a2_a1=array();
$arr_intersez_a2_a1=array_intersect($arr
ay2, $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>
| |
| ZeldorBlat 2007-07-12, 6:59 pm |
| On Jul 12, 8:54 am, artev <mailnotspa...@notspamm.nn> wrote:
> 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)
>
The solution is to not multi-post.
| |
|
| Il Thu, 12 Jul 2007 13:56:44 -0000, ZeldorBlat ha scritto:
> On Jul 12, 8:54 am, artev <mailnotspa...@notspamm.nn> wrote:
>
> The solution is to not multi-post.
I write at php groups (three), but
for this problem, which is the better newsgroup?
| |
| ZeldorBlat 2007-07-12, 6:59 pm |
| On Jul 12, 11:10 am, artev <mailnotspa...@notspamm.nn> wrote:
> Il Thu, 12 Jul 2007 13:56:44 -0000, ZeldorBlat ha scritto:
>
>
>
> I write at php groups (three), but
> for this problem, which is the better newsgroup?
It's ok to write to all of them -- but do a cross-post instead of a
multi-post. It's actually less work for you and you won't piss
everybody off. If you don't understand the difference read this (it's
one of many websites on the subject):
<http://oakroadsystems.com/genl/unice.htm#xpost>
| |
| Gufo Rosso 2007-08-11, 10:00 pm |
| artev ha scritto:
> 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';
this !is array is obj
if you have 2 value or more in une array
use "bidimensional array" alias of matrix
>
>
> $array2[0]->id='b';
> $array2[1]->id='e';
>
>
bidimensional array:
$pippo['a'][0] = "pippo0";
$pippo['a'][1] = "pippo2";
$pippo['a'][2] = "pippo2";
$pippo['a'][3] = "pippo3";
or
$pippo['a'] = array("pippo","qui","quo",...)
>
>
> $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($arr
ay1, $array2 );
>
>
> $arr_intersez_a2_a1=array();
> $arr_intersez_a2_a1=array_intersect($arr
ay2, $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>
<?
$array1[0]='a'; // array
$array1[1]='b';
$array1[2]='c';
$array1[3]='d';
class a_rray{
var $me=array();
function intersect_name($value,$value2){
$varname = array_intersect($value,$value2);
$str ="";
foreach($varname as $key =>$value){
$str .= $key." ".$value;
}
return $str;
}
}
$array2[0]='b';
$array2[1]='e';
$cc= new a_rray();
echo $cc->intersect_name($array1,$array2);
please read manual:
array
array_function
class
?>
|
|
|
|
|