sona
sona

Reputation: 96

how to remove multiple elements by index from a multidimensional array?

I have a multi dimension array like this

 $state=array(array('state'=>t,'capital=>'y'),array('state'=>'p','capital'=>'q'),array('state'=>,'l','capital'=>'s'),array('state'=>'g','capital=>'h'));

I need to form a sub array by removing one or more indexes in random order from this array lets say $state[1],$state[3] output is

  $new_state=array(array('state'=>t,'capital=>'y'),array('state'=>'g','capital=>'h'));

is there any direct function to achieve this?

Upvotes: 1

Views: 1512

Answers (1)

Razib Al Mamun
Razib Al Mamun

Reputation: 2711

Your first array 3 element index like 0,1,2. Used unset() for delete array element by array indexing like unset($state[1]) or more... Now array index is 0, 2. it is un serialize. For retrieve correct indexing serial used array_values(). Now final array index is 0,1

<?php                  
$state=array(array('state'=>'t','capital'=>'y'),array('state'=>'p','capital'=>'q'),array('state'=>'g','capital'=>'h'));

unset($state[1]);
$new_array = array_values($state);;
print_r($new_array);
?>

Upvotes: 1

Related Questions