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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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. |
| Sponsored Links |
| |||
| 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. |
| |||
| 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. |
| |||
| 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. |
| |||
| 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. |
| |||
| 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. |
| |||
| 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. |
| |||
| 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. |
| |||
| 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. |
| |||
| 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. |
| |||
| CachingIterator::__toString CachingIterator::__toString -- Return the string representation of the current element Description string CachingIterator::__toString ( void )
__________________ Thanks & Regards Sabari... |
| |||
| 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_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: Code: Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| 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... |
| |||
| 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... |
| |||
| 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... |
| |||
| 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... |
| |||
| 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... |
| |||
| 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... |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |