How to reindex or remove an index array element in PHP ?

I am explaining you, how to reindex an array element in PHP with examples.

we have an array named basicWebsiteInfo[] , we have some website information like website, url. At first, it is indexed correctly as 0,1,2.

<?php
   $basicWebsiteInfo = [
      '0' => [
          'website' => 'webdeveloperindia',
          'url' => 'https://webdeveloperindia.in'
      ],

      '1' => [
          'website' => 'markdevelopments',
          'url' => 'http://markdevelopments.com'
      ],

      '2' => [
          'website' => 'pakizacity',
          'url' => 'http://pakizacity.com'
      ],
   ];

   print_r($basicWebsiteInfo);
?>

Result:

Array ( [0] => Array ( [website] => webdeveloperindia [url] => https://webdeveloperindia.in ) [1] => Array ( [website] => markdevelopments [url] => http://markdevelopments.com ) [2] => Array ( [website] => pakizacity [url] => http://pakizacity.com ) )

Now, to understand the reindex array things, at first I am unset it by using the unset() function. The unset() function resets any variable. we want to remove index 1 which is the markdevelopments.

<?php
  unset($basicWebsiteInfo[1]);
  print_r($basicWebsiteInfo);
  ?>

Result:

Array ( [0] => Array ( [website] => webdeveloperindia [url] => https://webdeveloperindia.in ) [2] => Array ( [website] => pakizacity [url] => http://pakizacity.com ) )

Leave a Reply

Your email address will not be published. Required fields are marked *

51 − 47 =