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; ArrayIterator::valid ArrayIterator::valid -- Check whether array contains more entries Description bool ArrayIterator::valid ( void ) This function checks if the ...


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

Register FAQ Members List Calendar Mark Forums Read
  #21 (permalink)  
Old 08-16-2007, 11:05 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

ArrayIterator::valid

ArrayIterator::valid -- Check whether array contains more entries
Description
bool ArrayIterator::valid ( void )

This function checks if the array contains any more entries.

ArrayIterator::valid() example
<?php
$array = array('1' => 'one');

$arrayobject = new ArrayObject($array);
$iterator = $arrayobject->getIterator();

var_dump($iterator->valid()); //bool(true)

$iterator->next(); // advance to the next item

//bool(false) because there is only one array element
var_dump($iterator->valid());
?>
__________________
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
  #22 (permalink)  
Old 08-16-2007, 11:06 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

ArrayObject::append

ArrayObject::append -- Appends the value
Description
void ArrayObject::append ( mixed newval )
__________________
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
  #23 (permalink)  
Old 08-16-2007, 11:07 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

ArrayObject::__construct

ArrayObject::__construct -- Construct a new array object
Description
ArrayObject ArrayObject::__construct ( mixed input )

This constructs a new array object. The input parameter accepts an array or another ArrayObject.

ArrayObject::__construct() example
<?php
$array = array('1' => 'one',
'2' => 'two',
'3' => 'three');

$arrayobject = new ArrayObject($array);

var_dump($arrayobject);
?>

The above example will output:

object(ArrayObject)#1 (3) { [1]=> string(3) "one" [2]=> string(3) "two" [3]=> string(5) "three" }
__________________
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
  #24 (permalink)  
Old 08-16-2007, 11:08 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

ArrayObject::count

ArrayObject::count -- Return the number of elements in the Iterator
Description
int ArrayObject::count ( void )
__________________
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
  #25 (permalink)  
Old 08-16-2007, 11:09 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

ArrayObject::getIterator

ArrayObject::getIterator -- Create a new iterator from an ArrayObject instance
Description
ArrayIterator ArrayObject::getIterator ( void )

This function will return an iterator from an ArrayObject.

ArrayObject::getIterator() example
<?php
$array = array('1' => 'one',
'2' => 'two',
'3' => 'three');

$arrayobject = new ArrayObject($array);

$iterator = $arrayobject->getIterator();

while($iterator->valid()) {
echo $iterator->key() . ' => ' . $iterator->current() . "\n";

$iterator->next();
}
?>

The above example will output:

1 => one 2 => two 3 => three
__________________
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
  #26 (permalink)  
Old 08-16-2007, 11:09 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

ArrayObject::offsetExists

ArrayObject::offsetExists -- Returns whether the requested $index exists
Description
bool ArrayObject::offsetExists ( mixed index )
__________________
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
  #27 (permalink)  
Old 08-16-2007, 11:10 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

ArrayObject::offsetGet

ArrayObject::offsetGet -- Returns the value at the specified $index
Description
bool ArrayObject::offsetGet ( mixed index )
__________________
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
  #28 (permalink)  
Old 08-16-2007, 11:11 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

ArrayObject::offsetSet

ArrayObject::offsetSet -- Sets the value at the specified $index to $newval
Description
void ArrayObject::offsetSet ( mixed index, mixed newval )
__________________
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
  #29 (permalink)  
Old 08-16-2007, 11:11 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

ArrayObject::offsetUnset

ArrayObject::offsetUnset -- Unsets the value at the specified $index
Description
void ArrayObject::offsetUnset ( mixed index )
__________________
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
  #30 (permalink)  
Old 08-16-2007, 11:12 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

CachingIterator::hasNext

CachingIterator::hasNext -- Check whether the inner iterator has a valid next element
Description
bool CachingIterator::hasNext ( void )
__________________
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
  #31 (permalink)  
Old 08-16-2007, 11:13 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: How to Use Arrays efficiently in PHP

CachingIterator::next

CachingIterator::next -- Move the iterator forward
Description
void CachingIterator::next ( void )
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #32 (permalink)  
Old 08-16-2007, 11:14 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: How to Use Arrays efficiently in PHP

CachingIterator::rewind

