jump to navigation

Some arrays operations September 17, 2008

Posted by Nsh15 in php, PHP Arrays, Variables and Data.
Tags: , , , , , ,
add a comment

Summing arrays:

To add all the elements in the array, use this statement:

$sum_result= array_sum($array_name);

If u try to add some elements which is not numbers, PHP will autiomatically convert them to number with the zero value!

Example:

<?php
$sum_array= array (1,2,3,4);
$sum_result = array_sum($sum_array);

echo “The sum of all elements in the \$sum_array = “,$sum_result;
?>

The output:

The sum of all elements in the $sum_array = 10

Removing duplicate items:

to remove duplicate items, use the following statement:

$array_without_duplicate_values= array_unique($array_name);

Example:

<?php

$aaray_with_duplicate= array (“A1″,”B1″,”C1″,”A1″);
$aaray_without_duplicate= array_unique($aaray_with_duplicate);

echo “The array with duplicate elements <br><pre>”;
print_r($aaray_with_duplicate);
echo “</pre>”;
echo “The array without duplicate elements<br><pre>”;
print_r($aaray_without_duplicate);
echo “</pre>”;
?>

The output:

The array with duplicate elements

Array
(
    [0] => A1
    [1] => B1
    [2] => C1
    [3] => A1
)

The array without duplicate elements

Array
(
    [0] => A1
    [1] => B1
    [2] => C1
)

Exchanging keys and values:

To exchange values and keys in an arrays:

$array_exchanged= array_flip($array_name);

Example:

<?php
$array_to_flip = array (“A1″,”B1″,”C1″);
$array_exchanged= array_flip($array_to_flip);
echo “The array before fliping its value with its keys<br><pre>”;
print_r($array_to_flip);
echo “</pre>”;
echo “The array after fliping its value with its keys<br><pre>”;
print_r($array_exchanged);
echo “</pre>”;
?>

The output:

The array before fliping its value with its keys

Array
(
    [0] => A1
    [1] => B1
    [2] => C1
)

The array after fliping its value with its keys

Array
(
    [A1] => 0
    [B1] => 1
    [C1] => 2
)

Finding array size:

u can use count or sizeof statement to find the size of an array

$size_value= count($array_name);

$size_value= sizeof($array_name);

the number of elements will be stored in $size_value for each statement!

Example:

<?php

$array_to_find_size= array (“A1″,”B1″,”C1″,”A2″,”B2″,”C2″);
$size_count = count($array_to_find_size);
$size_sizeof= sizeof($array_to_find_size);

echo “Size of \$ array_to_find_size using count : “, $size_count,
“<br>Size of \$ array_to_find_size using sizeof : “, $size_sizeof;
?>
The output:


Size of $ array_to_find_size using count : 6
Size of $ array_to_find_size using sizeof : 6

comparing arrays September 17, 2008

Posted by Nsh15 in php, PHP Arrays, Variables and Data.
Tags: , , , , , , ,
add a comment

can identify the elements that r the same or the elements that r different.

To find different elements:

$difference_array= array_diff($array1, $array2, ….);

$difference_array contains the elements in array1 and not in array2 or the other arrays.

The order in which u list the arrays make a diffrenence.

To find arrays elements that differ in either value or key, use this statement:

$difference_array= array_diff_assoc($array1, $array2, ….);

To find same elements:

U can find the same elements in two arrays or more using:

$similar_array= array_intersect($array1, $array2, ….);

If u want both the value and the key to be the same, use:

$similar_array= array_intersect_assoc($array1, $array2, ….);

Example:

<?php

$array1_diff= array (“A1″,”B1″,”C1″,”A2″,”B2″,”C2″);
$array2_diff= array (“A1″,”B1″,”C1″);
$array3_diff= array (“A1″,”B1″,”C1″,”B2″,”C2″);

$diff= array_diff($array1_diff,$array2_diff,$array3_diff);

echo “The difference: <br> <pre>”;
print_r($diff);
echo “</pre>”;

$same= array_intersect($array1_diff,$array2_diff,$array3_diff);
echo “<br >The same elements: <br> <pre>”;
print_r($same);
echo “</pre>”;

?>

The output:

The difference:

Array
(
    [3] => A2
)


The same elements:

Array
(
    [0] => A1
    [1] => B1
    [2] => C1
)

splitting and merging arrays September 17, 2008

Posted by Nsh15 in php, PHP Arrays, Variables and Data.
Tags: , , , , , ,
add a comment

PHP allows u to put arrays together or take ‘em a part.

Splitting arrays:

u can split an array by creating a new array that contains a subset of an existing array.

$sub_array_name = array_slice($array_name,n1,n2);

where n1: the sequence number of the element in where the new array should start & n2: the length of the new array.

Example:

<?php
$big_array= array(“A1″,”B1″,”C1″,”A2″,”B2″,”C2″);
$sub_array = array_slice ($big_array,0,4);

echo “we want the sub array to contain the first 4 elements from the big_array”;
echo “<pre>”;
print_r($sub_array);
echo “</pre>”;
?>

The output:

