Francesco Esposto Francesco Esposto - 2 months ago 6
HTML Question

How to put a list of an array into another array with different format

My code is the 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


And what I would is:

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
)

)


The array is in one php variable (ex. $array), so there are not 2 arrayes separeted... otherwise I would have used the php function array_merge.
How can I do with php? Many thanks

RE-EDIT:
The code I used to generate the array is:

$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));
}


LAST EDIT:
XML first array

<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 second array

<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>

Answer

Alter your foreach loop -

$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);

It looks like you're actually calling that code twice, hence the two arrays that are being printed.