CachingIterator::rewind -- Rewind the iterator
Description
void CachingIterator::rewind ( void )
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #33 (permalink)  
Old 08-16-2007, 11:14 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: How to Use Arrays efficiently in PHP

CachingIterator::__toString

CachingIterator::__toString -- Return the string representation of the current element
Description
string CachingIterator::__toString ( void )
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #34 (permalink)  
Old 08-17-2007, 12:14 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,159
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Info array_map() Function

The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.
Applies the callback to the elements of the given arrays.

Syntax
Quote:
array array_map ( callback callback, array arr1 [, array ...] )
Description

array_map() returns an array containing all the elements of arr1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()

Example

PHP Code:
<?php
function cube($n)
{
    return(
$n $n $n);
}

$a = array(12345);
$b array_map("cube"$a);
print_r($b);
?>
This makes $b have:
Code:
Array
(
    [0] => 1
    [1] => 8
    [2] => 27
    [3] => 64
    [4] => 125
)
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #35 (permalink)  
Old 08-18-2007, 03:18 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: How to Use Arrays efficiently in PHP

Using foreach on Arrays

foreach is a great function that most people miss in the beginning when coding PHP. That's really stupid because it's a useful function for handling with Arrays. Look at this piece of code and I will explain what it does after the code:

$myArray = Array("firstname" => "sabari",
"lastname" => "nathan",
"age" => 28);

foreach($myArray as $key => $value)
{
echo $key . "-". $value . "<br/>";
}


This little neat piece of code will output the keys with the associated value. So the output will be something like this:

Output:

firstname - sabari
lastname - nathan
age - 28
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #36 (permalink)  
Old 08-18-2007, 04:13 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: How to Use Arrays efficiently in PHP

array_chunk

array_chunk — Split an array into chunks

array_chunk() example
<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>


The above example will output:
Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[0] => c
[1] => d
)

[2] => Array
(
[0] => e
)

)
Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[2] => c
[3] => d
)

[2] => Array
(
[4] => e
)

)
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #37 (permalink)  
Old 08-18-2007, 04:19 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 efficiently in PHP

array_merge_recursive

array_merge_recursive — Merge two or more arrays recursively

<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>

The above example will output:

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

[0] => blue
)

[0] => 5
[1] => 10
)

__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #38 (permalink)  
Old 08-18-2007, 04:24 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 efficiently in PHP

array_multisort

array_multisort — Sort multiple or multi-dimensional arrays

Example: Sorting multiple arrays

<?php
$ar1 = array("10", 100, 100, "a");
$ar2 = array(1, 3, "2", 1);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);
?>


The above example will output:

array(4) {
[0]=> string(2) "10"
[1]=> string(1) "a"
[2]=> int(100)
[3]=> int(100)
}
array(4) {
[0]=> int(1)
[1]=> int(1)
[2]=> string(1) "2"
[3]=> int(3)
}

__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #39 (permalink)  
Old 08-18-2007, 04:28 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 efficiently in PHP

array_walk

array_walk — Apply a user function to every member of an array

Example: array_walk() example

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}

function test_print($item2, $key)
{
echo "$key. $item2<br />\n";
}

echo "Before ...:\n";
array_walk($fruits, 'test_print');

array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";

array_walk($fruits, 'test_print');
?>


The above example will output:

Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple

__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #40 (permalink)  
Old 08-18-2007, 04:30 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 efficiently in PHP

array_walk_recursive

array_walk_recursive — Apply a user function recursively to every member of an array

Example: array_walk_recursive() example

<?php
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');

function test_print($item, $key)
{
echo "$key holds $item\n";
}

array_walk_recursive($fruits, 'test_print');
?>


The above example will output:

a holds apple
b holds banana
sour holds lemon
__________________
Thanks & Regards
Sabari...
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
Classic asp arrays and recordset ramesh123 ASP and ASP.NET Programming 1 12-02-2007 07:44 PM
Using arrays in stored procedures oxygen Database Support 1 11-26-2007 07:01 AM
Arrays in Java leoraja8 Java Programming 7 11-19-2007 12:23 AM
Jagged Arrays in C# vigneshgets C# Programming 3 08-23-2007 12:13 AM
Java:Tutorial - Arrays pranky Java Programming 0 02-23-2007 11:54 PM


All times are GMT -7. The time now is 01:56 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.