SlideShare a Scribd company logo
1 of 34
PHP Array Functions
 PHP provides various array functions to access and
manipulate the elements of array.
 rray function will save you a lot of time as they are
pre-defined in PHP libraries and all you have to do is
call them to use them.

 This function returns the size of the array or the
number of data elements stored in the array.
 It is just like count($arr) method.
sizeof($arr)
 <?php
 $lamborghinis = array("Urus", "Huracan",
"Aventador");
 echo "Size of the array is: ". sizeof($lamborghinis);
 ?>
 Output:
 Size of the array is: 3
 To check whether the provided data is in form of an array,
we can use the is_array() function.
 <?php
 $lamborghinis = array("Urus", "Huracan", "Aventador"); //
using ternary operator
 echo is_array($lamborghinis) ? 'Array' : 'not an Array';
$mycar = "Urus"; // using ternary operator
 echo is_array($mycar) ? 'Array' : 'not an Array';
 ?>
 Output:
 Array
 not an Array
is_array($arr)
 Although this is not an array function, but it deserves
a special mention here, as we can use this function to
print the array in the most descriptive way possible.
 This function prints the complete representation of
the array, along with all the keys and values.
print_r($arr)
 <?php
 $lamborghinis = array("Urus", "Huracan", "Aventador");
 print_r($lamborghinis);
 ?>
 Output:
 Array
 (
 [0] => "Urus"
 [1] => "Huracan"
 [2] => "Aventador"
 )
 If you want to combine two different arrays into a
single array, you can do so using this function.
 It doesn't matter whether the arrays to be combined
are of same type(indexed, associative etc)
 or different types, using this function we can combine
them into one single array.
array_merge($arr1, $arr2)
 <?php
 $hatchbacks = array( "Suzuki" => "Baleno",
 "Skoda" => "Fabia",
 "Hyundai" => "i20",
 "Tata" => "Tigor" ); // friends who own the above cars
$friends = array("Vinod", "Javed", "Navjot",
"Samuel"); // let's merge the two arrays into one
$merged = array_merge($hatchbacks, $friends);
print_r($merged);
 ?>
 Output:
 Array
 (
 [Suzuki] => Baleno
 [Skoda] => Fabia
 [Hyundai] => i20
 [Tata] => Tigor
 [0] => Vinod
 [1] => Javed
 [2] => Navjot
 [3] => Samuel
 )
 In an array, data is stored in form of key-value pairs,
where key can be numerical(in case of indexed array)
or user-defined strings(in case of associative array)
and values.
 If we want to take all the values from our array,
without the keys, and store them in a separate array,
then we can use array_values() function.
array_values($arr)
 <?php
 $hatchbacks = array(
 "Suzuki" => "Baleno",
 "Skoda" => "Fabia",
 "Hyundai" => "i20",
 "Tata" => "Tigor"
 ); // friends who own the above cars
 $friends = array("Vinod", "Javed", "Navjot", "Samuel"); //
let's merge the two arrays into one
 $merged = array_merge($hatchbacks, $friends); //getting
only the values
 $merged = array_values($merged);
 print_r($merged);
 ?>
 Output:
 Array
 (
 [0] => Baleno
 [1] => Fabia
 [2] => i20
 [3] => Tigor
 [4] => Vinod
 [5] => Javed
 [6] => Navjot
 [7] => Samuel
 )
 extract just the keys from an array. Let's use this
function to extract the keys from the array $merged.
array_keys($arr)
 <?php
 //getting only the keys
 $keys = array_values($merged);
 print_r($keys);
 ?>
 Output:
 Array (
 [0] => Suzuki
 [1] => Skoda
 [2] => Hyundai
 [3] => Tata
 [4] => 0
 [5] => 1
 [6] => 2
 [7] => 3
 )
 This function removes the last element of the array.
 Hence it can be used to remove one element from the
end.
array_pop($arr)
 <?php
 $lamborghinis = array("Urus", "Huracan", "Aventador"); //
removing the last element array_pop($lamborghinis);
 print_r($lamborghinis);
 ?>
 Output:
 Array (
 [0] => Urus
 [1] => Huracan
 )
 This function is the opposite of
the array_pop() function.
 This can be used to add a new element at the end of
the array.
array_push($arr, $val)
 <?php
 $lamborghinis = array("Urus", "Huracan", "Aventador"); //
adding a new element at the end
array_push($lamborghinis, "Estoque");
print_r($lamborghinis);
 ?>
 Output:
 Array (
 [0] => Urus
 [1] => Huracan
 [2] => Aventador
 [3] => Estoque
 )
 This function can be used to remove/shift the first
element out of the array.
 So, it is just like array_pop() function but different in
