填充二维关联数组

时间:2013-12-23 15:32:33

标签: php arrays

请帮我解决以下问题:

表单标题的数组

$ headers = array ('0 '=>' id ', '1' => 'Name', '2 '=>' Balance ', '3' => 'Date', '4 '=>' Region ', '5 '=>' Segment ', '6' => 'Manager', '7 '=>' Contract ');

和数组数据类型(当然,实际上,这些数据不是记录等)

 $ content = array (
array ('0 '=>' id1 ', '1' => 'Name1', '2 '=>' balance1 ', '3' => 'date1', '4 '=>' region1 ', '5' > 'segment1', '6 '=>' manager1 ', '7' => ' sontract1 ')
array ('0 '=>' id2 ', '1' => 'Name2', '2 '=>' balance2 ', '3' => 'date2', '4 '=>' region2 ', '5' > 'segment2', '6 '=>' manager2 ', '7' => ' sontract2 ')
array ('0 '=>' id3 ', '1' => 'Name3', '2 '=>' balance3 ', '3' => 'date1', '4 '=>' region3 ', '5' > 'segment3', '6 '=>' manager3 ', '7' => ' sontract3 ')
array ('0 '=>' id4 ', '1' => 'Name4', '2 '=>' balance4 ', '3' => 'date4', '4 '=>' region4 ', '5' > 'segment4', '6 '=>' manager4 ', '7' => ' sontract4 '));

如何将它们从阵列形式中取出

$ data = array (
'id' => array ('0 '=>' id1 ', '1' => 'id2', '2 '=>' id3 ', '3' => 'id4'),
'Name' => array ('0 '=>' name1 ', '1' =>'' name2 ', '2' =>'' name3 ', '3' =>'' name4 '),
'Balance' => array ('0 '=>' balance1 ', '1' => 'balance2', '2 '=>' balance3 ', '3' => 'balance4'),
'Date' => array ('0 '=>' date1 ', '1' => 'date2', '2 '=>' date3 ', '3' => 'date4'),
 'Region' => array ('0 '=>' region1 ', '1' => 'region2', '2 '=>' region3 ', '3' => 'region4'),
 'Segment' => array ('0 '=>' segment1 ', '1' => 'segment2', '2 '=>' segment3 ', '3' => 'segment4'),
'Manager' => array ('0 '=>' manager1 ', '1' => 'manager2', '2 '=>' manager3 ', '3' => 'manager4'),
'Contract' => array ('0 '=>' contract1 ', '1' => 'contract2', '2 '=>' contract3 ', '3' => 'contract4'));

我知道你想使用for循环,foreach,但不知道如何填充数组,因为我需要。求救!

2 个答案:

答案 0 :(得分:0)

试试这个,查看演示here

<?php
    $headers = array ('0 '=>' id ', '1' => 'Name', '2 '=>' Balance ', '3' => 'Date', '4 '=>' Region ', '5 '=>' Segment ', '6' => 'Manager', '7 '=>' Contract ');

     $content = array (
    array ('0 '=>' id1 ', '1' => 'Name1', '2 '=>' balance1 ', '3' => 'date1', '4 '=>' region1 ', '5' > 'segment1', '6 '=>' manager1 ', '7' => ' sontract1 '),
    array ('0 '=>' id2 ', '1' => 'Name2', '2 '=>' balance2 ', '3' => 'date2', '4 '=>' region2 ', '5' > 'segment2', '6 '=>' manager2 ', '7' => ' sontract2 '),
    array ('0 '=>' id3 ', '1' => 'Name3', '2 '=>' balance3 ', '3' => 'date1', '4 '=>' region3 ', '5' > 'segment3', '6 '=>' manager3 ', '7' => ' sontract3 '),
    array ('0 '=>' id4 ', '1' => 'Name4', '2 '=>' balance4 ', '3' => 'date4', '4 '=>' region4 ', '5' > 'segment4', '6 '=>' manager4 ', '7' => ' sontract4 '));

    foreach($headers as $k=>$v){

    $data[$k]=array_column($content,$k);

    }
    $data=array_combine($headers,$data);

    var_dump($data);

<强>输出

Array
(
    [ id ] => Array
        (
            [0] =>  id1 
            [1] =>  id2 
            [2] =>  id3 
            [3] =>  id4 
        )

    [Name] => Array
        (
            [0] => Name1
            [1] => Name2
            [2] => Name3
            [3] => Name4
        )

    [ Balance ] => Array
        (
            [0] =>  balance1 
            [1] =>  balance2 
            [2] =>  balance3 
            [3] =>  balance4 
        )

    [Date] => Array
        (
            [0] => date1
            [1] => date2
            [2] => date1
            [3] => date4
        )

    [ Region ] => Array
        (
            [0] =>  region1 
            [1] =>  region2 
            [2] =>  region3 
            [3] =>  region4 
        )

    [ Segment ] => Array
        (
        )

    [Manager] => Array
        (
        )

    [ Contract ] => Array
        (
        )

)

答案 1 :(得分:0)

$headers = array ('0 '=>' id ', '1' => 'Name', '2 '=>' Balance ', '3' => 'Date', '4 '=>' Region ', '5 '=>' Segment ', '6' => 'Manager', '7 '=>' Contract ');
$content = array (
array ('0'=>' id1 ', '1' => 'Name1', '2 '=>'balance1 ', '3' => 'date1', '4'=>' region1 ', '5' => 'segment1', '6'=>' manager1 ', '7' => ' sontract1 '),
array ('0'=>' id2 ', '1' => 'Name2', '2'=>'balance2 ', '3' => 'date2', '4'=>' region2 ', '5' => 'segment2', '6'=>' manager2 ', '7' => ' sontract2 '),
array ('0'=>' id3 ', '1' => 'Name3', '2'=>'balance3 ', '3' => 'date1', '4'=>' region3 ', '5' => 'segment3', '6'=>' manager3 ', '7' => ' sontract3 '),
array ('0'=>' id4 ', '1' => 'Name4', '2'=>'balance4 ', '3' => 'date4', '4'=>' region4 ', '5' => 'segment4', '6'=>' manager4 ', '7' => ' sontract4 '));

$result = array();
$count = 0;
foreach($headers as $key => $value){

    $tempArr = array();
    foreach ($content as $arr){
        $tempArr[] =$arr[$count];
    }
    $count +=1;
    $result[$value] = $tempArr;

}
echo "<pre>";
print_r($result);
echo "</pre>";

输出:

Array
(
    [ id ] => Array
        (
            [0] =>  id1 
            [1] =>  id2 
            [2] =>  id3 
            [3] =>  id4 
        )

    [Name] => Array
        (
            [0] => Name1
            [1] => Name2
            [2] => Name3
            [3] => Name4
        )

    [ Balance ] => Array
        (
            [0] => 
            [1] => balance2 
            [2] => balance3 
            [3] => balance4 
        )

    [Date] => Array
        (
            [0] => date1
            [1] => date2
            [2] => date1
            [3] => date4
        )

    [ Region ] => Array
        (
            [0] =>  region1 
            [1] =>  region2 
            [2] =>  region3 
            [3] =>  region4 
        )

    [ Segment ] => Array
        (
            [0] => segment1
            [1] => segment2
            [2] => segment3
            [3] => segment4
        )

    [Manager] => Array
        (
            [0] =>  manager1 
            [1] =>  manager2 
            [2] =>  manager3 
            [3] =>  manager4 
        )

    [ Contract ] => Array
        (
            [0] =>  sontract1 
            [1] =>  sontract2 
            [2] =>  sontract3 
            [3] =>  sontract4 
        )

)