Reputation: 3
I am having and issue to rearrange a array, i try most of them.. try to use array_merge, foreach, forloop, while loop etc. but no luck
My concept is not clear i need your help to get an idea how I can change array as per my requirements, Its batter if you give me the concept idea only, i don't required code i try my self first to code it.
Here is the array
Array
(
[links] => Array
(
[song_name] => Array
(
[0] => AA
[1] => BB
[2] => CC
.....
)
[singer_name] => Array
(
[0] => AA
[1] => BB
[2] => CC
.....
)
[song_url_320] => Array
(
[0] => AA
[1] => BB
[2] => CC
.....
)
[song_url_128] => Array
(
[0] => AA
[1] => BB
[2] => CC
.....
)
)
)
I Need to change array like this:
Array
(
[links] => Array
(
[0] => Array
(
[song_name] => AA
[singer_name] => AA
[song_url_320] => AA
[song_url_128] => AA
.....
)
[1] => Array
(
[song_name] => BB
[singer_name] => BB
[song_url_320] => BB
[song_url_128] => BB
.....
)
[2] => Array
(
[song_name] => CC
[singer_name] => CC
[song_url_320] => CC
[song_url_128] => CC
.....
)
)
)
Upvotes: 0
Views: 50
Reputation: 1492
If your array is in a variable $original_array then this would do:
$new_array = array('links'=>array());
foreach ($original_array['links'] as $key => $data) foreach ($data as $n => $value) $new_array['links'][$n][$key] = $value;
Upvotes: 0
Reputation: 723
Create yourself an empty array. Then do a foreach loop for each array in the links array using the index number as the key for the new array.
A little more detail, without showing code. Do a foreach loop in the links grabbing the alpha index of the array inside links. Then do a foreach loop on that array inside of links grabbing the numeric index and value of each item. Then add that in a new links array using the numeric and then alpha key and assigning it the value.
I hope that makes sense to you. If it doesnt, let me know and I will do some sample code for you.
Upvotes: 0
Reputation: 24156
Idea: loop through all items and combine them into another array
Here is sample code:
$inputarray = array(...);
$outputarray = array('links' => array());
foreach ($inputarray['links'] as $k => $v)
foreach ($v as $k2 => $v2) {
$outputarray['links'][$k2][$k] = $v2;
}
Upvotes: 1
Reputation: 4370
use for loop like this
foreach($arr['links'] as $k =>$v)
{
foreach ($v as $k1 => $v1) {
$t[$k1][$k] = $v1;
}
}
print_r($t);
output
Array
(
[0] => Array
(
[song_name] => AA
[singer_name] => AA
[song_url_320] => AA
[song_url_128] => AA
)
[1] => Array
(
[song_name] => BB
[singer_name] => BB
[song_url_320] => BB
[song_url_128] => BB
)
[2] => Array
(
[song_name] => CC
[singer_name] => CC
[song_url_320] => CC
[song_url_128] => CC
)
)
Upvotes: 0