IT Community - Software Programming, Web Development and Technical Support

Using Arrays in PHP

This is a discussion on Using Arrays in PHP within the PHP Programming forums, part of the Web Development category; array_reduce array_reduce — Iteratively reduce the array to a single value using a callback function Example: array_reduce() <?php function rsum($...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #41 (permalink)  
Old 08-18-2007, 04:43 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_reduce

array_reduce — Iteratively reduce the array to a single value using a callback function

Example: array_reduce()

<?php
function rsum($v, $w)
{
$v += $w;
return $v;
}

function rmul($v, $w)
{
$v *= $w;
return $v;
}

$a = array(1, 2, 3, 4, 5);
$x = array();
$b = array_reduce($a, "rsum");
$c = array_reduce($a, "rmul", 10);
$d = array_reduce($x, "rsum", 1);
?>


This will result in
$b containing 15,
$c containing 1200 (= 10*1*2*3*4*5),
and $d containing 1.

__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:54 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #42 (permalink)  
Old 08-18-2007, 04:46 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_reverse

array_reverse — Return an array with elements in reverse order

Example: array_reverse()

<?php
$input = array("php", 4.0, array("green", "red"));
$result = array_reverse($input);
$result_keyed = array_reverse($input, true);
?>


This makes both $result and $result_keyed have the same elements, but note the difference between the keys. The printout of $result and $result_keyed will be:

Array
(
[0] => Array
(
[0] => green
[1] => red
)

[1] => 4
[2] => php
)
Array
(
[2] => Array
(
[0] => green
[1] => red
)

[1] => 4
[0] => php
)
__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:55 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #43 (permalink)  
Old 08-18-2007, 04:52 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_shift

array_shift — Shift an element off the beginning of array

Example: array_shift()

<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);
?>

This would result in $stack having 3 elements left:

Array
(
[0] => banana
[1] => apple
[2] => raspberry
)
and orange will be assigned to $fruit.
__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:55 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #44 (permalink)  
Old 08-18-2007, 04:54 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_unshift

array_unshift — Prepend one or more elements to the beginning of an array

Example: array_unshift() example

<?php
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
?>


The above example will output:

Array
(
[0] => apple
[1] => raspberry
[2] => orange
[3] => banana
)
__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:56 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #45 (permalink)  
Old 08-18-2007, 04:55 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_unique

array_unique -- Removes duplicate values from an array

Example array_unique()

$input = array ("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique ($input);
print_r($result);


This will output:

Array
(
[b] => green
[1] => blue
[2] => red
)

__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:57 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #46 (permalink)  
Old 08-18-2007, 04:57 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_pad

array_pad -- Pad array to the specified length with a value

Example: array_pad()

$input = array (12, 10, 9);

$result = array_pad ($input, 5, 0);
// result is array (12, 10, 9, 0, 0)

$result = array_pad ($input, -7, -1);
// result is array (-1, -1, -1, -1, 12, 10, 9)

$result = array_pad ($input, 2, "noop");
// not padded


__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:57 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #47 (permalink)  
Old 08-18-2007, 04:58 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_pop

array_pop -- Pop the element off the end of array

Example: array_pop()

$stack = array ("orange", "banana", "apple", "raspberry");
$fruit = array_pop ($stack);



After this, $stack will have only 3 elements:
Array
(
[0] => orange
[1] => banana
[2] => apple
)

and rasberry will be assigned to $fruit.
__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:58 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #48 (permalink)  
Old 08-18-2007, 04:59 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_push

array_push -- Push one or more elements onto the end of array

Example: array_push()

$stack = array ("orange", "banana");
array_push ($stack, "apple", "raspberry");


This example would result in $stack having the following elements:
Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)
__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:58 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #49 (permalink)  
Old 08-18-2007, 06:26 AM
Naseema Naseema is offline
D-Web Trainee
 
Join Date: Mar 2007
Posts: 12
Naseema is on a distinguished road
Default Re: Using Arrays efficiently in PHP

Sabai really great job.....
its very informative .....
keep on dooing this
__________________
Nasee
True Faith never Fails
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #50 (permalink)  
Old 08-20-2007, 08:48 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_udiff

array_udiff — Computes the difference of arrays by using a callback function for data comparison

Example: array_udiff()

<?php
class cr {
private $priv_member;
function cr($val)
{
$this->priv_member = $val;
}

function comp_func_cr($a, $b)
{
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member)? 1:-1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);

$result = array_udiff($a, $b, array("cr", "comp_func_cr"));
print_r($result);
?>


The above example will output:

Array
(
[0.5] => cr Object
(
[priv_member:private] => 12
)

[0] => cr Object
(
[priv_member:private] => 23
)

)
__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:59 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #51 (permalink)  
Old 08-20-2007, 08:50 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_udiff_assoc

array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function

Example :array_udiff_assoc()

<?php
class cr {
private $priv_member;
function cr($val)
{
$this->priv_member = $val;
}

function comp_func_cr($a, $b)
{
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member)? 1:-1;
}
}

$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);

$result = array_udiff_assoc($a, $b, array("cr", "comp_func_cr"));
print_r($result);
?>


The above example will output:

Array
(
[0.1] => cr Object
(
[priv_member:private] => 9
)

[0.5] => cr Object
(
[priv_member:private] => 12
)

[0] => cr Object
(
[priv_member:private] => 23
)
)

__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:51 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #52 (permalink)  
Old 08-20-2007, 08:53 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_udiff_uassoc

array_udiff_uassoc — Computes the difference of arrays with additional index check, compares data and indexes by a callback function

Example: array_udiff_uassoc()


