Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,919 questions

51,852 answers

573 users

How to delete the middle element of an array in PHP

2 Answers

0 votes
function deleteMiddleElement(&$st, $size, $current) {
    if (empty($st) || $current == $size) {
        return;
    }
    
    $el = end($st);
        
    array_pop($st);
    
    deleteMiddleElement($st, $size, $current + 1);

    if ($current != (int)($size / 2)) {
        array_push($st, $el);
    }
}
        
$st = array();

array_push($st, '3');
array_push($st, '5');
array_push($st, '1');
array_push($st, 'm');
array_push($st, '9');
array_push($st, '2');
array_push($st, '7');
        
deleteMiddleElement($st, count($st), 0);

print_r($st);




/*
run:

Array
(
    [0] => 3
    [1] => 5
    [2] => 1
    [3] => 9
    [4] => 2
    [5] => 7
)

*/

 



answered May 27, 2023 by avibootz
0 votes
$st = array();

array_push($st, '3');
array_push($st, '5');
array_push($st, '1');
array_push($st, 'm');
array_push($st, '9');
array_push($st, '2');
array_push($st, '7');
        
unset($st[(int)(count($st) / 2)]); 

print_r($st);




/*
run:

Array
(
    [0] => 3
    [1] => 5
    [2] => 1
    [4] => 9
    [5] => 2
    [6] => 7
)

*/

 



answered May 27, 2023 by avibootz
...