PHP reindex array?
Use array_values
.
$myarray = array_values($myarray);
How do you reindex an array in PHP but with indexes starting from 1?
If you want to re-index starting to zero, simply do the following:
$iZero = array_values($arr);
If you need it to start at one, then use the following:
$iOne = array_combine(range(1, count($arr)), array_values($arr));
Here are the manual pages for the functions used:
array_values()
array_combine()
range()
How to reindex an array?
Use array_values:
$reindexed_array = array_values($old_array);
How to Remove Array Element and Then Re-Index Array?
unset($foo[0]); // remove item at index 0
$foo2 = array_values($foo); // 'reindex' array
Reindexing an array
If you don't mind using javascript 1.6: (note: this code uses the jQUery library)
var testArray = new Array();
testArray[3]="qwerty";
testArray[7]="asdfgh";
testArray[13]="zxcvbn";
var testString = testArray.filter(function (item) { return item != undefined }).join();
$(function(){
$('#write').text(testString);
});
filter prototype:
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}
reindex an array using value from multidimensional array
Yes there is a function for that array_column, which lets you extract a specific element from a multi-dimensional array and/or index the result by a specific element. So do both:
$result = array_column($array, 'qAVG', 'mID');
If the sub-arrays have more elements and you want them as well, then just re-index:
$result = array_column($array, null, 'mID');
How to filter out blank values from an array in PHP & Reindex the array
Replace your code from this line => $var = explode(',', $sizes);
& add this:
$var = array_filter(explode(',', $sizes));
$reindexed = array();
foreach ($var as $row)
{
if ($row !== null)
$reindexed[] = $row;
}
print_r($reindexed);
exit();
Let's See The Explaination of the code now
1.) This is 1st reference link from where i took the idea to filer but ass you prnt the array you will see that the array indexes are jumbled => Remove empty array elements
$var = array_filter(explode(',', $sizes));
$reindexed = array();
so we create a new variables reindexed
as an array to store the reindexed array value
2.) To remove the jumbled array index and reindex the array i took refernce from this link => How to remove null values from an array?
$reindexed = array();
foreach ($var as $row)
{
if ($row !== null)
$reindexed[] = $row;
}
print_r($reindexed);
reindexing array inside object after unsetting few keys in php
Use array_values()
to reindex an array.
$object->events = array_values($object->events);
Re-index numeric array keys
$your_new_array = array_values($your_old_array);
Related Topics
Implode an Array With ", " and Add "And " Before the Last Item
How to Show Multiple Recaptchas on a Single Page
How to Get the Content-type of a File in PHP
Replace a Whole Line Where a Particular Word Is Found in a Text File
Difference Between MySQL, MySQLi and Pdo
What Does the "[^][]" Regex Mean
Count How Many Files in Directory PHP
PHP Multiple Delimiters in Explode
Include PHP Inside JavaScript (.Js) Files
Creating PHP Class Instance With a String
PHP Mail() Function on Localhost
How to Show Ajax Requests in Url
Difference Between JavaScript and PHP