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  
Old 08-13-2007, 11:02 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
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
  #2  
Old 08-13-2007, 11:26 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
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  
Old 08-13-2007, 11:48 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,165
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  
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  
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  
Old 08-14-2007, 02:51 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 945
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  
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  
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  
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  
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
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 12:02 AM.


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