0

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.

9
  • so you are trying to group all these array by property right? Commented Nov 21, 2017 at 13:42
  • yes sir, These values are all coming in from a group of form selection boxes so the arrays are auto generated by the forms as shown in the first part of the question. But they then need to be loaded to the database with a propertyid filter (hence conversion) Commented Nov 21, 2017 at 13:45
  • I can think of a couple solutions to the problem I think you're trying to solve, but I don't really know the full scope of the problem or solution based on your original post. Commented Nov 21, 2017 at 13:46
  • The problem really does resolve to the example shown. The original problem is just bigger (much more data) that's all DJ. So would really appreciate a lead on any ideas you have Commented Nov 21, 2017 at 13:50
  • 1
    @FirstOne yes there are thousands of possible properties. However the structure of the arrays as above never changes. Commented Nov 21, 2017 at 14:17

4 Answers 4

1

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

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

2 Comments

That does it. Thank you. Close to a couple of other suggestions deserving a notch up although this one seems the most straight forward.
You can skip the part that merges the arrays if you want. I wrote that answer before I got your answer in comments so I just edited it to fit. But $arr[0][$key] can be replaced with $cost_type[$key] and the same with $supplier_name. And that means the foreach loops $propertyid instead. But the result is going to be the same.
1

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].

1 Comment

thank you. Your answer is very similar to Andreas who beat you to it by a whisker. I marked you ulp though. Again thank you
1
        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>';

1 Comment

It's not for me. But it's more correct than before. But you are missing an ; and the output is still wrong. Prop2 should not have isp2. 3v4l.org/g52YF edit i see you found the ;
0

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

2 Comments

Unexpected { in .... You missed a ) in the condition.
Having difficulty relating this to the problem to be honest. The principles are standard. The solution,,,cant really see how it applies just now. Bit more explanation would really help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.