0
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

The answer depends on what you want when you say 'efficient'. In my opinion, you should be using either a stdClass object with an array of Contacts, an array of Algorithms, etc. This will be more memory-intensive, but unless you are running in crazy looping structures then you will not notice any performance hit. What you would be gaining is readability and maintainability, which is far more valuable down the road.

Of course, if you are hitting a specific bottleneck for which you feel that you need "more efficient code" then please do describe the issue.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

This function can be used for array with n length

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

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
                )

        )

)
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.