|
Posted by Gufo Rosso on 08/12/07 01:32
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($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>
<?
$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
?>
Navigation:
[Reply to this message]
|