Skip to main content

Simple Example of PHP Array Function


Simple Example of PHP Array Function
Predefined  functions:
1: sizeof():This function returns the number of elements in an array.
Eg:  

$data = array("red", "green", "blue");
echo "Array has " . sizeof($data) . " elements";

Output:
Array has 3 elements

2: array_values($arr): This function accepts a PHP array and returns a new array containing only its values (not its keys).
Eg :$data = array("hero" => "Holmes", "villain" => "Moriarty");
print_r(array_values($data));

Output:
Array
(
[0] => Holmes,[1] => Moriarty)

3: array_pop($arr): This function removes an element from the end of an array.
$data = array("Donald", "Jim", "Tom");
array_pop($data);
print_r($data);
?> 
Output:
Array(
[0] => Donald
[1] => Jim
)
4: array_push($arr, $val): This function adds an element to the end of an array.
$data = array("Donald", "Jim", "Tom");
array_push($data, "Harry");
print_r($data);
?> 
Output:
Array
(
[0] => Donald
[1] => Jim
[2] => Tom
[3] => Harry
)
5)array_shift($arr): This function removes an element from the beginning of an array.

6)array_unshift($arr, $val): This function adds an element to the beginning of an array.
$data = array("Donald", "Jim", "Tom");
array_unshift($data, "Sarah");
print_r($data);
?> 
Output:
Array
(
[0] => Sarah
[1] => Donald
[2] => Jim
[3] => Tom
)
7:each($arr):  This function is most often used to iteratively traverse an array. Each time each() is called, it returns the current key-value pair and moves the array cursor forward one element. This makes it most suitable for use in a loop.

$data = array("hero" => "Holmes", "villain" => "Moriarty");
while (list($key, $value) = each($data)) {
echo "$key: $value \n";
}
?>
Output:
hero: Holmes
villain: Moriarty
8:sort($arr)    This function sorts the elements of an array in ascending order. String values will be arranged in ascending alphabetical order.
Note: Other sorting functions include asort(), arsort(), ksort(), krsort() and rsort(). 
$data = array("g", "t", "a", "s");
sort($data);
print_r($data);
?>

Output:
Array
(
[0] => a
[1] => g
[2] => s
[3] => t
)
9:array_reverse($arr)           The function reverses the order of elements in an array.
Use this function to re-order a sorted list of values in reverse for easier processing—for example, when you're trying to begin with the minimum or maximum of a set of ordered values.
$data = array(10, 20, 25, 60);
print_r(array_reverse($data));
?>
Output:
Array
(
[0] => 60
[1] => 25
[2] => 20
[3] => 10
)
10:array_merge($arr)This function merges two or more arrays to create a single composite array. Key collisions are resolved in favor of the latest entry.
Use this function when you need to combine data from two or more arrays into a single structure—for example, records from two different SQL queries

$data1 = array("cat", "goat");
$data2 = array("dog", "cow");
print_r(array_merge($data1, $data2));

Output:
Array
(
[0] => cat
[1] => goat
[2] => dog
[3] => cow
)
11:array_rand($arr)  This function selects one or more random elements from an array.
Use this function when you need to randomly select from a collection of discrete values—for example, picking a random color from a list.

$data = array("white", "black", "red");
echo "Today's color is " . $data[array_rand($data)];

Output:
Today's color is red

12: array_search($search, $arr):This function searches the values in an array for a match to the search term, and returns the corresponding key if found. If more than one match exists, the key of the first matching value is returned.
Use this function to scan a set of index-value pairs for matches, and return the matching index.   Code:
$data = array("blue" => "#0000cc", "black" => "#000000", "green" => "#00ff00");
echo "Found " . array_search("#0000cc", $data);
?>

Output:
Found blue

13:array_unique($data)This function strips an array of duplicate values.
Use this function when you need to remove non-unique elements from an array—for example, when creating an array to hold values for a table's primary key.
$data = array(1,1,4,6,7,4);
print_r(array_unique($data));

Output:
Array
(
[0] => 1
[3] => 6
[4] => 7
[5] => 4
)

Comments

Popular posts from this blog

A Guide to UTF-8 for PHP and MySQL

Data Encoding: A Guide to UTF-8 for PHP and MySQL As a MySQL or PHP developer, once you step beyond the comfortable confines of English-only character sets, you quickly find yourself entangled in the wonderfully wacky world of UTF-8. On a previous job, we began running into data encoding issues when displaying bios of artists from all over the world. It soon became apparent that there were problems with the stored data, as sometimes the data was correctly encoded and sometimes it was not. This led programmers to implement a hodge-podge of patches, sometimes with JavaScript, sometimes with HTML charset meta tags, sometimes with PHP, and soon. Soon, we ended up with a list of 600,000 artist bios with double- or triple encoded information, with data being stored in different ways depending on who programmed the feature or implemented the patch. A classical technical rat’s nest.Indeed, navigating through UTF-8 related data encoding issues can be a frustrating and hair-pul...

How To Create Shortcodes In WordPress

We can create own shortcode by using its predified hooks add_shortcode( 'hello-world', 'techsudhir_hello_world_shortcode' ); 1. Write the Shortcode Function Write a function with a unique name, which will execute the code you’d like the shortcode to trigger: function techsudhir_hello_world_shortcode() {    return 'Hello world!'; } Example: [hello-world] If we were to use this function normally, it would return Hello world! as a string 2. Shortcode function with parameters function techsudhir_hello_world_shortcode( $atts ) {    $a = shortcode_atts( array(       'name' => 'world'    ), $atts );    return 'Hello ' . $a['name'] . !'; } Example: [hello-world name="Sudhir"] You can also call shortcode function in PHP using do_shortcode function Example: do_shortcode('[hello-world]');

Integrating Kafka with Node.js

Integrating Kafka with Node.js Apache Kafka is a popular open-source distributed event streaming platform that uses publish & subscribe mechanism to stream the records(data). Kafka Terminologies Distributed system: Distributed system is a computing environment where various software components located on different machines (over multiple locations). All components coordinate together to get stuff done as one unit.   Kafka Broker: Brokers are cluster of multiple servers. Message of each topic are split among the various brokers. Brokers handle all requests from clients to write and read events. A Kafka cluster is simply a collection of one or more Kafka brokers. Topics: A topic is a stream of "related" messages. Its unique throughout application. Kafka producers write messages to topics. Producer: Producer publishes data on the topics. A producer sends a message to a broker and the broker receives and stores messages. Consumers: Consumers read data from topics. A consu...