[PHP] Array


Deskripsi
      Cara membuat array, menambahkan/push array ke array, serta mengubah value dari array php.

Howto
//create array
$myarray = array("permission" => array(
                                               "Main" => "true"
                                    ),
                  "Grants" => array(
                                             "Local" => "true"
       )
            );

//Accessing Array
echo $myarray["permission"] // output = array, because another array still exist
echo $myarray["permission"]["Main"] // output = true , because there's only leaf

//Count Array
echo count($myarray); // output 2 (permission and grant, another array not include)

//Count Array Recursive
echo count($myarray,COUNT_RECURSIVE); // output 4 (permission,grant,main,local)

//Add content
array_push($myarray["Grants"],array('suicide'=>'Yes'));
result : array("permission" => array(
                                               "Main" => "true"
                                    ),
                  "Grants" => array(
                                             "Local" => "true"
       ),
                                                      array(
                                             "suicide" => "true"
       ),
     );

//Compac Function
$name = "mahendra";
$position = "IT";
$detail=array("name","position");
$result=compact($detail);

print_r($result);

here's the result:
Array ( [name] => mahendra [position] => IT )

if i comment variable $position, the result:
Array ( [name] => mahendra )

*because the value not linking to same name of variable.

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih