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; can we discuss briefly about the efficient ways of Using Arrays in PHP ?...


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

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 08-13-2007, 11:02 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Exclamation Using Arrays in PHP

can we discuss briefly about the efficient ways of Using Arrays in PHP ?
__________________
Thanks & Regards
Sabari...

Last edited by Sabari : 08-21-2007 at 09:49 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-13-2007, 11:26 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: How to Use Array's efficiently in PHP

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-13-2007, 11:48 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: How to Use Array's efficiently in PHP

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-14-2007, 12:24 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Post array_map : Very useful function

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";
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-14-2007, 12:47 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Exclamation is it Possible wild card search in array values?

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?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-14-2007, 02:51 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Thumbs up Using Arrays in PHP

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-14-2007, 03:15 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Smile Re: How to Use Array's efficiently in PHP

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);
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-14-2007, 05:24 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post maximum size of variable? and maximum size of array?

What is maximum size of a normal variable & array variable? OR is there any default size available? Pls help me. Thanks
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-14-2007, 06:08 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post array random

Can anyone of you explain array_random ?

Last edited by senraj : 08-14-2007 at 07:32 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 08-16-2007, 12:29 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Post Info Array Random Entry

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";
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
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 (permalink)  
Old 08-16-2007, 02:42 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

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 (permalink)  
Old 08-16-2007, 03: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

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 (permalink)  
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 (permalink)  
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 (permalink)  
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 (permalink)  
Old 08-16-2007, 10:59 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

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 (permalink)  
Old 08-16-2007, 11:00 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::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 (permalink)  
Old 08-16-2007, 11:01 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::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 (permalink)  
Old 08-16-2007, 11:04 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::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 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 10:01 AM.


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

SEO by vBSEO 3.0.0