Web coding support
  • Home
  • PHP
  • Phonegap
  • Angular Js
  • Node Js
  • Previous year questions
  • B.Tech Questions
  • RTU

How to reset an array keys in php

  • By admin
  • Dec-20-2016
  • Array, PHP
  • 0 Comments.

In this post we will discuss to reset an array after add or delete an index.

If there are an array $a

$a = array(
		0 => 3,
		1 => 5, 
		2 => 7, 
		3 => 9, 
		4 => 0
	);

Now we updates this array and delete index 2, so the new array is –

unset($a[2]);
$a = array(
		0 => 3, 
		1 => 5, 
		3 => 9, 
		4 => 0
	);

Index 2 has been removed from array and we have to re-arrange all indexes of array.
So its not a big task. Just a simple step and array will arrange form index 0 to 3.
Use the predefined array function array_values and the array indexes will be reset.

$a = array_values($a);
$a = array(
		0 => 3, 
		1 => 5, 
		2 => 9, 
		3 => 0
	);

This was a simple array. If we have a multi-dimensional array, Then !!.
Nothing to worry about multi-dimensional, PHP have all tricky solutions 🙂

$reset_array = array_map('array_values', $arr);

To reset multidimensional array use array_map.
Here $arr is a multidimensional array with different indexes and $rest_array is output after reset all indexes.

Hope this will help you.

Related Posts

  • Magic function library for Codeigniter like CakePHPMagic function library for Codeigniter like CakePHP
  • What is static functions in phpWhat is static functions in php
  • Login with facebook in core phpLogin with facebook in core php
  • Fetch random results form MySQLFetch random results form MySQL
  • How to handle duplicate data in MySqlHow to handle duplicate data in MySql
  • How to create pagination in PHP and MySQLHow to create pagination in PHP and MySQL

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Google+ (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to email this to a friend (Opens in new window)
  • Click to print (Opens in new window)

Related

FacebookTwitterGoogle+Google GmailPrintPinterestFacebook MessengerWordPressShare

Comments

Leave a Reply Cancel reply

← Previous Post Next Post →

Categories

  • Angular Js
  • Array
  • Codeigniter
  • facebook
  • Javascript
  • jquery
  • MySQL
  • oops
  • Pagination
  • Phonegap
  • PHP

Recent Posts

  • Fetch random results form MySQL
  • HTML DOM Manipulation using JavaScript
  • Magic function library for Codeigniter like CakePHP
  • What is the difference between an interface and abstract class
  • How to reset an array keys in php

Tags

Abstract Class angularjs array codeigniter facebook facebook login facebook sdk form validations function hide html Interface javascript jquery magic functions method mysql ng-hide ng-show nghide ngshow oops pagiation php recursion reference show static static functions

Subscribe us

Name
Email *
About This Site

We provide the simplest tutorials for web development.

Pages
  • Home
  • B.Tech Questions
  • Previous year questions
  • RTU
Categories
  • Angular Js
  • Array
  • Codeigniter
  • facebook
  • Javascript
  • jquery
  • MySQL
  • oops
  • Pagination
  • Phonegap
  • PHP
Search
  • facebook
  • twitter

Powered by Minmarks.com  |  A Coding blog by By Jitendra.

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.