PHP function to reorder an array
Here is an example of an array of all external JS files from paypal.com:
Array
(
[src] => Array
(
[1] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/lib/min/global.js
[2] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/tns/mid.js
[8] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/opinionlab/oo_engine.js
[11] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/iconix.js
[12] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/pageBlockingUnsafeBrowsers.js
[13] => https://www.paypalobjects.com/js/tns/min/bid.js
[15] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/pp_naturalsearch.js
[17] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/site_catalyst/pp_jscode_080706.js
)
[size] => Array
(
[1] => 0.273
[2] => 0.266
[8] => 0.279
[11] => 0.265
[12] => 0.285
[13] => 0.248
[15] => 0.275
[17] => 0.289
)
)
Is there a built-in function for PHP or a user interface that can change the order of this array (without much performance):
Array
(
[src] => Array
(
[1] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/lib/min/global.js
[2] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/tns/mid.js
[3] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/opinionlab/oo_engine.js
[4] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/iconix.js
[5] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/pageBlockingUnsafeBrowsers.js
[6] => https://www.paypalobjects.com/js/tns/min/bid.js
[7] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/pp_naturalsearch.js
[8] => https://www.paypalobjects.com/WEBSCR-590-20090814-1/js/site_catalyst/pp_jscode_080706.js
)
[size] => Array
(
[1] => 0.273
[2] => 0.266
[3] => 0.279
[4] => 0.265
[5] => 0.285
[6] => 0.248
[7] => 0.275
[8] => 0.289
)
)
source to share
This feature should work, and it's as simple as it gets.
function reindex_array($src) {
$dest = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
foreach ($value as $dest_val) {
$dest[$key][] = $dest_val;
}
}
}
return $dest;
}
Using array_values ββ() as stated in Henrik's answer
function reindex_array($src) {
$dest = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$dest[$key] = array_values($value);
}
}
return $dest;
}
This will cause the array index to be 0. If you want to index 1 based on, use this:
function reindex_array($src) {
$dest = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$count = 1;
foreach ($value as $dest_val) {
$dest[$key][$count] = $dest_val;
$count++;
}
}
}
return $dest;
}
source to share
EDIT: I didn't notice that the array is nested at first, so not enough. Since Imran has already included my proposal, I will not edit it further.
Check out the array_values ββ() function , it does almost what you want.
The only difference to your desired result is that it reindexes starting at zero - if you really want an index starting at one, you can array_shift()
dummy entry first and then array_unshift()
.
source to share
if you really want each subarray to start at index 1, use this:
foreach($inputarray as &$a)
$a = array_combine(range(1,count($a)),$a);
to start each submachine index with index 0 use this ...
foreach($inputarray as &$a)
$a = array_values($a);
PHP reference:
array_combine
range
array_values
source to share
My best shot so far (quick and unverified try)
$i = 1;
foreach($src as $key=>$item) {
if ($i != $key) {
$src[$i] = $src[$key];
$size[$i] = $size[$key];
unset($src[$key]];
unset($size[$key]];
}
$i++;
}
But why would you use arrays? Running foreach ($ array as $ key => $ value) through them works great in this context.
source to share