该数组是动态的,可以有 7 个或多或少的键,但第一个键永远不会改变。
Array
(
[0] => Array
(
[ProviderID] => 1010
[ProviderName] => HAMZEPOUR, SHOKOUFEH
)
[1] => Array
(
[ContactName] => ABC XYZ
[Address1] => New York
[AddressType] => Physical
)
[2] => Array
(
[ContactName] => ABC XYZ
[Address1] => New York
[AddressType] => Billing
)
[3] => Array
(
[ContactName] => ABC XYZ
[Address1] => New York
[AddressType] => Mailing
)
[4] => Array
(
[AlgorithmID] => 1
[AlgoTitle] => Retro-Term
)
[5] => Array
(
[AlgorithmID] => 2
[AlgoTitle] => Modifier 25 errors
)
[6] => Array
(
[HoldType] => HoldType
[StatusID] => 1
)
[7] => Array
(
[HoldType] => HoldType
[StatusID] => 1
)
[8] => Array
(
[HoldType] => Hold
[StatusID] => 2
)
)
我需要把它改成这样:
Array
(
[ProviderInfo] => Array
(
[PORAProviderID] => 1010
[ProviderName] => HAMZEPOUR, SHOKOUFEH
)
[ProviderAddress] => Array
(
[Physical] => Array
(
[ContactName] => ABC XYZ
[Address1] => New York
[AddressType] => Physical
)
[Billing] => Array
(
[ContactName] => ABC XYZ
[Address1] => New York
[AddressType] => Billing
)
[Mailing] => Array
(
[ContactName] => ABC XYZ
[Address1] => New York
[AddressType] => Mailing
)
)
[ProviderAlgorithm] => Array
(
[0] => Array
(
[AlgorithmID] => 1
[AlgoTitle] => Retro-Term
)
[1] => Array
(
[AlgorithmID] => 2
[AlgoTitle] => Modifier 25 errors
)
)
[ProviderException] => Array
(
[0] => Array
(
[HoldType] => HoldType
[StatusID] => 1
)
[1] => Array
(
[HoldType] => HoldType
[StatusID] => 1
)
[2] => Array
(
[HoldType] => Hold
[StatusID] => 2
)
)
)
第一个数组是我从 db 作为 SP 的结果获取的,有四个结果集,我想按照第二个示例中的方式组织数组。
我试过这样做:
$search_array = $array;
$countb = count($search_array);
$counta = count($search_array) - 1;
//echo $countb;
$key_search = array('AlgorithmID', 'PORAProviderID', 'ContactName', 'HoldType');
$key_new = array('ProviderAlgorithm', 'ProviderInfo', 'ProviderAddress', 'ProviderException');
$b = 0;
while ($b <= $countb) {
$a = 0;
while ($a <= $counta) {
if (array_key_exists($key_search[$b], $search_array[$a])) {
$array[$key_new[$b]] = $array[$a];
unset($array[$a]);
// $a=$a-1;
}
$a++;
}
$b++;
}
这就是我得到的:
Array
(
[ProviderAlgorithm] => Array
(
[AlgorithmID] => 2
[AlgoTitle] => Modifier 25 errors
)
[ProviderInfo] => Array
(
[PORAProviderID] => 1010
[ProviderName] => HAMZEPOUR, SHOKOUFEH
)
[ProviderAddress] => Array
(
[ContactName] => ABC XYZ
[Address1] => New York
[AddressType] => Mailing
)
[ProviderException] => Array
(
[HoldType] => HoldType
[StatusID] => 1
)
)