2017-04-15 10 views
1

11個の配列を1つにまとめようとしています。 これは私のコードは、これまでどのように見えるかです:PHPが11個の配列を1つにマージする

$data

array(10) { 
[0]=> 
    array(2) { 
    ["VideoName"]=> 
    string(61) "TUTORIAL 1: How to Give an Awesome (PowerPoint) Presentation " 
    ["1min"]=> 
    string(2) "35" 
    } 
    [1]=> 
    array(2) { 
    ["VideoName"]=> 
    string(48) "TUTORIAL 2: How to open and close presentations?" 
    ["1min"]=> 
    string(2) "30" 
    } 
    [2]=> 
    array(2) { 
    ["VideoName"]=> 
    string(47) "TUTORIAL 3: Make a presentation like Steve Jobs" 
    ["1min"]=> 
    string(2) "12" 
    } 
    [3]=> 
    array(2) { 
    ["VideoName"]=> 
    string(56) "TUTORIAL 4: The five secrets of speaking with confidence" 
    ["1min"]=> 
    string(2) "20" 
    } 
    [4]=> 
    array(2) { 
    ["VideoName"]=> 
    string(93) "X.EXAMPLE 1 - Abraham Heifets: How can we make better medicines? Computer tools for chemistry" 
    ["1min"]=> 
    string(2) "24" 
    } 
    [5]=> 
    array(2) { 
    ["VideoName"]=> 
    string(70) "X.EXAMPLE 1 - Carol Dweck: The power of believing that you can improve" 
    ["1min"]=> 
    string(1) "2" 
    } 
    [6]=> 
    array(2) { 
    ["VideoName"]=> 
    string(66) "X.EXAMPLE 2 - Johanna Blakley: Social media and the end of gender " 
    ["1min"]=> 
    string(2) "15" 
    } 
    [7]=> 
    array(2) { 
    ["VideoName"]=> 
    string(56) "X.EXAMPLE 3 - Tim Berners-Lee: A Magna Carta for the web" 
    ["1min"]=> 
    string(2) "17" 
    } 
    [8]=> 
    array(2) { 
    ["VideoName"]=> 
    string(57) "X.EXAMPLE 4 - Eli Pariser: Beware online "filter bubbles"" 
    ["1min"]=> 
    string(1) "1" 
    } 
    [9]=> 
    array(2) { 
    ["VideoName"]=> 
    string(103) "X.EXAMPLE 4 - Jasdeep Saggar: Hypoxia-activated pro-drugs: a novel approach for breast cancer treatment" 
    ["1min"]=> 
    string(2) "21" 
    } 
} 

$data5

array(6) { 
    [0]=> 
    array(2) { 
    ["VideoName"]=> 
    string(48) "TUTORIAL 2: How to open and close presentations?" 
    ["6min"]=> 
    string(2) "24" 
    } 
    [1]=> 
    array(2) { 
    ["VideoName"]=> 
    string(47) "TUTORIAL 3: Make a presentation like Steve Jobs" 
    ["6min"]=> 
    string(2) "21" 
    } 
    [2]=> 
    array(2) { 
    ["VideoName"]=> 
    string(56) "TUTORIAL 4: The five secrets of speaking with confidence" 
    ["6min"]=> 
    string(2) "16" 
    } 
    [3]=> 
    array(2) { 
    ["VideoName"]=> 
    string(66) "X.EXAMPLE 2 - Johanna Blakley: Social media and the end of gender " 
    ["6min"]=> 
    string(2) "10" 
    } 
    [4]=> 
    array(2) { 
    ["VideoName"]=> 
    string(56) "X.EXAMPLE 3 - Tim Berners-Lee: A Magna Carta for the web" 
    ["6min"]=> 
    string(2) "11" 
    } 
    [5]=> 
    array(2) { 
    ["VideoName"]=> 
    string(57) "X.EXAMPLE 4 - Eli Pariser: Beware online "filter bubbles"" 
    ["6min"]=> 
    string(1) "1" 
    } 
} 

$data10

ここ
$combine = []; 
foreach($data as $key => $array){ 
    $combine[$key] = array_merge($array,$data1[$key],$data2[$key],$data3[$key],$data4[$key],$data5[$key],$data6[$key],$data7[$key],$data8[$key],$data9[$key],$data10[$key]); 
} 

である配列の一部がどのように見えますか私は、コードを実行すると

array(1) { 
    [0]=> 
    array(2) { 
    ["VideoName"]=> 
    string(70) "X.EXAMPLE 1 - Carol Dweck: The power of believing that you can improve" 
    ["11min"]=> 
    string(1) "2" 
    } 
} 
しかし、これは私はそれが最初のビデオのために完璧にすべてをマージし、それの後に、それはヌル表示 thisthis

を得るものです。私が間違っていることを教えてくれますか?ループのないあなたはこのような単純な方法でそれを行うことができます

+0

: '$ = array_merge($配列[0]、$データ[0]、$のDATA1を組み合わせ[0]、... 。 – splash58

+0

配列の文字要素が前者をオーバーライドしますか? –

+0

期待される出力も共有できますか? –

答えて

1

$combine = []; 

$datas = []; 
foreach(range(1, 10) as $index) 
{ 
    $variable = 'data' . $index; 
    $datas[] = $$variable; 
} 

$combine = $data; 
foreach($datas as $data){ 
    foreach($data as $key => $array; 
    { 
    foreach($array as $k => $v) 
    { 
     $combine[$key][$k] = $v; 
    } 
    } 
} 
+0

最初はうまくいきましたが、データを見てからデータが間違っていることがわかりました。私はそれをJSONに変換し、https://api.myjson.com/bins/yux9fで見ることができます。約4〜5本のビデオが欠落しており、そこにあるビデオのデータはすべて間違っています。問題がどこにあるのか? –

+0

何が間違っている、あなたは何を期待していますか?私たちは質問のコメントに尋ねましたが、あなたは何も答えませんでした。 –

関連する問題