0% found this document useful (0 votes)
10 views

Internet Technology

internet technology report writing

Uploaded by

BIKRAM
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Internet Technology

internet technology report writing

Uploaded by

BIKRAM
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

PHP Array And Some Array Functions

DEPARTMENT OF COMPUTER APPLICATION


CONTAI COLLEGE OF LEARNING & MANAGEMENT
SCIENCE

NAME: Bikram Adak


UNIVERSITY ROLL NO.: 34001222017
STREAM: BCA
SEMESTER: 5th
PAPER: Internet Technology
PAPER CODE: BCAC501
SESSION:2022-2025
ABSTRACT
PHP is a versatile server-side scripting language widely used for web
development. Two fundamental components of PHP are loops and
string functions, which are crucial for controlling the flow of code and
processing text data. Often when you write code, you want the same
block of code to run over and over again a certain number of times. So,
instead of adding several almost equal code-lines in a script, we can
use loops. Loops are used to execute the same block of code again
and again, as long as a certain condition is true. In this report I will
describe Loops and String.

INTRODUCTION
The term PHP is an acronym for – Hypertext Preprocessor. PHP is a
server-side scripting language designed specifically for web
development. It is open-source which means it is free to download and
use. It is very simple to learn and use. The file extension of PHP is
“.php”.

PHP was introduced by Rasmus Lerdorf in the first version and


participated in the later versions. It is an interpreted language and it
does not require a compiler.

PHP is a versatile server-side scripting language widely used for web


development. Two fundamental components of PHP are loops and
string functions, which are crucial for controlling the flow of code and
processing text data. Often when you write code, you want the same
block of code to run over and over again a certain number of times. So,
instead of adding several almost equal code-lines in a script, we can
use loops. Loops are used to execute the same block of code again
and again, as long as a certain condition is true. In this report I will
describe Loops and String.
LITERATURE REVIEW
PHP Array:
An array is a special variable that can hold many values under a single
name, and you can access the values by referring to an index number or
name.

PHP Array Types


In PHP, there are three types of arrays:
 Indexed arrays - Arrays with a numeric index
 Associative arrays - Arrays with named keys
 Multidimensional arrays - Arrays containing one or more arrays

Indexed or Numeric Arrays


These type of arrays can be used to store any type of element, but an
index is always a number. By default, the index starts at zero. These
arrays can be created in two different ways.
Examples of Indexed or Numeric Arrays
Example 1:
<?php
$name_one = array("Zack", "Anthony", "Ram", "Salim", "Raghav");
echo "Accessing the 1st array elements directly:\n";
echo $name_one[2], "\n";
echo $name_one[0], "\n";
echo $name_one[4], "\n";

$name_two[0] = "ZACK";
$name_two[1] = "ANTHONY";
$name_two[2] = "RAM";
$name_two[3] = "SALIM";
$name_two[4] = "RAGHAV";
echo "Accessing the 2nd array elements directly:\n";
echo $name_two[2], "\n";
echo $name_two[0], "\n";
echo $name_two[4], "\n";
?>
Output:
Accessing the 1st array elements directly:
Ram
Zack
Raghav
Accessing the 2nd array elements directly:
RAM
ZACK
RAGHAV

Example 2: We can traverse an indexed array using loops in PHP.


<?php
$name_one = array("Zack", "Anthony", "Ram", "Salim", "Raghav");
echo "Looping using foreach: \n";
foreach ($name_one as $val){
echo $val. "\n";
}
$round = count($name_one);
echo "\nThe number of elements are $round \n";
echo "Looping using for: \n";
for($n = 0; $n < $round; $n++){
echo $name_one[$n], "\n";
}
?>
Output:
Looping using foreach:
Zack
Anthony
Ram
Salim
Raghav
The number of elements is 5
Looping using for:
ZACK
ANTHONY
RAM
SALIM
RAGHAV

Associative Arrays
These types of arrays are similar to the indexed arrays but instead of
linear storage, every value can be assigned with a user-defined key of
string type.
Examples of Associative Arrays
Example 1:
<?php
$name_one = array("Zack"=>"Zara", "Anthony"=>"Any",
"Ram"=>"Rani", "Salim"=>"Sara",
"Raghav"=>"Ravina");
$name_two["zack"] = "zara";
$name_two["anthony"] = "any";
$name_two["ram"] = "rani";
$name_two["salim"] = "sara";
$name_two["raghav"] = "ravina";
echo "Accessing the elements directly:\n";
echo $name_two["zack"], "\n";
echo $name_two["salim"], "\n";
echo $name_two["anthony"], "\n";
echo $name_one["Ram"], "\n";
echo $name_one["Raghav"], "\n";
?>
Output:
Accessing the elements directly:
zara
sara
any
Rani
Ravina

