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; hi all, Can any one give details about ArrayIterator? Explain the usage of this function....


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

Register FAQ Members List Calendar Mark Forums Read

Reply
 
Thread Tools Display Modes
  #11  
Old 08-16-2007, 01:36 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Exclamation Can any one give details about ArrayIterator?

hi all,

Can any one give details about ArrayIterator? Explain the usage of this function.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12  
Old 08-16-2007, 02:42 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

ArrayIterator is the Component of SPL, here explained detailed about SPL and Iterators...

what is Standard PHP Library (SPL)?
SPL provides a standard set of interfaces for PHP5. The aim of SPL is to implement some efficient data access interfaces and classes for PHP. Functionally it is designed to traverse aggregate structures (anything you want to loop over). These may include arrays, database result sets, xml trees, directory listings or any list at all. Currently SPL deals with Iterators. To see all the classes available to SPL, this simple snippet will show you.

<?php
// a simple foreach() to traverse the SPL class names

foreach(spl_classes() as $key=>$value)
{
echo $key.' -&gt; '.$value.'<br />';
}
?>


What are iterators?

An Iterator is an object that traverses a structure eg: an array or a directory listing or possibly a set of database result sets or other resource. This is not an accurate discription, but more will become clear later by way of example. There are different types of iterators for dealing with different types of data such as array Iterators, Directory Iterators and more. Here we will begin to get familiar with them beginning with the DirectoryIterator. What is important to note is they can all be accessed with a standard interface. This means that regardless of the data type, access to the information is standardised. This is a real step forward for PHP.

ArrayIterator

The ArrayIterator makes use of the ArrayObject to traverse arrays and an understanding of this is important. Much like the Directory iterator, we can see the methods available to the Array Iterator with a simple snippet.

<?php
foreach(get_class_methods(new ArrayIterator()) as $key=>$method)
{
echo $key.' -> '.$method.'<br />';
}
?>
__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:50 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13  
Old 08-16-2007, 03:02 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

here we can discuss briefly one by one about ArrayIterator...

ArrayIterator::current

ArrayIterator::current -- Return current array entry
Description
mixed ArrayIterator::current ( void )

This function returns the current array entry

Example: ArrayIterator::current()
<?php
$array = array('1' => 'one',
'2' => 'two',
'3' => 'three');

$arrayobject = new ArrayObject($array);

for($iterator = $arrayobject->getIterator();
$iterator->valid();
$iterator->next()) {

echo $iterator->key() . ' => ' . $iterator->current() . "\n";
}
?>


The above example will output:

1 => one
2 => two
3 => three
__________________
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
  #14  
Old 08-16-2007, 10:11 PM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Post Info: Array Compact Function

hi all,

- Create array containing variables and their values.

- This function create a array & this array key from variables name & values from their values.

eg:

$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";

$location_vars = array("city", "state");

$result = compact("event", $location_vars);


After this, $result will be:

Array
(
[event] => SIGGRAPH
[city] => San Francisco
[state] => CA
)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15  
Old 08-16-2007, 10:37 PM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Question What is difference between array_diff & array_diff_assoc?

hi all,

What is difference between array_diff & array_diff_assoc function?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16  
Old 08-16-2007, 10:46 PM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Post Info : Array Flip function

hi all,

array_flip function:

-- Exchanges all keys with their associated values in an array ( Convert all keys to value & their values to key ).



$array = array("a" => 1, "b" => 1, "c" => 2);
$array = array_flip($array);
print_r($array);


Result:
Array
(
[1] => b
[2] => c
)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17  
Old 08-16-2007, 10:59 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

we can discuss briefly one by one about ArrayIterator...
ArrayIterator::key

ArrayIterator::key -- Return current array key
Description
mixed ArrayIterator::key ( void )

This function returns the current array key

ArrayIterator::key() example
<?php
$array = array('key' => 'value');

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

echo $iterator->key(); //key
?>
__________________
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
  #18  
Old 08-16-2007, 11:00 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

ArrayIterator::next


ArrayIterator::next -- Move to next entry
Description
void ArrayIterator::next ( void )

This function moves the iterator to the next entry.

ArrayIterator::next() example
<?php
$arrayobject = new ArrayObject();

$arrayobject[] = 'zero';
$arrayobject[] = 'one';

$iterator = $arrayobject->getIterator();

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

$iterator->next();
}
?>

The above example will output:

0 => zero 1 => one
__________________
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
  #19  
Old 08-16-2007, 11:01 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

ArrayIterator::rewind

ArrayIterator::rewind -- Rewind array back to the start
Description
void ArrayIterator::rewind ( void )

This function rewinds the iterator to the beginning.

ArrayIterator::rewind() example
<?php
$arrayobject = new ArrayObject();

$arrayobject[] = 'zero';
$arrayobject[] = 'one';
$arrayobject[] = 'two';

$iterator = $arrayobject->getIterator();

$iterator->next();
echo $iterator->key(); //1

$iterator->rewind(); //rewinding to the begining
echo $iterator->key(); //0
?>
__________________
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
  #20  
Old 08-16-2007, 11:04 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

ArrayIterator::seek

ArrayIterator::seek -- Seek to position
Description
void ArrayIterator::seek ( int position )
__________________
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
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 Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Jagged Arrays in C# vigneshgets C# Programming 4 09-30-2008 03:48 AM
Using arrays in stored procedures it.wily Database Support 2 02-09-2008 09:18 AM
Classic asp arrays and recordset ramesh123 ASP and ASP.NET Programming 1 12-02-2007 07:44 PM
Arrays in Java leoraja8 Java Programming 7 11-19-2007 12:23 AM
Java:Tutorial - Arrays pranky Java Programming 0 02-23-2007 11:54 PM


All times are GMT -7. The time now is 09:54 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0