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; Regular Expressions Exploding and imploding refers to changing between a string and an array. You can take a specific string ...


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

Register FAQ Members List Calendar Mark Forums Read
  #61  
Old 08-21-2007, 03:33 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

Regular Expressions

Exploding and imploding refers to changing between a string and an array. You can take a specific string and create an array out of the words or vice versa.

Using the explode command will create an array from a string.

Here is a sample string to start things off :
<?php
$pantry = "tomatoes,oranges,bananas,potatoes,bread,apple s";
?>


Now you have to figure out a common seperator element. In this case it is a comma seperating each word. In a normal sentence, it could be specifed as a space.

Time to explode this string into an array...
<?php
$pantry = "tomatoes,oranges,bananas,potatoes,bread,apple s";
$pantry_food = explode(",",$pantry);
?>


The array called $pantry_food now contains 6 elements. The array keys range from 0 to 5 and contains the values of tomatoes to apples.
Doing a quick array print out :
<?php
$count_total = count($pantry_food);
for ($counter=0; $counter<$count_total; $counter++){
$line = each ($pantry_food);
echo "$line[key] $line[value] <br />";
}
?>


Results as :

0 tomatoes
1 oranges
2 bananas
3 potatoes
4 bread
5 apples
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #62  
Old 08-21-2007, 03:34 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

Implode()

So exploding breaks the data apart, imploding brings the data together. The implode command will take the data from an array and assemble it into a single string.

We will use the newly created array from above and implode it into a new string.
<?php
$new_pantry = implode(" ",$pantry_food);
?>


Imploding uses a separator element as well. In this case, it adds the separator between each of the array elements when it creates the new string. The above example is using a space this time.

The new string :
<?php
echo "$new_pantry";
?>


Has the value of :

tomatoes oranges bananas potatoes bread apples
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #63  
Old 08-21-2007, 03:51 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Question Multi dimentional array

hi all,

can any one give solution for this array issue?...

i want to convert multi-dimensional array to single-dimensional array..

$firstArray =>
Array(

[0] = test
[1] = new
[2] =
Array(
[0] = hey
[1] =
Array(
[0] = innerarray
[1] = innertest
)
[3] = final

)

)

I want to change Above array like this....

Array(

[0] = test
[1] = new
[2] = hey
[3] = innerarray
[4] = innertest
[5] = final
}

can any one give the solution for multi-dimensional to single-dimensional array?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #64  
Old 08-21-2007, 06:08 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Post Info: Internal Array Pointer Functions

hi all,

here i have given the internal array pointer functions & sample code :

functions:
next

- Returns the array value in the next place that's pointed to by the internal
array pointer, or FALSE if there are no more elements.

current or pos

- Every array has an internal pointer to its "current" element, which is
initialized to the first element inserted into the array.

prev
- Returns the array value in the previous place that's pointed to by the
internal array pointer, or FALSE if there are no more elements.


eg for :current,next,prev,end function

<?php
$transport = array('current', 'next', 'car', 'plane');
$mode = current($transport); // $mode = 'current';
$mode = next($transport); // $mode = 'next';
$mode = next($transport); // $mode = 'car';
$mode = next($transport); // $mode = 'plane';
$mode = prev($transport); // $mode = 'car';
$mode = end($transport); // $mode = 'plane';


reset

- reset() rewinds array's internal pointer to the first element and returns
the value of the first array element, or FALSE if the array is empty.

eg for :reset function
<?php

$array = array('step one', 'step two', 'step three', 'step four');

// by default, the pointer is on the first element
echo current($array); // "step one"

// skip two steps
next($array);
next($array);
echo current($array); // "step three"

// reset pointer, start again on step one
reset($array);
echo current($array); // "step one"

?>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #65  
Old 08-21-2007, 08:46 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

Array Count()

Counting the number of elements in an array uses a command called count.

First we will start with our original pantry array :
<?php
$pantry = array(
1 => "apples",
2 => "oranges",
3 => "bananas"
);
?>


Now to count how many elements are in this array...
<?php
$total_elements = count($pantry);
echo "There are $total_elements elements in the array.";
?>


This will produce the result :
There are 3 elements in the array.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #66  
Old 08-21-2007, 08:53 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

natcasesort()

natcasesort — Sort an array using a case insensitive "natural order" algorithm

Example: natcasesort()

<?php
$array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');

sort($array1);
echo "Standard sorting\n";
print_r($array1);

natcasesort($array2);
echo "\nNatural order sorting (case-insensitive)\n";
print_r($array2);
?>

The above example will output:

Standard sorting
Array
(
[0] => IMG0.png
[1] => IMG3.png
[2] => img1.png
[3] => img10.png
[4] => img12.png
[5] => img2.png
)

Natural order sorting (case-insensitive)
Array
(
[0] => IMG0.png
[4] => img1.png
[3] => img2.png
[5] => IMG3.png
[2] => img10.png
[1] => img12.png
)

__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #67  
Old 08-21-2007, 08:55 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

natsort()

natsort — Sort an array using a "natural order" algorithm

Example: natsort()
<?php
$array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");

sort($array1);
echo "Standard sorting\n";
print_r($array1);

natsort($array2);
echo "\nNatural order sorting\n";
print_r($array2);
?>


The above example will output:

Standard sorting
Array
(
[0] => img1.png
[1] => img10.png
[2] => img12.png
[3] => img2.png
)

Natural order sorting
Array
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)

__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #68  
Old 08-21-2007, 08:57 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

extract()

extract — Import variables into the current symbol table from an array

extract() returns the number of variables successfully imported into the symbol table.

Example: extract()

<?php

/* Suppose that $var_array is an array returned from
wddx_deserialize */

$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");

echo "$color, $size, $shape, $wddx_size\n";

?>


The above example will output:

blue, large, sphere, medium

__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #69  
Old 08-21-2007, 09:03 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

Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

Example:

In this example we create a multidimensional array, with automatically assigned ID keys:

$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);

The array above would look like this if written to the output:

Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #70  
Old 08-21-2007, 09:05 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

print_r()

Print_r() outputs readable information about a variable. Though not an array-only function, this is actually one of the most useful functions out there when you are working with and debugging arrays. In the case of arrays, it allows you to see every key and the associated value.

<?php

$Array=array("http://www.yahoo.com", "http://www.internet.com", "http://www.google.com", "http://www.cnn.com", "http://www.php.net");

print_r($Array);
?>


This PHP snippet would output:

Array (
[0] => Yahoo!
[1] => internet.com - the Internet and IT Network from Jupitermedia Corp.
[2] => Google
[3] => CNN.com - Breaking News, U.S., World, Weather, Entertainment & Video News
[4] => PHP: Hypertext Preprocessor )

If you are trying to visualize what is inside of an array, or trying to track down an error within one, this function is an ideal tool.


__________________
Thanks & Regards
Sabari...
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:50 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