jimshot
jimshot

Reputation: 109

Restructuring Multi Dimensional Array Format

I am struggling with what would appear to be a pretty straight forward task. I have looked at and tried all kinds of functions and suggestion on SO hoping that maybe there is something simple and functional out there. Nothing I tried gives me the logic to do the restructuring.

I have a long complex array. However very much simplified the logic problem I am trying to solve generically is as follows:

$cost_type = Array
(
    0 => "ISP2",
    1 => "ISP3",
    2 => "ISP4"
);
$supplier_name  = Array
(
    0 => "NAME-A",
    1 => "NAME-B",
    2 => "NAME-C"
 );
$propertyid = Array
(
    0 => "property1",
    1 => "property2",
    2 => "property2"
);

and I need to convert it to the following set of arrays (noting the concatenation of the two arrays with a common property id.....

 $property1
 (
    array['charges']  
            [0] =>IPS2

   array ['names']
            [0] =>NAME-A
 )

 $property2
 (
   array['charges'] 
           [0] ->IPS3
           [1] =>IPS4

   array['names']
         [0] =>NAME-B
         [1] =>NAME-c
 ) 

I have tried everything over the course of the last few hours and a simple solution totally evades me.

Upvotes: 0

Views: 59

Answers (4)

DJ Sipe
DJ Sipe

Reputation: 1375

I can't say I completely follow what you are trying to do, but I think this may be part of the way there for you.

When trying to restructure data in PHP, it's often helpful to create a empty array (or other data structure) to store the new data in first. Then you find a way to loop over your initial data structure that allows you to insert items into your reformatted structure in the right sequence.

<?php
$properties = []; // Array to hold final result

// Loop over your initial inputs
foreach ($groupsOfValues as $groupName => $groupValues) {
    $temp = [];  // Array to hold each groupings reformatted data

    // Loop over the items in one of the inputs
    for ($i=0; $i<count($group) && $i<count($properties)+1; $i++) {
        if (!is_array($temp[$groupName])) {
             $temp[$groupName] = [];
        }
        $temp[$groupName][] = $group[$i];
    }

    $properties[] = $temp;
}

Upvotes: 0

Andreas
Andreas

Reputation: 23958

If you can join the three arrays as you say in comments above this code will generate the look you want.
I loop through the array with property and keep key as the key to find names and charges in the other subarrays.

$cost_type = Array
(
    0 => "ISP2",
    1 => "ISP3",
    2 => "ISP4"
);
$supplier_name  =Array
(
    0 => "NAME-A",
    1 => "NAME-B",
    2 => "NAME-C"
 );
$propertyid = Array
(
    0 => "property1",
    1 => "property2",
    2 => "property2"
);
$arr[] = $cost_type;
$arr[] = $supplier_name;
$arr[] = $propertyid;

$result = array();
Foreach($arr[2] as $key => $prop){
    $result[$prop]["charges"][] =$arr[0][$key];
    $result[$prop]["names"][] = $arr[1][$key];
}

Var_dump($result);

https://3v4l.org/EilvE

Upvotes: 1

Sourabh
Sourabh

Reputation: 500

        Run this code you will get smiler result as you want :
           $twodimantion=array();
$properties=array('property1','property2','property3');
$charges=array('ISP2','ISP3','ISP4');
$names=array('NAME-A','NAME-B','NAME-C');
foreach ($properties as $key => $property) {
       $twodimantion['charge'][$key]=$charges[$key];
       $twodimantion['names'][$key]=$names[$key];
       $twoarray[$property]=$twodimantion;
}
echo '<pre>';
print_r($twoarray);
echo '</pre>';

Upvotes: 1

FirstOne
FirstOne

Reputation: 6217

The following code converts the original array in the expected result:

$res = array();
foreach($arr[2] as $k => $foo){ // foreach property
    if(!isset($res[$foo])){ // add property if not yet in list
        $res[$foo] = array(
            'charges' => array($arr[0][$k]),
            'names' => array($arr[1][$k])
        );
    }else{ // add new value to already existing property
        $res[$foo]['charges'][] = $arr[0][$k];
        $res[$foo]['names'][] = $arr[1][$k];
    }
}

Check it out here: https://eval.in/904473

Of course, it assumes a bunch on things about the data, but it should work for any number of items.

And if you need the property in another variable, just access it with $res['name of it].

Upvotes: 1

Related Questions