1

The array is dynamic, can be with 7 ,more or less 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
        )

)

I need change it to something like this:

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

    )

The first array is what I fetch from db as result of SP with four result sets, I would like to organize the array the way that it look like in the second example.

I tried doing this:

$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++;
}

And this is what I'm getting:

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 where I'm trying new things

3
  • The answer to your question is probably too large for this site, but I'd encourage you to take a look at some PHP array functions - there's tons of powerful ones at your disposal. For example: key , array_keys Commented Sep 6, 2013 at 16:17
  • Why do you want to reorganize the array? What is your ultimate goal? Commented Sep 6, 2013 at 16:18
  • @AndyLester because I need to use the data in different places, and the original array doesnt have the same length always the address part not necessarily has the 3 types always, and the Algorithm and Exeption part have variable quantity in the result can be from 0 to unlimited Commented Sep 6, 2013 at 16:36

2 Answers 2

2

Why not use a foreach to iterate through the first array and then use a switch to detect which type of key->value pairs to expect like so:

EDIT: Code immediately below is wrong. Correct answer added below this code.

<?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;

    }

}

?>

I am sure this can be super simplified, but I am going for understanding here as opposed to efficiency. Using this method, you can then check for the algorithms and exceptions as well, and it does not expect any length or limitation of number of records.

EDIT: Fixed several mistakes and issues. I am not sure why PHP does not index the array when using foreach - I may need to brush up on my PHP functions. I apologize for the confusion. I am leaving the original solution above to display what not to do.

<?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;

    }

}
?>

I am pretty new to StackOverflow and I must apologize for making such a bad first impression.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm going to try your idea...thanks, I will comment the results, but
I went ahead and finished the script to check for algorithms and exceptions as well. This should allow you to replace $newarray with any array you are using and continuously add new records over time.
There needs to be quotes around Retro-Term and a few others. Make sure you have all of your strings enclosed with quotes. I also noticed a mistake by myself. Changed $count = $count++ to simply $count++;.
I fixed, but still continue getting empty values in the array...thanks for your cooperation
Edited with correct solution. I am sorry for leading you on with incorrect solutions.
1

In the answer @J.Michieli, he said that his solution can be super simplified, and I did this to simplify the process. 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
                )

        )

)

1 Comment

Excellent refactoring! Thanks for simplifying for better use!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.