1$colors = array("blue","green","red");
2
3//delete element in array by value "green"
4if (($key = array_search("green", $colors)) !== false) {
5 unset($colors[$key]);
6}
1sizeof($arr) //returns the size of elements in the array
2is_array($arr) // returns true if $arr is an array and false otherwise
3in_array($var, $arr) // check if $var is in the array $arr
4print_r($arr) // prints the complete representation of the array
5array_merge($arr1, $arr2) //combines $arr1 and $arr2 into a single array
6array_values($arr) //store all the values into a new array without the keys but only the values
7array_keys($arr) // returns only the keys inside an array
8array_pop($arr) // removes the last element of the array
9array_push($arr, $val) // pushes $val to the end of array
10array_shift($arr) // removes the first element in the array $arr
11sort($arr) // sorts an array in an ascending order
12
13/*Other sorting methods are:
14-asort()
15-arsort()
16-ksort()
17-krsort()
18-rsort()
19*/
20
21array_map('function_name', $arr) // passes each value in the array inside the fuction and do the operation on the array data
22array_flip($arr) //This function interchange the keys and the values of a PHP associative array.
23array_reverse($arr) //This function is used to reverse the order of elements
24array_rand($arr) //randomly pick an element from the array
25array_slice($arr, $offset, $length)//This function is used to create a subset of any array
26
27
28
29
30
31
1print_r(deep_delete_keys($arr,'country'));
2
3function deep_delete_keys($arr, $keys) {
4 if (!is_array($keys)) $keys = array($keys);
5 $filteredArr = array_diff_key( $arr, array_flip( $keys ) );
6 foreach ($filteredArr as &$val) {
7 if (is_array($val)) {
8 $val = deep_delete_keys($val, $keys);
9 }
10 }
11 return $filteredArr;
12}
1
2const triplets = (arr1,arr2) => {
3 let score1 = 0;
4 let score2 = 0;
5 let resultArr = [0,0]
6 for (let i = 0; i < arr1.length; i++){
7 if(arr1[i] === arr2[i]) {
8 resultArr[0] = score1
9 resultArr[1] = score2
10 } else if (arr1[i] > arr2[i]) {
11 score1++
12 resultArr[0] = score1
13 } else if (arr1[i] < arr2[i]) {
14 score2++
15 resultArr[1] = score2
16 }
17 }
18 return resultArr
19}
1
2<?php
3$array = array(
4 "foo" => "bar",
5 42 => 24,
6 "multi" => array(
7 "dimensional" => array(
8 "array" => "foo"
9 )
10 )
11);
12
13var_dump($array["foo"]);
14var_dump($array[42]);
15var_dump($array["multi"]["dimensional"]["array"]);
16?>
17
18
1$arrRoom[] = array("RoomCode" => "Deluxe",
2 "Rates" => array ( array(
3 "BoardCode" => "RO",
4 "Price" => 100)
5 ));
6$arrRoom[] = array("RoomCode" => "Standard",
7 "Rates" => array ( array(
8 "BoardCode" => "RO",
9 "Price" => 100)
10 ));
11$arrRoom[] = array("RoomCode" => "Deluxe",
12 "Rates" => array (array(
13 "BoardCode" => "RO",
14 "Price" => 200)
15 ));
16
17foreach($arrRoom as $room)
18{
19 foreach($room['Rates'] as $rates)
20 {
21 $nRooms[$room['RoomCode']][$rates['BoardCode']][] = array("RoomCode" => $room['RoomCode'],
22 "MealCode" => $rates['BoardCode'],
23 "Price" => $rates['Price']);
24 }
25}
26echo "\n ==== Output in Json Format ==== \n";
27{
28 "Deluxe": {
29 "RO": [
30 {
31 "RoomCode": "Deluxe",
32 "MealCode": "RO",
33 "Price": 100
34 },
35 {
36 "RoomCode": "Deluxe",
37 "MealCode": "RO",
38 "Price": 200
39 }
40 ]
41 },
42 "Standard": {
43 "RO": [
44 {
45 "RoomCode": "Standard",
46 "MealCode": "RO",
47 "Price": 100
48 }
49 ]
50 }
51}