Array functions


https://russelltheprogrammer.com

copied from W3programmers

foreach()

foreach() function এর মাধ্যমে আমরা খুব সহজেই array এর key এবং value গুলোকে print করতে পার

<?php

$array=["a"=>"America","b"=>"Bangladesh","c"=>"Canada","e"=>"England"];
foreach($array as $value){
    echo $value,"\n";
    }
?>

count()

Array তে total element সংখ্যা কত তা জানার জন্য count() function ব্যবহার করা হয়

<?php
$arr = array("a"=>'coffee', 'brown', 'caffeine',5,3.4,true);
#Result: 6
echo count($arr);

array_count_values()

array এর মধ্যে কোন value টি কতবার আছে

<?php
$arr = array(1,3,2,1,5,6,1,2,3);
print_r(array_count_values($arr));

array_keys()

array এর শুধু column name গুলোকে নিয়ে আলাদা array করতে চাইলে

<?php
$student= ["name"=>"Mamnun Bin Masud","age"=>4,"mobile"=>"01766882299"];
echo "Print Only Array Keys\n";
print_r(array_keys($student));
Print Only Array Keys
Array
(
[0] => name
[1] => age
[2] => mobile
)

array_values()

value গুলোকে নিয়ে আলাদা array করতে চাইলে

<?php
$student= ["name"=>"Mamnun Bin Masud","age"=>4,"mobile"=>"01766882299"];
echo "Print Only Array Values\n";
print_r(array_values($student));
Print Only Array Values
Array
(
[0] => Mamnun Bin Masud
[1] => 4
[2] => 01766882299
)

array_product()

array এর সব গুলো value এর গুনফল বের করতে চাইলে

<?php
$arr = array(1,2,3,4,5,6);
echo "Multiplication of all array Element: ";
print_r(array_product($arr));
Multiplication of all array Element: 720

array_sum()

array এর সব গুলো value এর যোগফল বের করতে চাইলে আপনাকে array_sum() function
<?php
$arr = array(1,2,3,4,5,6);
echo "Summation of all Array Element: ";
print_r(array_sum($arr));
Summation of all Array Element: 21

array_flip()

array এর key গুলোকে value তে রূপান্তর অথবা value গুলোকে key তে রূপান্তর করতে চাইলে

<?php
$input = array("oranges", "apples", "pears");
echo "Exmple of array_flip() Function\n";
$flipped = array_flip($input);
print_r($flipped);
?>
Exmple of array_flip() Function
Array
(
[oranges] => 0
[apples] => 1
[pears] => 2
)

array_reverse()

array এর সব গুলো value কে বিপরীত দিক থেকে সাজাতে চাইলে

<?php
$input = array("oranges", "apples", "pears");
echo "Exmple of array_flip() Function\n";
$flipped = array_flip($input);
echo "Exmple of array_reverse() Function\n";
$reversed = array_reverse($input);
print_r($reversed);
?>
Exmple of array_reverse() Function
Array
(
[0] => pears
[1] => apples
[2] => oranges
)

array_rand ()

array এর value গুলো থেকে এক বা একাধিক value কে (এলো-মেলো করে ) randomly পিক (বের) করতে চাইলে

<?php
$input = array("oranges", "apples", "pears","Mango","Banana");
echo "Exmple of array_rand() Function\n";
$randomly_pick = array_rand($input,2);
echo $randomly_pick[0],"\n",$randomly_pick[1],"\n";
Exmple of array_rand() Function
1
4

shuffle()

array এর সব গুলো value কে এলো-মেলো (shuffling) করতে চাইলে

<?php
$input = array("oranges", "apples", "pears","Mango","Banana");
echo "Exmple of array_rand() Function\n";
$randomly_pick = array_rand($input,2);
echo $randomly_pick[0],"\n",$randomly_pick[1],"\n";
echo "Exmple of shuffle() Function\n";
shuffle($input);
print_r($input);
Exmple of shuffle() Function
Array
(
[0] => pears
[1] => oranges
[2] => Mango
[3] => apples
[4] => Banana
)

in_array()

array এর মধ্যে কোনো নির্দিষ্ট value আছে কিনা তা চেক করতে চাইলে

<?php
$fruits= ["a"=>"Apple","b"=>"Banana","c"=>"Cadbury"];
echo in_array("Apple",$fruits)?"Yes, Apple is available":"Not Available";
Yes, Apple is available

array_key_exists()

array এর মধ্যে কোনো key আছে কিনা তা চেক করতে চাইলে

<?php
$fruits= ["a"=>"Apple","b"=>"Banana","c"=>"Cadbury"];
echo array_key_exists("b",$fruits)?"Yes, b key is available":"Not Available";
Yes, b key is available

extract()

array এর key গুলোকে variable এ রূপান্তর করার জন্য

<?php

$input = array("o"=>"oranges", "a"=>"apples", "p"=>"pears","m"=>"Mango","b"=>"Banana");
extract($input);
echo $o,"\n";
echo $a,"\n";
echo $p,"\n";
echo $m,"\n";
echo $b,"\n";
oranges
apples
pears
Mango
Banana

compact()

