将索引的关联阵列转换为3D关联阵列

问题描述 投票:0回答:2

数组是动态的,可以使用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 ) )

link我在哪里尝试新事物

    

当不使用foreach通过第一个数组进行迭代,然后使用开关来检测类型的键 - >值对,以便这样的期望:

php arrays multidimensional-array grouping associative-array
2个回答
2
投票
<?php $newArray = array(); foreach($array as $arr) { $keys = array_keys($arr); switch($keys[0]) { case "PORAProviderID": if(!isset($newArray["ProviderInfo"])) { $newArray["ProviderInfo"] = array(); } $newArray["ProviderInfo"]["PORAProviderID"] = $arr[0]; $newArray["ProviderInfo"]["ProviderName"] = $arr[1]; break; case "ContactName": if(!isset($newArray["ProviderAddress"])) { $newArray["ProviderAddress"] = array(); } $newArray["ProviderAddress"][$arr[2]] = array(); $newArray["ProviderAddress"][$arr[2]]["ContactName"] = $arr[0]; $newArray["ProviderAddress"][$arr[2]]["Address1"] = $arr[1]; $newArray["ProviderAddress"][$arr[2]]["AddressType"] = $arr[2]; break; case "AlgorithmID": if(isset($newArray["ProviderAlgorithm"])) { $count = count($newArray["ProviderAlgorithm"]); } else { $newArray["ProviderAlgorithm"] = array(); $count = -1; } $count++; $newArray["ProviderAlgorithm"][$count] = array(); $newArray["ProviderAlgorithm"][$count]["AlgorithmID"] = $arr[0]; $newArray["ProviderAlgorithm"][$count]["AlgoTitle"] = $arr[1]; break; case "HoldType": if(isset($newArray["ProviderException"])) { $count = count($newArray["ProviderException"]); } else { $newArray["ProviderException"] = array(); $count = -1; } $count++; $newArray["ProviderException"][$count] = array(); $newArray["ProviderException"][$count]["HoldType"] = $arr[0]; $newArray["ProviderException"][$count]["StatusID"] = $arr[1]; break; } } ?>

我相信这可以超级简化,但是我会在这里理解而不是效率。 然后,您也可以检查算法和异常,并且不会期望记录数的任何长度或限制。

Edit:修复了几个错误和问题。 我不确定为什么使用foreach时php不会索引数组 - 我可能需要刷新我的PHP功能。 我为混乱表示歉意。 我要在上面留下原始解决方案以显示不做的事情。

<?php $array=array( 0 => array ( 'PORAProviderID' => '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' => 1, 'AlgoTitle' => 'Retro-Term' ), 6 => array ( 'HoldType' => 'HoldType', 'StatusID' => 1 ), 7 => array ( 'HoldType' => 'HoldType', 'StatusID' => 1 ), 8 => array ( 'HoldType' => 'Hold', 'StatusID' => 2 ) ); $newArray = array(); foreach($array as $arr) { $keys = array_keys($arr); switch($keys[0]) { case "PORAProviderID": if(!isset($newArray["ProviderInfo"])) { $newArray["ProviderInfo"] = array(); } $newArray["ProviderInfo"]["PORAProviderID"] = $arr["PORAProviderID"]; $newArray["ProviderInfo"]["ProviderName"] = $arr["ProviderName"]; break; case "ContactName": if(!isset($newArray["ProviderAddress"])) { $newArray["ProviderAddress"] = array(); } $newArray["ProviderAddress"][$arr['AddressType']] = array(); $newArray["ProviderAddress"][$arr['AddressType']]["ContactName"] = $arr["ContactName"]; $newArray["ProviderAddress"][$arr['AddressType']]["Address1"] = $arr["Address1"]; $newArray["ProviderAddress"][$arr['AddressType']]["AddressType"] = $arr["AddressType"]; break; case "AlgorithmID": if(isset($newArray["ProviderAlgorithm"])) { $count = count($newArray["ProviderAlgorithm"]); } else { $newArray["ProviderAlgorithm"] = array(); $count = 0; } $newArray["ProviderAlgorithm"][$count] = array(); $newArray["ProviderAlgorithm"][$count]["AlgorithmID"] = $arr["AlgorithmID"]; $newArray["ProviderAlgorithm"][$count]["AlgoTitle"] = $arr["AlgoTitle"]; break; case "HoldType": if(isset($newArray["ProviderException"])) { $count = count($newArray["ProviderException"]); } else { $newArray["ProviderException"] = array(); $count = 0; } $newArray["ProviderException"][$count] = array(); $newArray["ProviderException"][$count]["HoldType"] = $arr["HoldType"]; $newArray["ProviderException"][$count]["StatusID"] = $arr["StatusID"]; break; } } ?>

我对Stackoverflow非常陌生,我必须为留下如此糟糕的第一印象而道歉。

在答案 @j.michieli中,他说他的解决方案可以超级简化,我这样做是为了简化过程。此函数可用于具有

n

长度

的数组

$key_search = array( 'PORAProviderID' => 'ProviderInfo', 'ContactName' => 'ProviderAddress', 'HoldType' => 'ProviderException', 'AlgorithmID' => 'ProviderAlgorithm' ); /** * Reorganize array by categories * @param array $array * @param array $key_search keyToSearch=>NewArrayKey * @return array */ function organizeArray($array, $key_search) { $new_array = array(); if (count($array) == count($array, COUNT_RECURSIVE)) { //single array foreach ($key_search as $key => $key_data) { if (array_key_exists($key, $array)) { $new_array[$key_data] = array($array); } } } else { //nested array foreach ($array as $array_data) { foreach ($key_search as $key => $key_data) { if (array_key_exists($key, $array_data)) { if (isset($new_array[$key_data])) { $temp = $new_array[$key_data]; array_push($temp, $array_data); $new_array[$key_data] = $temp; }else $new_array[$key_data] = array($array_data); } } } } return $new_array; } $array = array( 0 => array ( 'PORAProviderID' => '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' => 1, 'AlgoTitle' => 'Retro-Term' ), 6 => array ( 'HoldType' => 'HoldType', 'StatusID' => 1 ), 7 => array ( 'HoldType' => 'HoldType', 'StatusID' => 1 ), 8 => array ( 'HoldType' => 'Hold', 'StatusID' => 2 ) );
Otuput

print_r(organizeArray($array, $key_search));

1
投票

Array ( [ProviderInfo] => Array ( [0] => Array ( [PORAProviderID] => 1010 [ProviderName] => HAMZEPOUR, SHOKOUFEH ) ) [ProviderAddress] => Array ( [0] => Array ( [ContactName] => ABC XYZ [Address1] => New York [AddressType] => Physical ) [1] => Array ( [ContactName] => ABC XYZ [Address1] => New York [AddressType] => Billing ) [2] => Array ( [ContactName] => ABC XYZ [Address1] => New York [AddressType] => Mailing ) ) [ProviderAlgorithm] => Array ( [0] => Array ( [AlgorithmID] => 1 [AlgoTitle] => Retro-Term ) [1] => Array ( [AlgorithmID] => 1 [AlgoTitle] => Retro-Term ) ) [ProviderException] => Array ( [0] => Array ( [HoldType] => HoldType [StatusID] => 1 ) [1] => Array ( [HoldType] => HoldType [StatusID] => 1 ) [2] => Array ( [HoldType] => Hold [StatusID] => 2 ) ) )

    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.