如何将数组列表放入另一个具有不同格式的数组中

时间:2016-10-05 13:36:09

标签: php html arrays

我的代码是seguent:

Array
(
    [0] => Array
        (
            [ASUSEMANUELE] => 2
            [BOLLE] => 1
            [BRUNELLA] => 1
            [CHIARA] => 2
            [IMACSONORA] => 2
            [INTERNET] => 1
            [LAB1] => 2
            [LAB2] => 2
            [MARKETING] => 1
            [OFFICE] => 2
            [QUALITA] => 1
            [RAIMONDO] => 2
            [SC] => 2
            [SERVER2K8] => 2
            [SICA] => 1
        )

)
1

Array
(
    [0] => Array
        (
            [LEGALE1] => 2
            [LEGALE2] => 1
            [LEGALE3] => 2
        )

)
1

我想要的是:

Array
(
    [0] => Array
        (
            [ASUSEMANUELE] => 2
            [BOLLE] => 1
            [BRUNELLA] => 1
            [CHIARA] => 2
            [IMACSONORA] => 2
            [INTERNET] => 1
            [LAB1] => 2
            [LAB2] => 2
            [MARKETING] => 1
            [OFFICE] => 2
            [QUALITA] => 1
            [RAIMONDO] => 2
            [SC] => 2
            [SERVER2K8] => 2
            [SICA] => 1
        )

    [1] => Array
        (
            [LEGALE1] => 2
            [LEGALE2] => 1
            [LEGALE3] => 2
        )

)

数组在一个php变量(例如$ array)中,所以没有separeted 2个数组...否则我会使用php函数array_merge。 我怎么能用PHP?非常感谢

RE-编辑: 我用来生成数组的代码是:

$files = glob('logs/*.xml', GLOB_BRACE);
foreach($files as $file) {
    $xmlLog = simplexml_load_file($file);

    foreach($xmlLog->LogsUser->User as $LogsUser): 
        $arr_ks[] = $LogsUser->Name;
        $arr_ch[] = checkLastAccess(date('Y-m-d', rtrim($LogsUser->LastModify)));
    endforeach;

    $c = array(array_combine($arr_ks, $arr_ch));
}

最后编辑: XML第一个数组

<Logs>
    <LogsUser>
       <UserName>1c:98:ec:0e:9d:98</UserName>
       <User>
          <Name>ASUSEMANUELE</Name>
          <SpaceUsed>143134152</SpaceUsed>
          <Letter>G</Letter>
          <LastAccess>1475596801</LastAccess>
          <LastModify>1475248419</LastModify>
       </User>
       <User>
          <Name>BOLLE</Name>
          <SpaceUsed>857512</SpaceUsed>
          <Letter>M</Letter>
          <LastAccess>1475658001</LastAccess>
          <LastModify>1471844853</LastModify>
       </User>
       <User>
          <Name>BRUNELLA</Name>
          <SpaceUsed>4027220</SpaceUsed>
          <Letter>G</Letter>
          <LastAccess>1475625602</LastAccess>
          <LastModify>1464599323</LastModify>
       </User>
       <User>
          <Name>CHIARA</Name>
          <SpaceUsed>31480800</SpaceUsed>
          <Letter>G</Letter>
          <LastAccess>1475650802</LastAccess>
          <LastModify>1475475835</LastModify>
       </User>
       <User>
          <Name>IMACSONORA</Name>
          <SpaceUsed>29429480</SpaceUsed>
          <Letter>G</Letter>
          <LastAccess>1475665201</LastAccess>
          <LastModify>1475665144</LastModify>
       </User>
       <User>
          <Name>INTERNET</Name>
          <SpaceUsed>40519812</SpaceUsed>
          <Letter>G</Letter>
          <LastAccess>1475661601</LastAccess>
          <LastModify>1473056006</LastModify>
       </User>
       <User>
          <Name>LAB1</Name>
          <SpaceUsed>4180192</SpaceUsed>
          <Letter>G</Letter>
          <LastAccess>1475676002</LastAccess>
          <LastModify>1474982475</LastModify>
       </User>
       ...
   </LogsUser>
   <LogsDisk>
       <Disk>
           <Total>3845561016</Total>
           <Used>1410121024</Used>
           <Status>0</Status>
       </Disk>
   </LogsDisk>
   <LogsVersion>
       <Version>2.0-10</Version>
   </LogsVersion>
</Logs>

XML第二个数组

<Logs>
    <LogsUser>
        <UserName>1c:98:ec:1e:b0:84</UserName>
        <User>
            <Name>LEGALE1</Name>
            <SpaceUsed>175987628</SpaceUsed>
            <Letter>G</Letter>
            <LastAccess>1475611207</LastAccess>
            <LastModify>1475610761</LastModify>
        </User>
        <User>
            <Name>LEGALE2</Name>
            <SpaceUsed>701804</SpaceUsed>
            <Letter>M</Letter>
            <LastAccess>1475604002</LastAccess>
            <LastModify>1473186312</LastModify>
        </User>
        <User>
            <Name>LEGALE3</Name>
            <SpaceUsed>10949684</SpaceUsed>
            <Letter>G</Letter>
            <LastAccess>1475607602</LastAccess>
            <LastModify>1475606475</LastModify>
        </User>
    </LogsUser>
    <LogsDisk>
        <Disk>
            <Total>1922722952</Total>
            <Used>187851100</Used>
            <Status>0</Status>
        </Disk>
    </LogsDisk>
    <LogsVersion>
        <Version>2.0-10</Version>
    </LogsVersion>
</Logs>

4 个答案:

答案 0 :(得分:0)

最直接的方法是创建一个新数组并推送你想要的东西 -

$newArray = [];
array_push($newArray, $firstArray[0], $secondArray[0]);
print_r($newArray);

答案 1 :(得分:0)

试试这个,

foreach($array2 as $val)
{
    $array1[]  = $val;    
}

<强> DEMO

答案 2 :(得分:0)

DEMO

您的案例是具体的。我建议你创建新的自定义函数来完成工作并根据需要适当地使用它。

$newArray = array();
$a = array(array("ASUSEMANUELE" => 2,"BOLLE" => 1,"BRUNELLA" =>"..."));
$b = array(array("LEGALE1" => 2,"LEGALE2" => 1,"LEGALE3" => 2));

function specialArrayMerge($oldArray, $newArray)
{   
    array_push($newArray, array());
    $highestIndex = max(array_keys($newArray));
    foreach ($oldArray as $row)
    {
        foreach ($row as $key => $value)
        {
            $newArray[$highestIndex][$key] = $value;
        }
    }
    return $newArray;
}

$newArray = specialArrayMerge($a, $newArray);
$newArray = specialArrayMerge($b, $newArray);

echo '<pre>';
var_dump($newArray);
echo '</pre>';

答案 3 :(得分:0)

改变你的foreach循环 -

$output = array();
$files = glob('logs/*.xml', GLOB_BRACE);
foreach($files as $file) {
    $xmlLog = simplexml_load_file($file);

    $file_log_user = array();
    foreach($xmlLog->LogsUser->User as $LogsUser){
        $file_log_user[(string) $LogsUser->Name] = checkLastAccess(date('Y-m-d', rtrim($LogsUser->LastModify)));
    }
    $output[] = $file_log_user;
}

print_r($output);

看起来您实际上正在调用该代码两次,因此正在打印两个数组。