terms of the position of the element removed.
array_shift($arr)
 <?php
 $lamborghinis = array("Urus", "Huracan",
"Aventador"); // removing the first element
array_shift($lamborghinis);
 print_r($lamborghinis);
 ?>
 Output:
 Array (
 [0] => Huracan
 [1] => Aventador
 )
 Similar to this, we have another
function array_unshift($arr, $val) to add a new
value($val) at the start of the array(as the first
element).
 This function sorts the array elements in ascending
order. In case of a string value array, values are sorted
in ascending alphabetical order.
 Some other sorting functions
are: asort(), arsort(), ksort(), krsort() and rsort().
sort($arr)
 <?php
 $lamborghinis = array("Urus", "Huracan",
"Aventador", "Estoque"); // sort the array
sort($lamborghinis);
 print_r($lamborghinis);
 ?>
 Output:
 Array (
 [0] => Aventador
 [1] => Estoque
 [2] => Huracan
 [3] => Urus )
 If you want to perform certain operation on all the values
stored in an array, you can do it by iterating over the array
using a for loop or foreach and performing the required
operation on all the values of the array.
 Or, you can use the function array_map(). All we have to
do is define a separate function to which we will provide
the values stored in the array one by one(one at a time)
and it will perform the operation on the values. Let's have
an example,
array_map('function_name', $arr)
 <?php
 function addOne($val)
 { // adding 1 to input value
 return ($val + 1);
 }
 $numbers = array(10, 20, 30, 40, 50); // using
array_map to operate on all the values stored in array
$numbers = array_map('addOne', $numbers);
print_r($numbers)
 ?>
 Output:
 Array (
 [0] => 11
 [1] => 21
 [2] => 31
 [3] => 41
 [4] => 51
 )
 The function array_walk($arr, 'function_name') works
just like the array_map() function.

 This function interchange the keys and the values of a
PHP associative array.
array_flip($arr)
 <?
 php $hatchbacks = array( "Suzuki" => "Baleno",
"Skoda" => "Fabia", "Hyundai" => "i20", "Tata" =>
"Tigor" ); // we can directly print the result of array
flipping
 print_r(array_flip($hatchbacks));
 ?>
 Output:
 Array (
 [Baleno] => Suzuki
 [Fabia] => Skoda
 [i20] => Hyundai
 [Tigor] => Tata
 )

More Related Content

Similar to PHP Array Functions.pptx

PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
 
PHP for Python Developers
PHP for Python DevelopersPHP for Python Developers
PHP for Python DevelopersCarlos Vences
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvZahouAmel1
 
Laravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SPLaravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SPMatheus Marabesi
 
PHP tips and tricks
PHP tips and tricks PHP tips and tricks
PHP tips and tricks Damien Seguy
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128PrinceGuru MS
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Perl.predefined.variables
Perl.predefined.variablesPerl.predefined.variables
Perl.predefined.variablesKing Hom
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHPHari K T
 
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011camp_drupal_ua
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHPTaras Kalapun
 
Smarty Template
Smarty TemplateSmarty Template
Smarty Templateguest48224
 
Smarty Template
Smarty TemplateSmarty Template
Smarty Templatemussawir20
 

Similar to PHP Array Functions.pptx (20)

PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
PHP for Python Developers
PHP for Python DevelopersPHP for Python Developers
PHP for Python Developers
 
Php array
Php arrayPhp array
Php array
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
 
Laravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SPLaravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SP
 
Functional programming with php7
Functional programming with php7Functional programming with php7
Functional programming with php7
 
PHP tips and tricks
PHP tips and tricks PHP tips and tricks
PHP tips and tricks
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Perl.predefined.variables
Perl.predefined.variablesPerl.predefined.variables
Perl.predefined.variables
 
Evolving Tests
Evolving TestsEvolving Tests
Evolving Tests
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
 
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
 
What are arrays in java script
What are arrays in java scriptWhat are arrays in java script
What are arrays in java script
 
Java script arrays
Java script arraysJava script arrays
Java script arrays
 
Java script arrays
Java script arraysJava script arrays
Java script arrays
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHP
 
Smarty Template
Smarty TemplateSmarty Template
Smarty Template
 
Smarty Template
Smarty TemplateSmarty Template
Smarty Template
 

Recently uploaded

/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In.../:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...lizamodels9
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfpollardmorgan
 
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service Jamshedpur
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service JamshedpurVIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service Jamshedpur
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service JamshedpurSuhani Kapoor
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdfRenandantas16
 
(8264348440) 🔝 Call Girls In Keshav Puram 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Keshav Puram 🔝 Delhi NCR(8264348440) 🔝 Call Girls In Keshav Puram 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Keshav Puram 🔝 Delhi NCRsoniya singh
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfPaul Menig
 
Regression analysis: Simple Linear Regression Multiple Linear Regression
Regression analysis:  Simple Linear Regression Multiple Linear RegressionRegression analysis:  Simple Linear Regression Multiple Linear Regression
Regression analysis: Simple Linear Regression Multiple Linear RegressionRavindra Nath Shukla
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMRavindra Nath Shukla
 
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
Keppel Ltd. 1Q 2024 Business Update  Presentation SlidesKeppel Ltd. 1Q 2024 Business Update  Presentation Slides
Keppel Ltd. 1Q 2024 Business Update Presentation SlidesKeppelCorporation
 
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...lizamodels9
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...anilsa9823
 
Vip Female Escorts Noida 9711199171 Greater Noida Escorts Service
Vip Female Escorts Noida 9711199171 Greater Noida Escorts ServiceVip Female Escorts Noida 9711199171 Greater Noida Escorts Service
Vip Female Escorts Noida 9711199171 Greater Noida Escorts Serviceankitnayak356677
 
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptx
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptxBanana Powder Manufacturing Plant Project Report 2024 Edition.pptx
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptxgeorgebrinton95
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis UsageNeil Kimberley
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service PuneVIP Call Girls Pune Kirti 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service PuneCall girls in Ahmedabad High profile
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...lizamodels9
 
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 

Recently uploaded (20)

/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In.../:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
/:Call Girls In Indirapuram Ghaziabad ➥9990211544 Independent Best Escorts In...
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
 
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service Jamshedpur
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service JamshedpurVIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service Jamshedpur
VIP Call Girl Jamshedpur Aashi 8250192130 Independent Escort Service Jamshedpur
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
 
(8264348440) 🔝 Call Girls In Keshav Puram 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Keshav Puram 🔝 Delhi NCR(8264348440) 🔝 Call Girls In Keshav Puram 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Keshav Puram 🔝 Delhi NCR
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdf
 
Regression analysis: Simple Linear Regression Multiple Linear Regression
Regression analysis:  Simple Linear Regression Multiple Linear RegressionRegression analysis:  Simple Linear Regression Multiple Linear Regression
Regression analysis: Simple Linear Regression Multiple Linear Regression
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSM
 
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
Keppel Ltd. 1Q 2024 Business Update  Presentation SlidesKeppel Ltd. 1Q 2024 Business Update  Presentation Slides
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
 
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
 
Vip Female Escorts Noida 9711199171 Greater Noida Escorts Service
Vip Female Escorts Noida 9711199171 Greater Noida Escorts ServiceVip Female Escorts Noida 9711199171 Greater Noida Escorts Service
Vip Female Escorts Noida 9711199171 Greater Noida Escorts Service
 
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptx
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptxBanana Powder Manufacturing Plant Project Report 2024 Edition.pptx
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptx
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service PuneVIP Call Girls Pune Kirti 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service Pune
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
 
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
 
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 

