This is a discussion on Using Arrays in PHP within the PHP Programming forums, part of the Web Development category; can we discuss briefly about the efficient ways of Using Arrays in PHP ?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| A brief introduction to PHP Arrays: In PHP we can do magic with the Array Functions which will allow you to interact with and manipulate arrays in various ways. Arrays are essential for storing, managing and operating on sets of variables. Simple and multi-dimensional arrays are supported in PHP and it may be either user created or created by another function. There are specific database handling functions for populating arrays from database queries and several functions return arrays.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Hi Guys, Here i have two arrays $a = array(0,2,3,4) $b = array(2,4) i want to get the new array which only have elements like array(0,3) which does not have the elements in $b So in this case we have to use array_diff functions as like this $newarray = array_diff($a,$b) $newarray contains the elements as (0,3)
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| hi all, Applies the callback to the elements of the given array. we can apply user defined function in each element of an array by this function. Eg: we can change all values to lower string an array by array_map function here example for that.. $inputarray = array("Hello","Buy It"); function funStrChange($pmArray) { return strtolower($pmArray); } $aResult = array_map("funStrChange", $inputarray); now, $aResult is: $aResult[0] = "hello"; $aResult[1] = "buy it"; |
| |||
| hi all, is it possible to wild card search in an array? for eg: $vSearchStr = "hi"; $Array = array("hello","hi mathew"); here, i want to get the key value for $vSearchStr: how do i get the result for this? can any one give sample for this? |
| |||
| it's very simple, but i'ts very difficult to get solution for this issue, here listed the coding to get the key values from given Array values. $vSearchStr = "hi"; $Array = array("hello","hi mathew"); $Search=array(); foreach($Array as $key => $value) { if(ereg($vSearchStr,$value)) { $Search[$key]=$value; } } print_r($key); i think it's very useful for you ![]()
__________________ Thanks & Regards Sabari... Last edited by Sabari : 08-21-2007 at 09:49 PM. |
| |||
| Hi sabari, Its ok, but ive made one small change in your code. It is checking only case sensitive in array... so i have changed just one function : ereg to eregi. Now its working case insensitive. $vSearchStr = "hi"; $Array = array("hello","hi mathew"); $Search=array(); foreach($Array as $key => $value) { if(eregi($vSearchStr,$value)) { $Search[$key]=$value; } } print_r($key); |
| |||
| hi, array_rand function gets the random values from an array. $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand_keys = array_rand($input, 2); //#-- 1st parameter is Array for your random entries //#-- 2nd Parameter is number of picked element, ie, how many random you //# want to get from this array echo $input[$rand_keys[0]] . "\n"; echo $input[$rand_keys[1]] . "\n"; |
| |||
| 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.' -> '.$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. |
| |||
| 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. |
| |||
| 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 ) |
| |||
| 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 ) |
| |||
| 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. |
| |||
| 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. |
| |||
| 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. |
| |||
| 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. |
![]() |
| 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 |