note 109864 deleted from function.array-reverse by salathe

From: Date: Mon, 21 Sep 2020 14:51:54 +0000
Subject: note 109864 deleted from function.array-reverse by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215360@lists.php.net to get a copy of this message
Note Submitter: atulmvAThotmailDOTcoDOTuk ---- Here are a couple of routines to swap the order of two elements in an array <?php function array_swap_forward($arr,$elem) { $ndx = array_search($elem,$arr) - 1; $b4 = array_slice($arr,0,$ndx); $mid = array_reverse(array_slice($arr,$ndx,2)); $after = array_slice($arr,$ndx + 2); return array_merge($b4,$mid,$after); } function array_swap_back($arr,$elem) { $ndx = array_search($elem,$arr); $b4 = array_slice($arr,0,$ndx); $mid = array_reverse(array_slice($arr,$ndx,2)); $after = array_slice($arr,$ndx + 2); return array_merge($b4,$mid,$after); } $arr =array('a','b','c','d','e','f'); print_r(array_swap_forward($arr,'c')); echo '<br>'; print_r(array_swap_back($arr,'c')); ?>

« previous php.notes (#215360) next »