Example 2: We can traverse associative arrays in a similar way did in


numeric arrays using loops.
<?php
$name_one = [
"Zack" => "Zara",
"Anthony" => "Any",
"Ram" => "Rani",
"Salim" => "Sara",
"Raghav" => "Ravina",
];
echo "Looping using foreach: \n";
foreach ($name_one as $val => $val_value) {
echo "Husband is " . $val . " and Wife is " . $val_value . "\n";
}
echo "\nLooping using for: \n";
$keys = array_keys($name_one);
$round = count($name_one);

for ($i = 0; $i < $round; ++$i) {


echo $keys[$i] . " " . $name_one[$keys[$i]] . "\n";
}
?>

Output:
Looping using foreach:
Husband is Zack and Wife is Zara
Husband is Anthony and Wife is Any
Husband is Ram and Wife is Rani
Husband is Salim and Wife is Sara
Husband is Raghav and Wife is Ravina
Looping using for:
zack zara
anthony any
ram rani
salim sara
raghav ravina
Multidimensional Arrays
Multi-dimensional arrays are such arrays that store another array at each
index instead of a single element. In other words, we can define multi-
dimensional arrays as an array of arrays. As the name suggests, every
element in this array can be an array and they can also hold other sub-
arrays within. Arrays or sub-arrays in multidimensional arrays can be
accessed using multiple dimensions.
Examples of Multidimensional Arrays
Example 1:
<?php
$favorites = array(
array(
"name" => "Dave Punk",
"mob" => "5689741523",
"email" => "[email protected]",
),
array(
"name" => "Monty Smith",
"mob" => "2584369721",
"email" => "[email protected]",
),
array(
"name" => "John Flinch",
"mob" => "9875147536",
"email" => "[email protected]",
)
);
echo "Dave Punk email-id is: " . $favorites[0]["email"], "\n";
echo "John Flinch mobile number is: " . $favorites[2]["mob"];
?>
Output:
Dave Punk email-id is: [email protected]
John Flinch mobile number is: 9875147536

Example 2:
<?php
$favorites = array(
"Dave Punk" => array(
"mob" => "5689741523",
"email" => "[email protected]",
),
"Dave Punk" => array(
"mob" => "2584369721",
"email" => "[email protected]",
),
"John Flinch" => array(
"mob" => "9875147536",
"email" => "[email protected]",
)
);
$keys = array_keys($favorites);
for($i = 0; $i < count($favorites); $i++) {
echo $keys[$i] . "\n";
foreach($favorites[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "\n";
}
echo "\n";
}
?>
Output:
Dave Punk
mob : 2584369721
email : [email protected]
John Flinch
mob : 9875147536
email : [email protected]

PHP Array Functions:


PHP Arrays are a data structure that stores multiple elements of a similar
type in a single variable. The arrays are helpful to create a list of
elements of similar type. It can be accessed using their index number or
key. The array functions are allowed to interact and manipulate the array
elements in various ways. The PHP array functions are used for single
and multi-dimensional arrays.

Array Functions are given below:


Function Description
array() Creates an array
array_change_key_case() Changes all keys in an array to
lowercase or uppercase
array_chunk() Splits an array into chunks of arrays
array_column() Returns the values from a single
column in the input array
array_combine() Creates an array by using the
elements from one "keys" array and
one "values" array
array_count_values() Counts all the values of an array
array_diff() Compare arrays, and returns the
differences (compare values only)
array_diff_assoc() Compare arrays, and returns the
differences (compare keys and
values)
array_diff_key() Compare arrays, and returns the
differences (compare keys only)
array_diff_uassoc() Compare arrays, and returns the
differences (compare keys and
values, using a user-defined key
comparison function)
array_diff_ukey() Compare arrays, and returns the
differences (compare keys only,
using a user-defined key comparison
function)
array_fill() Fills an array with values
array_fill_keys() Fills an array with values, specifying
keys
array_filter() Filters the values of an array using a
callback function
array_flip() Flips/Exchanges all keys with their
associated values in an array
array_intersect() Compare arrays, and returns the
matches (compare values only)
array_intersect_assoc() Compare arrays and returns the
matches (compare keys and values)
array_intersect_key() Compare arrays, and returns the
matches (compare keys only)
array_intersect_uassoc() Compare arrays, and returns the
matches (compare keys and values,
using a user-defined key comparison
function)
array_intersect_ukey() Compare arrays, and returns the
matches (compare keys only, using a
user-defined key comparison
function)
array_key_exists() Checks if the specified key exists in
the array
array_keys() Returns all the keys of an array
array_map() Sends each value of an array to a
user-made function, which returns
new values
array_merge() Merges one or more arrays into one
array
array_merge_recursive() Merges one or more arrays into one
array recursively
array_multisort() Sorts multiple or multi-dimensional
arrays
array_pad() Inserts a specified number of items,
with a specified value, to an array
array_pop() Deletes the last element of an array
array_product() Calculates the product of the values
in an array
array_push() Inserts one or more elements to the
end of an array
array_rand() Returns one or more random keys
from an array
array_reduce() Returns an array as a string, using a
user-defined function
array_replace() Replaces the values of the first array
with the values from following
arrays
array_replace_recursive() Replaces the values of the first array
with the values from following
arrays recursively
array_reverse() Returns an array in the reverse
order
array_search() Searches an array for a given value
and returns the key
array_shift() Removes the first element from an
array, and returns the value of the
removed element
array_slice() Returns selected parts of an array
array_splice() Removes and replaces specified
elements of an array
array_sum() Returns the sum of the values in an
array
array_udiff() Compare arrays, and returns the
differences (compare values only,
using a user-defined key comparison
function)
array_udiff_assoc() Compare arrays, and returns the
differences (compare keys and
values, using a built-in function to
compare the keys and a user-
defined function to compare the
values)
array_udiff_uassoc() Compare arrays, and returns the
differences (compare keys and
values, using two user-defined key
comparison functions)
array_uintersect() Compare arrays, and returns the
matches (compare values only, using
a user-defined key comparison
function)
array_uintersect_assoc() Compare arrays, and returns the
matches (compare keys and values,
using a built-in function to compare
the keys and a user-defined function
to compare the values)
array_uintersect_uassoc() Compare arrays, and returns the
matches (compare keys and values,
using two user-defined key
comparison functions)
array_unique() Removes duplicate values from an
array
array_unshift() Adds one or more elements to the
beginning of an array
array_values() Returns all the values of an array
array_walk() Applies a user function to every
member of an array
array_walk_recursive() Applies a user function recursively
to every member of an array
arsort() Sorts an associative array in
descending order, according to the
value
asort() Sorts an associative array in
ascending order, according to the
value
compact() Create array containing variables
and their values
count() Returns the number of elements in
an array
current() Returns the current element in an
array
each() Deprecated from PHP 7.2. Returns
the current key and value pair from
an array
end() Sets the internal pointer of an array
to its last element
extract() Imports variables into the current
symbol table from an array
in_array() Checks if a specified value exists in
an array
key() Fetches a key from an array
krsort() Sorts an associative array in
descending order, according to the
key
ksort() Sorts an associative array in
ascending order, according to the
key
list() Assigns variables as if they were an
array
natcasesort() Sorts an array using a case
insensitive "natural order" algorithm
natsort() Sorts an array using a "natural
order" algorithm
next() Advance the internal array pointer
of an array
pos() Alias of current()
prev() Rewinds the internal array pointer
range() Creates an array containing a range
of elements
reset() Sets the internal pointer of an array
to its first element
rsort() Sorts an indexed array in descending
order
shuffle() Shuffles an array
sizeof() Alias of count()
sort() Sorts an indexed array in ascending
order
uasort() Sorts an array by values using a
user-defined comparison function
and maintains the index association
uksort() Sorts an array by keys using a user-
defined comparison function
usort() Sorts an array by values using a
user-defined comparison function
Some String Function example are given bellow:
1. array_push()
Adds one or more elements to the end of an array.

$arr = ["apple", "banana"];


array_push($arr, "orange", "mango");
// $arr is now ["apple", "banana", "orange", "mango"]

2. array_pop()
Removes the last element from an array.

$arr = ["apple", "banana", "orange"];


array_pop($arr);
// $arr is now ["apple", "banana"]

3. array_merge()
Merges one or more arrays into one.

$arr1 = ["apple", "banana"];


$arr2 = ["orange", "mango"];
$result = array_merge($arr1, $arr2);
// $result is ["apple", "banana", "orange", "mango"]

4. array_diff()
Computes the difference between arrays.

$arr1 = ["apple", "banana", "orange"];


$arr2 = ["banana"];
$result = array_diff($arr1, $arr2);
// $result is ["apple", "orange"]
CONCLUSION
Arrays in PHP are a fundamental and flexible data structure, allowing
developers to store and manipulate multiple values efficiently. With
different types like indexed, associative, and multidimensional arrays,
PHP provides a versatile approach to handling various data forms. Built-
in array functions make operations like adding, removing, and merging
elements straightforward. Understanding arrays is essential for
managing dynamic data in PHP, enabling the development of robust and
scalable applications.

REFERENCES
1. https://www.geeksforgeeks.org

2.https://www.academia.edu

3.https://iopscience.iop.org

4.https://ieeexplore.ieee.org

You might also like