Answers for "common array methods php"

PHP
15

common array methods php

$colors = array("blue","green","red");

//delete element in array by value "green"
if (($key = array_search("green", $colors)) !== false) {
    unset($colors[$key]);
}
Posted by: Guest on October-30-2019
5

common array methods php

sizeof($arr) //returns the size of elements in the array
is_array($arr) // returns true if $arr is an array and false otherwise
in_array($var, $arr) // check if $var is in the array $arr
print_r($arr) // prints the complete representation of the array
array_merge($arr1, $arr2) //combines $arr1 and $arr2 into a single array
array_values($arr) //store all the values into a new array without the keys but only the values
array_keys($arr) // returns only the keys inside an array
array_pop($arr) // removes the last element of  the array
array_push($arr, $val) // pushes $val to the end of array
array_shift($arr) // removes the first element in the array $arr
sort($arr) // sorts an array in an ascending order

/*Other sorting methods are:
-asort()
-arsort()
-ksort()
-krsort()
-rsort()
*/

array_map('function_name', $arr) // passes each value in the array inside the fuction and do the operation on the array data
array_flip($arr) //This function interchange the keys and the values of a PHP associative array.
array_reverse($arr) //This function is used to reverse the order of elements
array_rand($arr) //randomly pick an element from the array
array_slice($arr, $offset, $length)//This function is used to create a subset of any array
Posted by: Guest on July-15-2020
1

common array methods php

if (count($arr) > 1) {
     ....
}
Posted by: Guest on June-12-2020
1

common array methods php

$key_of_max_value = array_search(max($arrCompare),$arrCompare);
Posted by: Guest on May-06-2020
1

common array methods php

print_r(deep_delete_keys($arr,'country'));

function deep_delete_keys($arr, $keys) {
    if (!is_array($keys)) $keys = array($keys);
    $filteredArr = array_diff_key( $arr, array_flip( $keys ) );
    foreach ($filteredArr as &$val) {
        if (is_array($val)) {
            $val = deep_delete_keys($val, $keys);
        }
    }
    return $filteredArr;
}
Posted by: Guest on March-28-2020
1

common array methods php

const triplets = (arr1,arr2) => {
  let score1 = 0;
  let score2 = 0;
  let resultArr = [0,0]
  for (let i = 0; i < arr1.length; i++){
    if(arr1[i] === arr2[i]) {
      resultArr[0] = score1
      resultArr[1] = score2
    } else if (arr1[i] > arr2[i]) {
      score1++
      resultArr[0] = score1
    } else if (arr1[i] < arr2[i]) {
      score2++
      resultArr[1] = score2
    }
  }
  return resultArr
}
Posted by: Guest on December-23-2019
4

common array methods php

<?php
$array = array(
    "foo" => "bar",
    42    => 24,
    "multi" => array(
         "dimensional" => array(
             "array" => "foo"
         )
    )
);

var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>
Posted by: Guest on June-17-2020
3

common array methods php

<?php
$array = array("foo", "bar", "hello", "world");
var_dump($array);
?>
Posted by: Guest on June-17-2020
1

common array methods php

$arrRoom[] = array("RoomCode" => "Deluxe",
                    "Rates" => array ( array(
                        "BoardCode" => "RO",
                        "Price" => 100)
                    ));
$arrRoom[] = array("RoomCode" => "Standard",
                    "Rates" => array ( array(
                        "BoardCode" => "RO",
                        "Price" => 100)                        
                    ));                    
$arrRoom[] = array("RoomCode" => "Deluxe",
                    "Rates" => array (array(
                        "BoardCode" => "RO",
                        "Price" => 200)
                    ));

foreach($arrRoom as $room)
{    
    foreach($room['Rates'] as $rates)
    {        
        $nRooms[$room['RoomCode']][$rates['BoardCode']][] = array("RoomCode" => $room['RoomCode'],
                                                            "MealCode" => $rates['BoardCode'],
                                                            "Price" => $rates['Price']);
    }
}
echo "\n ==== Output in Json Format ==== \n";
{
    "Deluxe": {
        "RO": [
            {
                "RoomCode": "Deluxe",
                "MealCode": "RO",
                "Price": 100
            },
            {
                "RoomCode": "Deluxe",
                "MealCode": "RO",
                "Price": 200
            }
        ]
    },
    "Standard": {
        "RO": [
            {
                "RoomCode": "Standard",
                "MealCode": "RO",
                "Price": 100
            }
        ]
    }
}
Posted by: Guest on May-26-2020

Code answers related to "common array methods php"

Browse Popular Code Answers by Language