Friday, October 14, 2011

How to find the common values of two array, without using array_intersect


$array1 = array(’3′,’8′,’7′,’2′);
$array2 = array(’4′,’5′,’7′,’2′);
$length1=count($array1);
$length2=count($array2);
$common=array();
if ($length1 > $length2)
{
for ($i=0; $i<$length1; $i++)
{
if (in_array($array1[$i], $array2))
{
$common[]=$array1[$i];
}
}
}
else
{
for($i=0; $i<$length2; $i++)
{
if (in_array($array2[$i], $array1))
{
$common[]=$array2[$i];
}
}
}
print ‘
’;

print_r($common);
?>

category sub category program


mysql_connect("localhost","root","");
mysql_select_db("test");
class Categories
{
public function categories_tree($country_id='',$parent='0',$spacer='')
{
$sql = mysql_query("SELECT * FROM categories WHERE parent='$parent'");
$num = mysql_num_rows($sql);
$spacer .= '- ';
while ($row = mysql_fetch_array($sql))
{
if ($num == 0 || $row['parent']=='0')
{
$spacer = '';
}
echo $spacer.'‘.$row['name'].’
‘;
$this->categories_tree($country_id, $row['id'], $spacer);
}
}
}
$cat = new Categories();
$a = ”;
echo $cat->categories_tree(0, $a);
?>