PHP Array with explanation

<?php 
$ar1[] = 10; // index 0 value 10
$ar1[] = 20; // index 1 value 20

echo '<pre>'; // <pre> use to arrangement print of array
print_r($ar1); // print_r(); to print array with value and index
echo $ar1[0]; // to print the defined array

$ar2[1] = 10; // coder define key or index for array
$ar2[3] = 20; // her coder define key is 3

print_r($ar2); // to print array

$ar3 = array("a","b","c"); // This array define structure is to use contain multiple value in single variable
print_r($ar3); // print_r(); to print array

$ar4 = array(    // User define numerical & associative key together in an array
    0=>"a", // user define numerical key
    "z"=>"b", // user define string key
    3=>"c" // user define numerical key
    );
print_r($ar4);    // print_r(); to print array
echo $ar4[3];    // to echo user define index '3'
echo '<br/>';
echo '<br/>';

$var = 10; // This is a simple variable not an array

//echo is_array($ar4);
//echo is_int(10.5);

if(is_array($ar4)){ // is_array(); function to check variable is it array??
foreach($ar4 as $key=>$value){ // foreach loop running array
    echo $key." : "; // printing key
    echo $value.'<br/>'; // printing value
}
}
echo count($ar4); // count(); function to count how many value ???


$cars = array("Volvo", "BMW", "Toyota"); // string variables in array
$numbers = array(500, 700, 200); // number variables in array

sort($cars); // sort(); function to arrange value into ascending order
arsort($numbers); //arsort(); function to arrange vale into descending order

print_r($cars); // Now array will be print into ascending order cause
                // it's before $car was in sort(); function

print_r($numbers); // Now array will be print into descending order cause
                   // it's before $numbers was in arsort(); function

die("program end"); // die(); function use to stop running below code
exit("program end"); // exit(); function use to stop running below code
break; // break; used in loop to stop loop execution from where break; called.

$ar5 = array(2=>"a",1=>"b",5=>"c"); // this is an array with key and value
krsort($ar5); // krsort(); function to arrange value into descending order
var_dump($ar5); // var_dump(); function to print array with values type(sring/int/float)


if(is_array($ar4)){ //is_array(); function to chech array, is it array ?? & if condition
foreach($ar4 as $key=>$value){ // foreach loop structure
    if($value=='b') // finding value 'b' by variable $value;
        continue; // continue; will skip printing 'b'
    echo $key." : "; // Here will be echo $key
    echo $value.'<br/>'; // Here will be echo $value
}
}

$arrrr[1] = 10; // Array with user define key [1]
$arrrr[2] = 10; // Array with user define key [2]
$arrrr[5] = 10; // Array with user define key [5]
unset($arrrr[2]); // unset will not print key number [2]
print_r($arrrr); // print_r(); function will print array

?>

Comments

Popular posts from this blog

WP register_post_type() with custom CMB2 meta box

Git post receive setup at server for git push to the production from local machine