一个assosiate数组

时间:2017-06-15 08:26:06

标签: php arrays multidimensional-array

这是我的数组

Array
(
    [0] => Array
        (
            [0] => Music One
            [1] => Two
        )

    [1] => Array
        (
            [0] =>  C:\fakepath\Shape of You - Ed Sheeran (DJJOhAL.Com).mp3
            [1] => C:\fakepath\I m The One Ft Justin Bieber Quavo Chance The Rapper  Lil Wayne - DJ Khaled (DJJOhAL.Com).mp3
            [2] => 
        )

)

我想要这样

Array
(
    [0] => Array
        (
            [releasetrack_track_title] => Music One
            [releasetrack_mp3_demo] =>  C:\fakepath\Shape of You - Ed Sheeran (DJJOhAL.Com).mp3sample-DJ026-2.mp3

        )

    [1] => Array
        (
            [releasetrack_track_title] => Two
            [releasetrack_mp3_demo] => C:\fakepath\I m The One Ft Justin Bieber Quavo Chance The Rapper  Lil Wayne - DJ Khaled (DJJOhAL.Com).mp           
        )  

)

我怎么可能还使用了函数array_merge_recursive,但我没有得到我想要的输出。

有人知道吗,请分享我的代码。

5 个答案:

答案 0 :(得分:2)

您需要特定的自定义来管理您的阵列,我遇到了与自定义相同的问题,请尝试下面提到的代码,挑衅它会有帮助,

<?php
$yourArr; //Your Requested array
$outputArr = [];

foreach ($yourArr as $i => $val) 
{   
    foreach ($val as $j => $con) 
    {
        if($j == 0) { $outputArr[$i]['releasetrack_track_title'] = $con; }

        if($j == 1) { $outputArr[$i]['releasetrack_mp3_demo'] = $con; }
    }
}

echo "<pre>"; print_r($outputArr); die();

&GT;

答案 1 :(得分:2)

您可以尝试这样做,live demo

<resources>
    <string name="a">A value</string>
    <string name="b">Another value</string>
</resources>

答案 2 :(得分:1)

除了循环和新数组之外别无他法:

$newArray = [];

foreach ($oldArray as $index => $items) {
    switch ($index) {
        case 0:
            $key = 'releasetrack_track_title';
            break;
        case 1:
            $key = 'releasetrack_mp3_demo';
            break;
    }

    foreach ($items as $subIndex => $value) {
        $newArray[$subIndex][$key] = $value;
    }
}

答案 3 :(得分:0)

在数组[0]上使用foreach函数,即标题数组。以下是代码:

result = [x for values in G2.values() for x in values]

答案 4 :(得分:-1)

<?php
$arr = array(array(" Music One","C:\fakepath\Shape of You - Ed Sheeran (DJJOhAL.Com).mp3sample-DJ026-2.mp3"),array("Two", "C:\fakepath\I m The One Ft Justin Bieber Quavo Chance The Rapper  Lil Wayne - DJ Khaled (DJJOhAL.Com).mp"));
echo'<pre>';
print_r($arr);
?>