Array awmzia
Php code-a thil pakhat emaw a aia tam dahna (variable) a ni. Variable kan tih ber hi nakin danglam thei eng data pawh chhinchhiahna/dahthatna a ni. Entirnan PHP-ah chuan variable siam nan $ symbol kan hmang a,$word = "Thu";
Hetah hian $word hi variable a ni a, a hlutna or data ken chu 'Thu' a ni. Nakinah chuan $word hian 'Thu' ni kher lo pawh kan dahthar thei a. Tichuan $word chu a saptawnga a awmzia ang deuh hian danglam thei reng a ni. Mahse variable hi chuan vawikhatah thil pakhat chiah(data type) a vawng thei a, array kan tih hi chuan thil chi hrang hrang, pakhat aia tam pawh a vawng thei a ni. Data-Type hi String, Integer, Boolean, Array, Object, Null, Float etc. te a awm thei a, hengte hi variable chuan vawi khatah chi khat chauh zel a keng thei a, Array erawh thung chuan chi khat lek emaw a aia tam, chi hrang hrang in pawlh pawhin a keng thei a ni.
PHP language-a thiam taka coding thei tur chuan array laimu leh hman dan kan zei hle a ngai a, stackoverflow-a PHP-a zawhna tam ber pawh hi array kaihhnawih deuh hlir an ni. Tun tumah hian PHP-in array-a hnathawhna tur min chhawpchhuahsak sa (builtin function) hrang hrang Duhlian tawngin kan sawifiah dawn a ni.
PHP Array siam dan
PHP array siam nan hian array keyword leh [ ] hman ve ve theih a ni.Example 1:
$mizo[] = "Sailo";Example 2:
$mizo[] = "Pachuau";
$mizo[0] = "Sailo";Example 3:
$mizo[1] = "Pachuau";
$mizo = array("Sailo", "Pachuau");Example 4:
$mizo = ["Sailo", "Pachuau"];
A chunga Example 1-4 hi array kan siam hi a in ang vek a, a siam dan a hrang mai chauh a ni. A hnuaia mi ang hian print_r($mizo) kan run chuan a rawn lang dawn a ni:
Array
(
[0] => Sailo
[1] => Pachuau
)
Array hi Key leh Value nei turin a siam theih a, kan example ah hi chuan a value ang chauh kan dah a, a key hi php-in a rawn siam chawp a, chu chu zero(0) atangin a in tan zel thin a ni. key hi a value kengtu chhinchhiahna a ni a, kan peksa loh chuan number zero atangin a in tan thin a, a key kan dahtel erawh chuan kan dahtel ang khan a hmang zawk a ni. Key hi String data type emaw, Integer data Type a ni thei. Example-ah hian key kan dah lo va, kan output kan en chuan 0 leh 1 kan hmuh tho hi. Example 2 ah hi chuan key hi kan dahtel a. Example 3 leh Example 4-ah hian kan dah lo ve ve a, mahse output kan hmuh ang hian a rawn print chhuak tho a ni. Hetianga key atana number a awm hian Numerical Indexed (Numbera chhinchhiahna) Arrays an ti a, number ni lo kan hman chuan Associative Arrays tiin an sawi thung.
Associative Arrays siam Dan
Array hi Database ang deuhva data kentir kan duh chuan a key tan string emaw kan duh ang datatype kan kentir thei a ni.
Example 5:
Output:$student=["Name"=>"Zorama","Age"=>14,"Address"=>"Tuikual"];print_r($student);
Array$student Output kan hmuh ang hian [ ] chhunga awm zawng hi a key a ni a, dinglama Zorama, 14 leh Tuikual hi a value a ni. A key hi database-a field name angah ngai ta ila, a value te hi row angah ngaih tur chu a ni ang.
(
[Name] => Zorama
[Age] => 14
[Address] => Tuikual
)
A chunga array hi array chhungah kan dah leh chuan Multidimensional Array tiin an sawi a, chu chu entirna nen lo en leh ila:
Example 6:
$student=[php 5.4 hnuai lamah chuan a hnuaia mi ang hian ziah tur a ni thung:
["Name"=>"Zorama","Age"=>14,"Address"=>"Tuikual"],
["Name"=>"Pari","Age"=>12,"Address"=>"Zotlang"],
["Name"=>"Rama","Age"=>15,"Address"=>"Ramthar"]
];
print_r($student);
$student=array(Output:
array("Name"=>"Zorama","Age"=>14,"Address"=>"Tuikual"),
array("Name"=>"Pari","Age"=>12,"Address"=>"Zotlang"),
array("Name"=>"Rama","Age"=>15,"Address"=>"Ramthar")
);
print_r($student);
ArrayA chunga kan hmuh ang hian array chhungah key leh value tan array kan hmang leh zel thei a ni. A dang kan sawi belh leh hmain array chhunga element (data) te lakchhuah (get) or print chhuah dan kan sawi leh lawk ang. Example 4-a mi Sailo tih hi lo print dawn ta ila, a hnuaia mi ang hian kan print thei ang:
(
[0] => Array
(
[Name] => Zorama
[Age] => 14
[Address] => Tuikual
)
[1] => Array
(
[Name] => Pari
[Age] => 12
[Address] => Zotlang
)
[2] => Array
(
[Name] => Rama
[Age] => 15
[Address] => Ramthar
)
)
Example 7:
echo $mizo[0];Sailo leh pachuau hi print ve ve kan duh chuan a key atangin vekin kan print leh thei a;
echo $mizo[0].'<br> '.$mizo[1];Duh chuan foreach loop hmangin:
foreach($mizo as $hnam):for loop hmanga kan print duh chuan:
echo $hnam.'<br>';
endforeach;
for($i=0; $i<count($mizo); $i++):list() hmangin a print chhuah theih bawk a:
echo $mizo[$i].'<br>';
endfor;
list($sailo, $pachuau) = $mizo;Heng bakah hian extract() hmangin Associative Array te chu lakchhuah theih an ni leh a, Numerical Indexed Arrays chu a theih ve loh a ni. Entirnan Example 5-a mi hi a hnuaia mi ang hian la chhuak thei a ni.
echo $sailo.'<br>'.$pachuau;
Example 8:
extract($student);Kan hmuh ang hian kan key te kha print chhuahna variable a tan kan hmang a ni. Multidimensional array te chu nested foreach loop hmangin eng anga inthuah nasa pawh print chhuah vek theih a ni. Entirnan example 6-a mi hi kan print duh chuan a hnuaia mi ang hian:
echo $Name.' '.$Age.' '.$Address;
foreach($students as $key=>$field){
foreach($field as $row){
echo $row."\n";
}
}
PHP Array Built-in Function:
1. print_r()/var_dump(): Hei hi array i suasam tur a chhung awm dan en nan hman a ni. Kan exampleah hian kan hman bawk hi.print_r($mizo);//Sailo & Pachuau2. count(): Array chhunga data awmzat chhiar nan kan hmang a ni.
var_dump($mizo);//Sailo & Pachuau
count($mizo)//23. explode(): Hei hi String pangai arraya dah nan kan hmang
$hnam = "Sailo, Pachuau";4. implode(): Hei hi a chunga mi opposite chiah hi a ni a, array pangai stringa dahna hman a ni.
$mizo = explode(",",$hnam);
print_r($mizo);
$hnam = implode(",",$mizo);5. array_flip(): Array key leh Value awmna thlak thleng nan:
echo $hnam;//Sailo, Pachuau
$student=array("Name"=>"Zorama","Age"=>14,"Address"=>"Tuikual");print_r(array_flip($student));6. array_push(): Array kan neihsa tawpa data dang dah belh nan:
Output:
Array
(
[Zorama] => Name
[14] => Age
[Tuikual] => Address
)
$cms = ["Ch Chhunga","Brigadier","Hawla"];
print_r($cms);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
)
array_push($cms,"Zorama");
print_r($cms);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
[3] => Zorama
)
7. array_shift(): Array data hmasa ber paihna:
$cms = ["Ch Chhunga","Brigadier","Hawla"];8. array_unshift(): Array-a data hmasa ber tur dah chawpna, array_push opposite chiah kha ni mai:
print_r($cms);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
)
array_shift($cms);
print_r($cms);
Array
(
[0] => Hawla
[1] => Zorama
)
$cms = ["Ch Chhunga","Brigadier","Hawla"];9. array_pop(): Array-a data hnuhnung ber paihna, array_shift opposite chiah kha ni mai:
print_r($cms);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
)
array_unshift($cms,"Zorama");
print_r($cms);
Array
(
[0] => Zorama
[1] => Ch Chhunga
[2] => Brigadier
[3] => Hawla
)
$cms = ["Ch Chhunga","Brigadier","Hawla"];10. array_splice(): Array-a data awmsa a lai hawl vela mi paih leh a lai hawl vela data dahbelhna:
print_r($cms);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
)
array_pop($cms);
print_r($cms);
Array
(
[0] => Ch Chhunga [1] => Brigadier
)
$cms = ["Ch Chhunga","Brigadier","Hawla", "Zorama"];Zero index atanga chhiarin 1-na chu Brigadier a ni a, data pahnih kan paih a ni. Data dahbelh kan duh chuan:
print_r($cms);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
[3] => Zorama)
array_splice($cms, 1,2);//1=index1, 2=paih tur zat
print_r($cms);
Array
(
[0] => Ch Chhunga [1] => Zorama
)
Zorama leh Hawla inkarah Lalduhawma dahbelh dawn ta ila
$cms = ["Ch Chhunga","Brigadier","Hawla","Zorama"];
print_r($cms);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
[3] => Zorama
)
array_splice($cms, 3, 0, array("Lalduhawma"));//Duhawma hi a 3-na tur tihna
print_r($cms);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
[3] => Lalduhawma
[4] => Zorama)
11. array_slice(): Array-a data awmsa a lai hawl vela mi lakchhuahna a ni ber a, array_splice nen an in an lohna chu kan original array a tidanglam ve lo a ni. Brigadeir leh Hawla hi lo lachhuak dawn ta ila:
$cms = ["Ch Chhunga","Brigadier","Hawla","Zorama"];12. array_unique(): Array-a Duplicate data paihna:
print_r($cms);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
[3] => Zorama
)
$thar = array_slice($cms, 1, 2);//Chhiar tanna 1 hi Brigedier, lak zat tur hi 2
print_r($thar);
Array
(
[0] => Brigadier
[1] => Hawla
)
$cms = ["Ch Chhunga","Brigadier","Hawla","Zorama", "Hawla",];13. array_values(): Associative array value lakchhuahna a ni ber a, numerical index a siam nghal bawk.
print_r($cms);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
[3] => Zorama
[4] => Hawla
)
$thar = array_unique($cms);//Hawla hi 2 a awm a, pakhat kan paih
print_r($thar);
Array
(
[0] => Ch Chhunga
[1] => Brigadier
[2] => Hawla
[3] => Zorama
)
$student=["Name"=>"Zorama","Age"=>14,"Address"=>"Tuikual"];
print_r($student);
Array
(
[Name] => Zorama
[Age] => 14
[Address] => Tuikual
)
$thar = array_values($student);
print_r($thar);
Array
(
[0] => Zorama
[1] => 14
[2] => Tuikual
)
14. array_keys(): Associative array key lakchhuahna a ni ber a, numerical index a siam nghal bawk.
$student=["Name"=>"Zorama","Age"=>14,"Address"=>"Tuikual"];
print_r($student);
Array
(
[Name] => Zorama
[Age] => 14
[Address] => Tuikual
)
$thar = array_keys($student);
print_r($thar);
Array
(
[0] => Name
[1] => Age
[2] => Address
)
Array Built-in function tam tak a awm a, hetah hian kan sawi vek seng rih lo a ni ber a. A duh tan chuan hetah hian click mai tur.
Array-a thil zawn:
He topic hi pawimawh tak nia ka hriat vangin ka telh leh hram nge. Array thil zawn hi array nihphung hrefiah vak lo tan chuan a har hle a ni, chuvangin hnathawhna ti awlsam tur lam chi ang zawng deuhvin tlem kan tarlang dawn a ni.
Key zawn nan:
array_key_exists();
Example 5-a mi ang associative array-a key zawn duh kan neih chuan
echo
array_key_exists
(
"Age"
,
$student
) ?
"exists"
:
"not exist"
;
Value zawn nan:
in_array();
Example 5-a mi ang associative array-a value zawn duh kan neih chuan
echo
in_array(
"Zorama"
,
$student
) ?
"exists"
:
"not exist"
;
Multidimensional array key i zawn duh chuan custom function a ngai a, hetiang hian:
$arr = array(
'21' => array(),
'24' => array(
'22' => array(),
'25' => array(
'26' => 'exists'
)
)
);
var_dump(multiKeyExists($arr, 22));
var_dump(multiKeyExists($arr, 23));
var_dump(multiKeyExists($arr, 26));
function multiKeyExists(array $arr, $key) {
// is in base array?
if (array_key_exists($key, $arr)) {
return true;
}
// check arrays contained in this array
foreach ($arr as $element) {
if (is_array($element)) {
if (multiKeyExists($element, $key)) {
return true;
}
}
}
return false;
}
Multidimensional array-a value i zawng duh a nih chuan php in_array hmang ringawtin a zawn theih loh va, mahni custom function siam ve a ngai tawh a ni. A hnuai mi hi en ila:
function in_arraym($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle)
|| (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; }
$b = array(array("Mac", "NT"), array("Irix", "Linux")); echo in_arraym("Irix", $b) ? 'found' : 'not found';
Note: Chawl rih phawt ang: