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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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... |
| Sponsored Links |
| |||
| 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... |
| |||
| 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? |
| |||
| 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" ?> |
| |||
| 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... |
| |||
| 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... |
| |||
| 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... |
| |||
| 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... |
| |||
| 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... |
| |||
| 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... |
| |||
| Very good contribution has been made to this thread and so ive made this thread sticky. Good Luck and continue quality content posting in this thread regularly because our goal must be to get the 1st result in search engines when searched for using arrays in php or php arrays or arrays in php etc., Thanks
__________________ Vinoth Chandar Creator of Discussweb |
| |||
| hi all, - Create an array containing a range of elements using array range function. eg: 1 $number = range(0, 5); echo "<pre>"; print_r($number); Result: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 ) eg: 2 - last parameter is : optional step parameter was added in 5.0.0. // The step parameter was introduced in 5.0.0 // array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100) foreach (range(0, 100, 10) as $number) { echo $number; } |
| |||
| hi all, - list array function get the array values and assign to the variable.. eg: $arr = array('list', 'function', 'sample code'); // Listing all the variables list($list, $function, $code) = $arr; echo "<b>$list</b> $function $code"; result: list function sample code |
| |||
| Hi All, Can you guys help me to solve this problem. Want to spilt this one array in two seperate array with same value without using any loop. EG. array1[0]= 'One'; array1[1]= 'Two'; array1[2]= 'One'; array1[3]= 'Two'; array1[4]= 'Two'; array1[5]= 'One'; Output Should be like this. array2[0]= 'One'; array2[1]= 'One'; array2[2]= 'One'; array3[0]= 'Two'; array3[1]= 'Two'; array3[2]= 'Two'; |
| |||
| hi, see this example ... i hope its ok to you... <? $array1[0]= 'One'; $array1[1]= 'Two'; $array1[2]= 'One'; $array1[3]= 'Two'; $array1[4]= 'Two'; $array1[5]= 'One'; echo "<pre>"; print_r($array1); $aRes = array_count_values($array1); $aArrayKeys = array_keys($aRes); $aArrayValues = array_values($aRes); $aFinalResult = array_map("funTest", $aArrayKeys, $aArrayValues ); print_r($aFinalResult); function funTest($pmArray, $pmArray1) { $aFinalResult = array_fill(0, $pmArray1, $pmArray); return $aFinalResult; } ?> //#-- Result Array ( [0] => One [1] => Two [2] => One [3] => Two [4] => Two [5] => One ) Array ( [0] => Array ( [0] => One [1] => One [2] => One ) [1] => Array ( [0] => Two [1] => Two [2] => Two ) ) |
| |||
| array_walk: hi all, array_walk is apply user defined function in each element of an array by this function. Eg: function add_string($value, $key) { $value = $key.'. '.$value; echo $value."\n"; } $stuff = array( 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four" ); $v = array_walk($stuff, 'add_string'); echo ($v); output: 1. One 2. Two 3. Three 4. Four |
| |||
| Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative . The keys of an indexed array are integers, beginning at 0. Indexed arrays are used when you identify things by their position. Associative arrays have strings as keys and behave more like two-column tables. The first column is the key, which is used to access the value. PHP internally stores all arrays as associative arrays , so the only difference between associative and indexed arrays is what the keys happen to be. Some array features are provided mainly for use with indexed arrays because they assume that you have or want keys that are consecutive integers beginning at 0. In both cases, the keys are unique. In other words, you can't have two elements with the same key, regardless of whether the key is a string or an integer. PHP arrays have an internal order to their elements that is independent of the keys and values, and there are functions that you can use to traverse the arrays based on this internal order. The order is normally that in which values were inserted into the array, but the sorting functions let you change the order to one based on keys, values, or anything else you choose.
__________________ Thanks & Regards Sabari... |
| |||
| Identifying Elements of an Array You can access specific values from an array using the array variable's name, followed by the element's key (sometimes called the index) within square brackets: $age['Fred'] $shows[2] The key can be either a string or an integer. String values that are equivalent to integer numbers (without leading zeros) are treated as integers. Thus, $array[3] and $array['3'] reference the same element, but $array['03'] references a different element. Negative numbers are valid keys, and they don't specify positions from the end of the array as they do in Perl. You don't have to quote single-word strings. For instance, $age['Fred'] is the same as $age[Fred]. However, it's considered good PHP style to always use quotes, because quoteless keys are indistinguishable from constants. When you use a constant as an unquoted index, PHP uses the value of the constant as the index: define('index',5); echo $array[index]; // retrieves $array[5], not $array['index']; You must use quotes if you're using interpolation to build the array index: $age["Clone$number"] However, don't quote the key if you're interpolating an array lookup: // these are wrong print "Hello, $person['name']"; print "Hello, $person["name"]"; // this is right print "Hello, $person[name]";
__________________ Thanks & Regards Sabari... |
| |||
| Storing Data in Arrays Storing a value in an array will create the array if it didn't already exist, but trying to retrieve a value from an array that hasn't been defined yet won't create the array. For example: // $addresses not defined before this point echo $addresses[0]; // prints nothing echo $addresses; // prints nothing $addresses[0] = 'spam@cyberpromo.net'; echo $addresses; // prints "Array" Using simple assignment to initialize an array in your program leads to code like this: $addresses[0] = 'spam@cyberpromo.net'; $addresses[1] = 'abuse@example.com'; $addresses[2] = 'root@example.com'; // ... That's an indexed array, with integer indexes beginning at 0. Here's an associative array: $price['Gasket'] = 15.29; $price['Wheel'] = 75.25; $price['Tire'] = 50.00; // ... An easier way to initialize an array is to use the array( ) construct, which builds an array from its arguments. This builds an indexed array, and the index values (starting at 0) are created automatically: $addresses = array('spam@cyberpromo.net', 'abuse@example.com', 'root@example.com'); To create an associative array with array( ), use the => symbol to separate indexes from values: $price = array('Gasket' => 15.29, 'Wheel' => 75.25, 'Tire' => 50.00); Notice the use of whitespace and alignment. We could have bunched up the code, but it wouldn't have been as easy to read: $price = array('Gasket'=>15.29,'Wheel'=>75.25,'Tire'=>50.00 ); To construct an empty array, pass no arguments to array( ): $addresses = array( ); You can specify an initial key with => and then a list of values. The values are inserted into the array starting with that key, with subsequent values having sequential keys: $days = array(1 => 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'); // 2 is Tuesday, 3 is Wednesday, etc.
__________________ Thanks & Regards Sabari... |
| |||
| hi guys, i give some more info in array range function. range() returns an array of elements from low to high, inclusive. If low > high, the sequence will be from high to low. Parameters: array range ( mixed low, mixed high [, number step] ) Step should be given as a positive number, If not specified, step will default to 1. it will be used as the increment between elements in the sequence. eg: 1. $number = range(0, 12); print_r($number); 2. $number1 = range(0, 100, 10); print_r($number1); 3. $chars = range('a', 'i'); print_r($chars); 4. $chars1 = range('c', 'a'); print_r($chars1); --kamalakannan. |
![]() |
| 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 01:04 PM |
| DiscussWeb IT Community - Technical Support and Technology Discussions | This thread | Refback | 08-23-2007 10:11 AM |