we want the sub array to contain the first 4 elements from the big_array

Array
(
    [0] => A1
    [1] => B1
    [2] => C1
    [3] => A2
)

Merging arrays:

Conversely; u can merg two or more array together using this statement:

$big_array = array_merg($array1,$array2, …. );

u can merge arrays with number keys or words; As well!

However, for the word key if the keys r the same in any of the elements the later element will overwrite the first element and u will lose ur first element !

But the case is not the same for the numbers key, bcz it will give each a unique number!!!

Example:

<?php

$array1["A"]= “AAA”;
$array1["B"]= “BBB”;
$array2["A"]= “aaa”;

$array_12= array_merge($array1,$array2);
echo “When there is two identical string keys the last one will
over write the first element<br>”;
echo “<pre>”;
print_r($array_12);
echo “</pre>”;

$array11= array (“A1″,”B1″,”C1″);
$array22= array (“A2″,”B2″,”C2″);

$array_1212= array_merge($array11,$array22);
echo “When there is two identical number keys, they will be given another key value<br>”;
echo “<pre>”;
print_r($array_1212);
echo “</pre>”;

?>

The output:

When there is two identical string keys the last one will over write the first element

Array
(
    [A] => aaa
    [B] => BBB
)

When there is two identical number keys, they will be given another key value

Array
(
    [0] => A1
    [1] => B1
    [2] => C1
    [3] => A2
    [4] => B2
    [5] => C2
)

Note:

If u need to merge arrays that have identical arrays, use

array_merge_recursive which creates multidimensional array when keys r identical (u won’t lose any elements this way)!

converting variables into arrays (and vice versa) September 17, 2008

Posted by Nsh15 in php, PHP Arrays, Variables and Data.
Tags: , , , , , ,
3 comments

sometimes u want the information in an array atored in variables that u can use in PHP statements or u need vaiables converted to array elements.

Convert array into variables:

Using the extract statement u can retrieve all the values from an array and insert each value into variables by using the key for the variable name. In other words, each array is copied into a variable named for the key.

extract($array_name);

Example:

<?php
$array_to_extract= array (“first_element” => “ABC”,
“second_element” => “DEF”, “third_element” => “GHI”);
extract($array_to_extract);

echo “After extracting the array it will gives us the following variables <br>”;
echo “First element is “, $first_element, “<br>”;
echo “Second element is “, $second_element, “<br>”;
echo “Third element is “, $third_element, “<br>”;
?>

The output:

After extracting the array it will gives us the following variables
First element is ABC
Second element is DEF
Third element is GHI

Convert variables into arrays:

Conversely, u can also convert a group of simple variables into an array using compact statemenet which copies the value from each specific variable name into an array element

$array_name= compact ($variable_name);

OR

$array_name= compact ($array_name);

in the first method u use the variable name directly, while in the second method u use an array that contains the names of the variables.

Example:

<?php

$array_index= array (“index1″,”index2″,”index3″);

$index1= “Hi!”;
$index2 = “Welcome 2 my blog”;
$index3 = “Noor”;

$array_to_compact1= compact(“index1″,”index2″,”index3″);
echo “Using the first method <br>”;
echo “<pre>”;
print_r($array_to_compact1);
echo “</pre>”;

$array_to_compact2= compact($array_index);
echo “Using the second method <br>”;
echo “<pre>”;
print_r($array_to_compact2);
echo “</pre>”;
?>

The output:

Using the first method

Array
(
    [index1] => Hi!
    [index2] => Welcome 2 my blog
    [index3] => Noor
)

Using the second method

Array
(
    [index1] => Hi!
    [index2] => Welcome 2 my blog
    [index3] => Noor
)

converting arrays into strings (and vice versa) September 17, 2008

Posted by Nsh15 in php, PHP Arrays, Variables and Data.
Tags: , , , , , ,
3 comments

You can easily converte between arrays and strings, i.e.. u can ‘ve a sentence with each word of it in an array element!

Convert string into array:

u can create an array that contains the contents of a string by using this statement:

$array_name= explode(“s”,String);

  • s: Is the character to use to divide the string
  • String: string itself

Example:

<?php
$string_variable= “Hi:and:welcome:to:my:blog”;
$array_string = explode (“:”,$string_variable);

echo “Now lets print our new array <br>”;
echo “<pre>”;
print_r($array_string );
echo “</pre>”;
?>

The output:

Now lets print our new array

Array
(
    [0] => Hi
    [1] => and
    [2] => welcome
    [3] => to
    [4] => my
    [5] => blog
)

Convert array into a string:

Conversely, u can convert an array into string using:

$variable_name= implode (“s”,$array_name);

s: seprating the text from each array element and then store the string the $variable_name

Example:

<?php

$array_to_be_string= array (“hi”, “and”, “welcome”, “to”, “my”, “blog”);
$string_array= implode (” “,$array_to_be_string);

echo “The new string is <br>”, $string_array;
?>
The output:


The new string is
hi and welcome to my blog

Note: both emplode and implode doensn’t affect the input to the statmentm it just read it !

Follow

Get every new post delivered to your Inbox.