Wednesday, 18 October 2017

SPACESHIP OPERATOR IN PHP

🚀 SPACESHIP OPERATOR Tun tumah hian PHP 7-a feature thar Spaceship Operator an tih <=> hman tangkai dan tur ka hriat ve chhun han share che u ka duh a. Online taima deuh tan chuan a use case hi a awm nual mai thei.
PHP chungchang kan sawi dawn leh project khawih lai pawha ka rawn tam ber thin PHP.NET documentation-in a sawi dan leh ziak dan hi Bible ang deuha ka rin ber a ni chung hian hriat thiam loh ka ngah hle. Tuna Spaceship operator kan sawi tur pawh hi hetiang hian a sawi thla then mai a. $a <=> $bAn integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.”
English
thiam ang tawkin a ngial a nganin (Literally) han letling ta ila: “$a hi $b aia te zawk emaw, lian zawk emaw, $b tluk chiah a nih hian Integer (Nambar thensawm loh) bial tluk emaw, bial aia lian emaw, bial aia te zawk emaw a ni. PHP7 chinah hman theih a ni.” A awmzia ber chu dan pathum: intluk, lian zawk, te zawk hmanga comparison siamna a ni.
💡
EXAMPLE PHP.NET-in example a tarlan tlema zawng hi in belchiang leh dawn teh ang: <?php // Integers Compare echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // Floats Compare echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // Strings Compare echo "a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1 echo "a" <=> "aa"; // -1 echo "zz" <=> "aa"; // 1 // Arrays Compare echo [] <=> []; // 0 echo [1, 2, 3] <=> [1, 2, 3]; // 0 echo [1, 2, 3] <=> []; // 1 echo [1, 2, 3] <=> [1, 2, 1]; // 1 echo [1, 2, 3] <=> [1, 2, 4]; // -1 // Objects Compare $a = (object) ["a" => "b"]; $b = (object) ["a" => "b"]; echo $a <=> $b; // 0 $a = (object) ["a" => "b"]; $b = (object) ["a" => "c"]; echo $a <=> $b; // -1 $a = (object) ["a" => "c"]; $b = (object) ["a" => "b"]; echo $a <=> $b; // 1 // only values are compared $a = (object) ["a" => "b"]; $b = (object) ["b" => "b"]; echo $a <=> $b; // 1
PHP.NET example atanga kan hmuh dan leh a definitiona kan sawi tawh angin Spaceship Operator hian thil chi pathum Zero(0), Negative (-1) leh Positive (1) min chhawpchhuahsak a ni. Tichuan Kan variable compare chu Object, Array, Integer, Float eng pawh lo ni se, a result tur chu 0 or -1 or 1 bak a ni dawn lo a ni.
☑️
USE CASE
$names = []; //
File kan sort duh avangin blank array kan siam $file = new SplFileObject('contact.csv');//File suasamna //$file->setFlags(SplFileObject::READ_CSV); $file->getCurrentLine();//Column header paihna while(!$file->eof()){ //File tawpna a nih loh chuan $line = $file->fgetcsv();//CVS file chhiar nan if(!is_null($line[0])){//Blank line a nih loh chuan $names[]= $line;//CSV file line tin chu array-ah kan dahtha } } usort($names, function($a, $b){//sorting chak tak a ni return [$a[1], $a[0]]<=>[$b[1], $b[0]]; //Vei lam value hi a dinglam aia a tet chuan = -1 //Vei lam leh dinglam value hi a in ang chuan = 0 //Vei lam value hi a len zawk chuan = 1 });
🗨️ usort hi chu sorting ril zet a ni a, experience coder tan lo chuan hriatthiam a har nak mai. function kan siamchawp hmanga array sort-na a ni. A sort dan kalphung ber chu lian zawk, te zawk, in ang tih hi a ni. Ascending order hi a sort chhuah dan ber chu a ni a. CSV-a kan file chu kan print chhuak ang.
//foreach ($names as $name){ // echo implode(' ', $name)."\n"; //} echo hlConsole($names); //hlConsole()hmangin table-ah result kan display ang. +---------+-----------+ | Avana | Aizawl | | Muana | Aizawl | | Rama | Champhai | | Duhawma | Kolasib | | Zara | Kolasib | | Bma | Lunglei | | Zova | Seling | | Chama | Serchhip | | Kima | Sialsuk | | Hruaia | Thenzawl | | Faka | Zawlnuam | +---------+-----------+
PHP hian built-in sorting function descending leh ascending-a sort theihna bakah thil leh leh te thliar zung zungna entirnan min(), max() function te a nei a, array-ah minimum value or maximum value pakhat aia tam lakchhuah kan duh chuan chuan array_slice hmangin kan sort ang a, ascending-in kan sort chhuak tihna a ni ang, descending kan duh array_reverse kan hmang leh thei ang. Hengte hi an tangkai thei viau ang. Mahse Array hnuaia subarray or object compare a lo ngaih chuan hengte hi kan hmang thei tawh lo. Hetiang compare nan tak hianusort(), uasort(), leh uksort() te a lo awm a ni. Array subelement te hi array values or object properties te an ni thei a, chutiang compare nan chuan usort hi kan hmang thei a ni.
function sortAsc($a, $b){ return ($a->published< $b->published) ? -1 : 1; //array of object->property `
published` chu $a, $b dah-in a lian zawk te zawk kan khaikhin a, $a a tet a piangin -1 kan return a, a nih loh chuan 1 kan return thung }
Space lamah lo let leh tawh ila. Kan use case example-a mi hi spaceship tel miah lovin dah dawn ta ila:
usort($names, function($a, $b){ //return [$a[1], $a[0]]<=>[$b[1], $b[0]]; return ([$a[1],$a[0]]<[$b[1], $b[0]]) ? -1:(([$a[1],$a[0]]>[$b[1], $b[0]]) ? 1:0);
});
tia kan ziah duah a ngai dawn a, hnawksak tham viau a ni. Ti zawng kha ni mai teh se.