PHP Array Functions.pptx

  • 2.  PHP provides various array functions to access and manipulate the elements of array.  rray function will save you a lot of time as they are pre-defined in PHP libraries and all you have to do is call them to use them. 
  • 3.  This function returns the size of the array or the number of data elements stored in the array.  It is just like count($arr) method. sizeof($arr)
  • 4.  <?php  $lamborghinis = array("Urus", "Huracan", "Aventador");  echo "Size of the array is: ". sizeof($lamborghinis);  ?>  Output:  Size of the array is: 3
  • 5.  To check whether the provided data is in form of an array, we can use the is_array() function.  <?php  $lamborghinis = array("Urus", "Huracan", "Aventador"); // using ternary operator  echo is_array($lamborghinis) ? 'Array' : 'not an Array'; $mycar = "Urus"; // using ternary operator  echo is_array($mycar) ? 'Array' : 'not an Array';  ?>  Output:  Array  not an Array is_array($arr)
  • 6.  Although this is not an array function, but it deserves a special mention here, as we can use this function to print the array in the most descriptive way possible.  This function prints the complete representation of the array, along with all the keys and values. print_r($arr)
  • 7.  <?php  $lamborghinis = array("Urus", "Huracan", "Aventador");  print_r($lamborghinis);  ?>  Output:  Array  (  [0] => "Urus"  [1] => "Huracan"  [2] => "Aventador"  )
  • 8.  If you want to combine two different arrays into a single array, you can do so using this function.  It doesn't matter whether the arrays to be combined are of same type(indexed, associative etc)  or different types, using this function we can combine them into one single array. array_merge($arr1, $arr2)
  • 9.  <?php  $hatchbacks = array( "Suzuki" => "Baleno",  "Skoda" => "Fabia",  "Hyundai" => "i20",  "Tata" => "Tigor" ); // friends who own the above cars $friends = array("Vinod", "Javed", "Navjot", "Samuel"); // let's merge the two arrays into one $merged = array_merge($hatchbacks, $friends); print_r($merged);  ?>
  • 10.  Output:  Array  (  [Suzuki] => Baleno  [Skoda] => Fabia  [Hyundai] => i20  [Tata] => Tigor  [0] => Vinod  [1] => Javed  [2] => Navjot  [3] => Samuel  )
  • 11.  In an array, data is stored in form of key-value pairs, where key can be numerical(in case of indexed array) or user-defined strings(in case of associative array) and values.  If we want to take all the values from our array, without the keys, and store them in a separate array, then we can use array_values() function. array_values($arr)
  • 12.  <?php  $hatchbacks = array(  "Suzuki" => "Baleno",  "Skoda" => "Fabia",  "Hyundai" => "i20",  "Tata" => "Tigor"  ); // friends who own the above cars  $friends = array("Vinod", "Javed", "Navjot", "Samuel"); // let's merge the two arrays into one  $merged = array_merge($hatchbacks, $friends); //getting only the values  $merged = array_values($merged);  print_r($merged);  ?>
  • 13.  Output:  Array  (  [0] => Baleno  [1] => Fabia  [2] => i20  [3] => Tigor  [4] => Vinod  [5] => Javed  [6] => Navjot  [7] => Samuel  )
  • 14.  extract just the keys from an array. Let's use this function to extract the keys from the array $merged. array_keys($arr)
  • 15.  <?php  //getting only the keys  $keys = array_values($merged);  print_r($keys);  ?>
  • 16.  Output:  Array (  [0] => Suzuki  [1] => Skoda  [2] => Hyundai  [3] => Tata  [4] => 0  [5] => 1  [6] => 2  [7] => 3  )
  • 17.  This function removes the last element of the array.  Hence it can be used to remove one element from the end. array_pop($arr)
  • 18.  <?php  $lamborghinis = array("Urus", "Huracan", "Aventador"); // removing the last element array_pop($lamborghinis);  print_r($lamborghinis);  ?>  Output:  Array (  [0] => Urus  [1] => Huracan  )
  • 19.  This function is the opposite of the array_pop() function.  This can be used to add a new element at the end of the array. array_push($arr, $val)
  • 20.  <?php  $lamborghinis = array("Urus", "Huracan", "Aventador"); // adding a new element at the end array_push($lamborghinis, "Estoque"); print_r($lamborghinis);  ?>  Output:  Array (  [0] => Urus  [1] => Huracan  [2] => Aventador  [3] => Estoque  )
  • 21.  This function can be used to remove/shift the first element out of the array.  So, it is just like array_pop() function but different in terms of the position of the element removed. array_shift($arr)
  • 22.  <?php  $lamborghinis = array("Urus", "Huracan", "Aventador"); // removing the first element array_shift($lamborghinis);  print_r($lamborghinis);  ?>
  • 23.  Output:  Array (  [0] => Huracan  [1] => Aventador  )
  • 24.  Similar to this, we have another function array_unshift($arr, $val) to add a new value($val) at the start of the array(as the first element).
  • 25.  This function sorts the array elements in ascending order. In case of a string value array, values are sorted in ascending alphabetical order.  Some other sorting functions are: asort(), arsort(), ksort(), krsort() and rsort(). sort($arr)
  • 26.  <?php  $lamborghinis = array("Urus", "Huracan", "Aventador", "Estoque"); // sort the array sort($lamborghinis);  print_r($lamborghinis);  ?>
  • 27.  Output:  Array (  [0] => Aventador  [1] => Estoque  [2] => Huracan  [3] => Urus )
  • 28.  If you want to perform certain operation on all the values stored in an array, you can do it by iterating over the array using a for loop or foreach and performing the required operation on all the values of the array.  Or, you can use the function array_map(). All we have to do is define a separate function to which we will provide the values stored in the array one by one(one at a time) and it will perform the operation on the values. Let's have an example, array_map('function_name', $arr)
  • 29.  <?php  function addOne($val)  { // adding 1 to input value  return ($val + 1);  }  $numbers = array(10, 20, 30, 40, 50); // using array_map to operate on all the values stored in array $numbers = array_map('addOne', $numbers); print_r($numbers)  ?>
  • 30.  Output:  Array (  [0] => 11  [1] => 21  [2] => 31  [3] => 41  [4] => 51  )
  • 31.  The function array_walk($arr, 'function_name') works just like the array_map() function. 
  • 32.  This function interchange the keys and the values of a PHP associative array. array_flip($arr)
  • 33.  <?  php $hatchbacks = array( "Suzuki" => "Baleno", "Skoda" => "Fabia", "Hyundai" => "i20", "Tata" => "Tigor" ); // we can directly print the result of array flipping  print_r(array_flip($hatchbacks));  ?>
  • 34.  Output:  Array (  [Baleno] => Suzuki  [Fabia] => Skoda  [i20] => Hyundai  [Tigor] => Tata  )