The array is dynamic, can be with 7 or more keys, except the first key never changes.
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 ) ) The first array is what I fetch from db as result of SP with four result sets
I'm doing this:
$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; } } And this is what I'm getting:
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 ) ) ) It's the best way to reorganize the array