2017-04-16 67 views
-1

我從XML SOAP服務中獲取結果,該結果返回的結果不適用於將結果編碼爲json然後解碼到數組中。使用PHP構建動態數組

下面是我通過我的結果運行以下功能渲染輸出的一個例子:

$result[implode('/',$keys)] = $val; 



Array 
(
    [accountList/accountNo] => 0610010000048744 
    [accountList/accountOpeningDate] => 2014-09-01T00:00:00+03:00 
    [accountList/accountStatus] => DORMANT 
    [accountList/arrearAmount] => 80000.0 
    [accountList/arrearDays] => 30 
    [accountList/balanceAmount] => 120000.0 
    [accountList/disputed] => false 
    [accountList/isMyAccount] => false 
    [accountList/lastPaymentDate] => 2015-01-23T00:00:00+03:00 
    [accountList/listingDate] => 2014-09-30T00:00:00+03:00 
    [accountList/pastDueDate] => 1900-01-01T00:00:00+03:00 
    [accountList/principalAmount] => 300000.0 
    [accountList/scheduledPaymentAmount] => 0.0 
    [accountList/tradeSector] => Bank Sector Bureau 
    [accountList/worstArrear] => 30 
    [accountList/currency] => KES 
    [personalProfile/crn] => 79908 
    [personalProfile/dateOfBirth] => 1959-01-01T00:00:00+02:45 
    [personalProfile/fullName] => Surname 79908 OtherNames 79908 
    [personalProfile/gender] => F 
    [personalProfile/nationalID] => ID79908 
    [summary/npaAccounts/mySector] => 2 
    [summary/npaAccounts/otherSectors] => 0 
    [summary/npaTotalValueList/currency] => KES 
    [summary/npaTotalValueList/mySector] => 120000.0 
    [summary/npaTotalValueList/otherSectors] => 0.0 
    [summary/openAccounts/mySector] => 2 
    [summary/openAccounts/otherSectors] => 0 
) 

因爲結果和鍵始終取決於要求我做服務器,我想通過遞歸函數構建多維數組,在某些情況下,例如鍵。爲accountList可以有多個值

預期的結果會是這個樣子:

Array 
(


    [accountList] => Array 
     (
      [accountNo] => 0610010000048744 
      [accountOpeningDate] => 2014-09-01T00:00:00+03:00 
      [accountStatus] => DORMANT 
      [arrearAmount] => 80000.0 
      [arrearDays] => 30 
      [balanceAmount] => 120000.0 
      [disputed] => false 
      [isMyAccount] => false 
      [lastPaymentDate] => 2015-01-23T00:00:00+03:00 
      [listingDate] => 2014-09-30T00:00:00+03:00 
      [pastDueDate] => 1900-01-01T00:00:00+03:00 
      [principalAmount] => 300000.0 
      [scheduledPaymentAmount] => 0.0 
      [tradeSector] => Bank, Sector Bureau 
      [worstArrear] => 30 
      [currency] => KES 
     ) 
    Array 
    (
     [accountNo] => 0610010000048788 
     [accountOpeningDate] => 2014-09-01T00:00:00+03:00 
     [accountStatus] => ACTIVE 
     [arrearAmount] => 10000.0 
     [arrearDays] => 90 
     [balanceAmount] => 10000.0 
     [disputed] => TRUE 
     [isMyAccount] => false 
     [lastPaymentDate] => 2015-01-23T00:00:00+03:00 
     [listingDate] => 2014-09-30T00:00:00+03:00 
     [pastDueDate] => 1900-01-01T00:00:00+03:00 
     [principalAmount] => 300000.0 
     [scheduledPaymentAmount] => 0.0 
     [tradeSector] => Bank, Sector Bureau 
     [worstArrear] => 30 
     [currency] => KES 
    ) 

    [personalProfile] => Array 
     (
      [crn] => 79908 
      [dateOfBirth] => 1959-01-01T00:00:00+02:45 
      [fullName] => Surname, 79908 OtherNames 79908 
      [gender] => F 
      [nationalID] => ID79908 
     ) 

) 
+0

嗯,你可以給一個'$ val'的例子,以便我們可以測試一下嗎? – Passerby

+0

你可以根據你上面的內容添加一個結果數組包含的例子嗎? –

回答

2

我認爲你正在努力做到這一點。

Try this code snippet here

explode("/", $key)這將返回數組其中將包含兩個值。

list($first,$second)=explode("/", $key);並通過這個我把這些值,一個在$first和其他在$second

<?php 
ini_set('display_errors', 1); 
$data=Array 
(
    "accountList/accountNo"=>"0610010000048744", 
    "accountList/accountOpeningDate"=>"2014-09-01T00:00:00+03:00", 
    "accountList/accountStatus"=>"DORMANT", 
    "accountList/arrearAmount"=>"80000.0", 
    "accountList/arrearDays"=>"30", 
    "accountList/balanceAmount"=>"120000.0", 
    "accountList/disputed"=>"false", 
    "accountList/isMyAccount"=>"false", 
    "accountList/lastPaymentDate"=>"2015-01-23T00:00:00+03:00", 
    "accountList/listingDate"=>"2014-09-30T00:00:00+03:00", 
    "accountList/pastDueDate"=>"1900-01-01T00:00:00+03:00", 
    "accountList/principalAmount"=>"300000.0", 
    "accountList/scheduledPaymentAmount"=>"0.0", 
    "accountList/tradeSector"=>"Bank, Sector Bureau", 
    "accountList/worstArrear"=>"30", 
    "accountList/currency"=>"KES", 
    "personalProfile/crn"=>"79908", 
    "personalProfile/dateOfBirth"=>"1959-01-01T00:00:00+02:45", 
    "personalProfile/fullName"=>"Surname, 79908 OtherNames 79908", 
    "personalProfile/gender"=>"F", 
    "personalProfile/nationalID"=>"ID79908", 
    "sample/data/data1"=>"ID79908",//added a sample data 
); 
$result=array(); 
foreach($data as $key => $value) 
{ 
    $string=""; 
    $numberOfWords=explode("/", $key); 
    foreach($numberOfWords as $newValue) 
    { 
     $string.="['$newValue']"; 
    } 
    eval('$result'.$string."= \"$value\";"); 
} 
print_r($result); 
+0

@SloanThrasher當然。 –

+1

@Salhil:謝謝!今天學到了新東西。 –

+0

@SloanThrasher歡迎.... :) –

0

要建立你的陣列的方法如下。原因是你希望諸如「accountList」和「personalProfile」之類的東西是數組中的唯一鍵,就像它們在關係數據庫中一樣。

$array = $array('accountList'=>array("accountNo"=>"0610010000048744", 
"accountOpenDate"=>"2014-09- 
01T00:00:00+03:00"),array("personalProfile"=>array("...and so on.")); 
+0

他們的鑰匙是動態的,取決於函數調用,所以這可能不起作用 –