Reputation: 7091
Here is a sample 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 are built in function to PHP, or custom which can reorder this array to this (without being a big performance hit either):
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
)
)
Upvotes: 2
Views: 17828
Reputation: 3238
You can simply use the array_values
function, it's very easy to use and it's just one method/function.
// The array
$array = array(0 => "value", 3 => "value", 8 => "value");
// Reorder the array
$array = array_values($array);
Hope this helps!
Upvotes: 2
Reputation: 90999
This function should work, and this is as straightforward as it can get.
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 suggested 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 make the array index 0-based though. If you want 1-based indexing, then 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;
}
Upvotes: 10
Reputation: 1056
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 subarray index at 0 index use this...
foreach($inputarray as &$a)
$a = array_values($a);
PHP reference:
array_combine
range
array_values
Upvotes: 2
Reputation: 1599
My best shot, for the moment (quick and untested attempt)
$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 do you need to resort the arrays? Looping trough them with foreach ($array as $key=>$value) works fine in this context.
Upvotes: 0
Reputation: 19441
EDIT: I did not notice the array nesting at first, hence the following is not sufficient. As Imran already incorporated my suggestion, I will not edit this any further.
Check the array_values() function, it does almost exactly what you need.
The only difference to your desired output is that it reindexes starting with zero - if you really need the index to start at one, you can array_shift()
a dummy entry in first, and array_unshift()
it afterwards.
Upvotes: 8