Developers Archive for March, 2007

array_intersect and array_intersect_assoc

array_intersect and array_intersect_assoc Monday, March 19th, 2007

- These both functions are basically used to find the common elements between two arrays.
- Basic difference above function is
array_intersect()
- returns the common elements which are having same value
array_intersect_assuc()
- returns the common elements which are having same key and value.

Example:
$arr1 = array("index1" => "one", "index2" => "two", "index3" => "three", "four");
$arr2 = array("index1" => "one", "three","four");

$resArr = array_intersect_assoc($arr1, $arr2);
print_r($resArr);

$resArr2 = array_intersect($arr1, $arr2);
print_r($resArr2);

In the above example first print_r() will give only one value(one). Because only one element in both arrays are having same key and value.

Second print_r() will give one,three and four.

Continue

Continue Monday, March 19th, 2007

continue is used to control looping structure . continue will skip the rest of the statement in the current loop and continue the next iteration of the loop.

printing odd numbers

<?php
for($i=0;$i<20;$i++)
{
if($i%2==0) continue; // skip the rest of the loop if the number is even
echo $i;
}

?>

continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.

<?php

$i=0;
while($i<20)
{
while($i){
echo $i;
continue 2; //// this will continue the loop while($i<20)
echo ‘ iam not reachable’;
}
echo ‘this cannot be reached’;
}

?>

array_combine

array_combine Monday, March 19th, 2007

array_combine

array_combine

- It creates an array by using one array for keys and another for its values

Syntax:

array array_combine ( array keys, array values )

It returns an array by using the values from the keys array as keys and the values from the values array as the

corresponding values.

It returns FALSE if the number of elements for each array isn’t equal or if the arrays are empty.

Example:

$a = array(’one’, ‘two’, ‘three’);
$b = array(’1′, ‘2′, ‘3′);
$c = array_combine($a, $b);

print_r($c);

The content in the array $c is

Array
(
[one]  => 1
[two]    => 2
[three] => 3
)


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.