PHP Page এর মধ্যে অবস্থিত Variable গুলোকে array element এ রূপান্তর
<?php
$city  = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$result = compact("city", "state", "event");
echo "<pre>";
print_r($result);
echo "</pre>";
?>
Array
(
[city] => San Francisco
[state] => CA
[event] => SIGGRAPH
)

range()

PHP তে Number অথবা letter দিয়ে নির্দিষ্ট range এর array তৈরী করার জন্য

<?php
$numbers=range(5,10);
echo "Ascending Order: \n";
print_r($numbers);
echo "\n";
echo "Descending Order: \n";
$numbers2=range(10,5);
print_r($numbers2);
$letters=range("a","f");
echo "Ascending Order: \n";
print_r($letters);
echo "\n";
echo "Descending Order: \n";
$letters2=range("f","a");
print_r($letters2);

Result

Ascending Order: 
Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
[4] => 9
[5] => 10
)

Descending Order: 
Array
(
[0] => 10
[1] => 9
[2] => 8
[3] => 7
[4] => 6
[5] => 5
)
Ascending Order: 
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
)

Descending Order: 
Array
(
[0] => f
[1] => e
[2] => d
[3] => c
[4] => b
[5] => a
)

array_unique()

array এর মধ্যে অবস্থিত value গুলোর মধ্যে unique value নিয়ে নতুন array তৈরী করার জন্য

<?php
$arr=[1,3,2,4,1,2,5];
$unique=array_unique($arr);
print_r($unique);

Result

Array
(
[0] => 1
[1] => 3
[2] => 2
[3] => 4
[6] => 5
)

sort()

array এর value গুলোকে ascending order এ সাজানোর জন্য

<?php
echo "Number Sorting";
$numbers = [2,4, 5, 1, 13];
sort($numbers);
echo "<pre>";
print_r($numbers);
echo "</pre>";
echo "Letters Sorting";
$letters = ['c','a','d','b'];
sort($letters);
echo "<pre>";
print_r($letters);
echo "</pre>";
echo "String Sorting";
$strings=["a"=>"aa","a","aaa","abc","abcd","aaaa"];
sort($strings);
echo "<pre>";
print_r($strings);
echo "</pre>";
?>

Output

Number Sorting
Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
[4] => 13
)
Letters Sorting
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
String Sorting
Array
(
[0] => a
[1] => aa
[2] => aaa
[3] => aaaa
[4] => abc
[5] => abcd
)

rsort()

array এর value গুলোকে Descending Order বা reverse order এ সাজানোর জন্য

<?php
echo "Number Sorting in Reverse Order";
$numbers = [2,4, 5, 1, 13];
rsort($numbers);
echo "<pre>";
print_r($numbers);
echo "</pre>";
echo "Letters Sorting in Reverse Order";
$letters = ['c','a','d','b'];
rsort($letters);
echo "<pre>";
print_r($letters);
echo "</pre>";
echo "String Sorting in Reverse Order";
$strings=["a"=>"aa","a","aaa","abc","abcd","aaaa"];
rsort($strings);
echo "<pre>";
print_r($strings);
echo "</pre>";
?>

Output

Number Sorting in Reverse Order
Array
(
[0] => 13
[1] => 5
[2] => 4
[3] => 2
[4] => 1
)
Letters Sorting in Reverse Order
Array
(
[0] => d
[1] => c
[2] => b
[3] => a
)
String Sorting in Reverse Order
Array
(
[0] => abcd
[1] => abc
[2] => aaaa
[3] => aaa
[4] => aa
[5] => a
)

asort()

index ঠিক রেখে ascending order এ সাজানোর জন্য

<?php
$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","e"=>"apple");
asort($fruits);
echo "<pre>";
print_r($fruits);
echo "</pre>";
?>

Output

Array
(
[e] => apple
[b] => banana
[d] => lemon
[a] => orange
)

natsort()

Natural Sorting অর্থাৎ img1, img2, img10, img12 এইভাবে প্রদর্শন করুক, তাহলে আপনাকে natsort() function

<?php
$array = array("img12.png", "img10.png", "img2.png", "img1.png");
natsort($array);
echo "\nNatural order sorting\n";
echo "<pre>";
print_r($array);
echo "</pre>";
?>

Output

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

natcasesort()

IMG0.png, IMG3.png, img1.png, img2.png এইভাবে sorting হচ্ছে , এখানে আসা উচিত ছিল IMG0.png, img1.png, img2.png, IMG3.png এইভাবে। আর এই ধরণের case জনিত সমস্যা আপনি সহজেই php natcasesort()

<?php
$array= array('IMG0.png','img12.png','img10.png','img2.png','img1.png','IMG3.png');
natcasesort($array);
echo "\nNatural order sorting (case-insensitive)\n";
echo "<pre>";
print_r($array);
echo "</pre>";
?>

Output

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

PHP arsort() Function

arsort টি ও asort এর মতোই index ঠিক রেখে Sorting করে , তবে এটি ascending order এর পরিবর্তে Descending Order এ array এর value গুলোকে Sorting করে

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "e" => "apple");
arsort($fruits);
echo "<pre>";
print_r($fruits);
echo "</pre>";
?>

Output

Array
(
[a] => orange
[d] => lemon
[b] => banana
[e] => apple
)

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *




Calender

October 2020
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031