使用PHP构建动态数组

时间:2017-04-16 05:16:19

标签: php arrays

我从XML SOAP服务中提取结果,该服务返回的结果结果不能很好地将结果编码为json然后解码成数组。

以下是我通过在结果上运行以下函数呈现的输出示例:

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

)

2 个答案:

答案 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);

答案 1 :(得分:0)

您希望构建阵列的方式如下。原因是你希望像“accountList”和“personalProfile”这样的东西成为数组中的唯一键,就像它们在关系数据库中一样。

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