<?php
class cr {
private $priv_member;
function cr($val)
{
$this->priv_member = $val;
}

function comp_func_cr($a, $b)
{
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member)? 1:-1;
}

function comp_func_key($a, $b)
{
if ($a === $b) return 0;
return ($a > $b)? 1:-1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);

$result = array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key"));
print_r($result);
?>


The above example will output:

Array
(
[0.1] => cr Object
(
[priv_member:private] => 9
)

[0.5] => cr Object
(
[priv_member:private] => 12
)

[0] => cr Object
(
[priv_member:private] => 23
)
)
__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:52 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #53 (permalink)  
Old 08-20-2007, 08:54 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_uintersect_assoc

array_uintersect_assoc — Computes the intersection of arrays with additional index check, compares data by a callback function

Example: array_uintersect_assoc()

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");

print_r(array_uintersect_assoc($array1, $array2, "strcasecmp"));
?>


The above example will output:

Array
(
[a] => green
)

__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:52 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #54 (permalink)  
Old 08-20-2007, 09:01 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_uintersect_uassoc

array_uintersect_uassoc — Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions

Example: array_uintersect_uassoc()


<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");

print_r(array_uintersect_uassoc($array1, $array2, "strcasecmp", "strcasecmp"));
?>


The above example will output:

Array
(
[a] => green
[b] => brown
)

__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:53 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #55 (permalink)  
Old 08-20-2007, 09:02 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

array_uintersect

array_uintersect — Computes the intersection of arrays, compares data by a callback function

Example: array_uintersect()


<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");

print_r(array_uintersect($array1, $array2, "strcasecmp"));
?>


The above example will output:

Array
(
[a] => green
[b] => brown
[0] => red
)


__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:53 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #56 (permalink)  
Old 08-20-2007, 10:44 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

Sorting Arrays

Sorting means organizing a bunch of values in alphabetical or numerical order. PHP can be used to sort an array by it's keys or values. The result can keep the corresponding key/value with the original value/key or replace them. That sounds a bit confusing, so here are the examples to sort it out.

Here is a sample array

<?php
$pantry = array(
0 => "tomatoes",
1 => "oranges",
2 => "bananas"
3 => "potatoes",
4 => "bread",
5 => "apples"
);
?>


Use the sort command to sort the values with no regard to the keys. The values only will be changed.

sort($pantry);

The pantry array now looks like this...

0 apples
1 bananas
2 bread
3 oranges
4 potatoes
5 tomatoes

The values have changed places into alphabetical order while the keys remain in the same order.

rsort()

To sort the values in reverse order with no regard to the keys, the rsort command is used.

The pantry array now looks like this...
0 tomatoes
1 potatoes
2 oranges
3 bread
4 bananas
5 apples

The values have changed places into alphabetical order while the keys remain in the same order.

asort()

To sort the values and keep the corresponding keys, the asort command is used.

asort($pantry);

The pantry array now looks like this...

5 apples
2 bananas
4 bread
1 oranges
3 potatoes
0 tomatoes

arsort()

A similar sorting with keys technique can also be done in the reverse order using the arsort command.

arsort($pantry);

The pantry array now looks like this...

0 tomatoes
3 potatoes
1 oranges
4 bread
2 bananas
5 apples

ksort()

To sort the keys and keep the values, the ksort command is used.

ksort($pantry);

The pantry array now looks like this...

0 tomatoes
1 oranges
2 bananas
3 potatoes
4 bread
5 apples

In this case, it remains the same as the original array considering the keys were already in numerical order.

krsort()

To sort the keys and keep the values in reverse order, the krsort command is used.

krsort($pantry);

The pantry array now looks like this...

5 apples
4 bread
3 potatoes
2 bananas
1 oranges
0 tomatoes

shuffle()

The shuffle command is used to randomly reorganize the values of an array. The keys remain the same.

shuffle($pantry);

The pantry array now looks like this...

0 apples
1 potatoes
2 bananas
3 oranges
4 bread
5 tomatoes
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #57 (permalink)  
Old 08-21-2007, 03:09 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Question how do i get the last value of an array?

hi,

how do i get the last value of an array?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #58 (permalink)  
Old 08-21-2007, 03:29 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: Using Arrays in PHP

hi raj,

you can use the array_pop() function. This function returns last value of array. If array is empty returns NULL. And original array is end of element is Off.

Eg:

$stack = array("bus", "car", "cycle");
$val = array_pop($stack);

echo $val; // Output : cycle

print_r($stack);
Array(
[0] = 'bus'
[1] = 'car'
)

--R.kamalakannan
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #59 (permalink)  
Old 08-21-2007, 03:29 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP


very simple Mr.Raj,

suppose we can take this array
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($fruit);


the above print statement will return the last array element, like this

raspberry
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #60 (permalink)  
Old 08-21-2007, 03:29 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Thumbs up Info: Delete particular index & their values from an Array?

hi all,

here i have given sample code for delete the array index & their values...

eg:
this loop to delete the empty value of an array


foreach($aResult as $vKey => $Value)
{
//#-- Checking the Value is empty or not
if(is_null($Value)) unset($aResult[$vKey]);
}
//# unset function to destroy the element
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/php-programming/3298-using-arrays-php.html
Posted By For Type Date
mrajendhran's bookmarks tagged with This thread Refback 08-30-2007 12:04 PM
DiscussWeb IT Community - Technical Support and Technology Discussions This thread Refback 08-23-2007 09:11 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Jagged Arrays in C# vigneshgets C# Programming 4 09-30-2008 03:48 AM
Classic asp arrays and recordset ramesh123 ASP and ASP.NET Programming