0

I am not familiar with PHP, and it is hard for me to understand arrays in PHP. I have some problem to resolve, and I write simple JAVA class to resolve it, but I need it in PHP. Could some one help me to covert this below code to PHP?

Java code:

public static void main(String[] args) {
        Map<Integer, List<Integer>> outputData = new HashMap<Integer,List<Integer>>();
        int[][] inputData = new int[6][2];
        inputData[0][0] = 1;
        inputData[0][1] = 22;
        inputData[1][0] = 1;
        inputData[1][1] = 33;
        inputData[2][0] = 2;
        inputData[2][1] = 44;
        inputData[3][0] = 2;
        inputData[3][1] = 55;
        inputData[4][0] = 3;
        inputData[4][1] = 66;
        inputData[5][0] = 1;
        inputData[5][1] = 77;

        // process input data
        for(int i = 0;i<=5;i++){
            if(outputData.containsKey(inputData[i][0])){
                List<Integer> list = outputData.get(inputData[i][0]);
                list.add(inputData[i][1]);

            }else{
                List<Integer> list = new ArrayList<Integer>();
                list.add(inputData[i][1]);
                outputData.put(inputData[i][0], list);
            }
        }

        System.out.println("INPUT DATA DISPLAY");
        for(int i = 0;i<=5;i++){
            System.out.println("i: "+inputData[i][0] + " = "+inputData[i][1]);
        }
        System.out.println("OUTPUT MAP");
        for(Integer i : outputData.keySet()){
            List<Integer> l = outputData.get(i);
            System.out.print("i: "+i+" = ");
            for(Integer j : l){
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }

I have input table:

i: 1 = 22
i: 1 = 33
i: 2 = 44
i: 2 = 55
i: 3 = 66
i: 1 = 77

and I need it to group elements by key. This is output:

i: 1 = 22 33 77 
i: 2 = 44 55 
i: 3 = 66 

In JAVA it is working as expected, but in PHP I am not able to write it, though I read some tutorials, manuals and examples.

[EDIT]: The real issue is: I have an excel file with N rows (loading excel is implemented). Every row have 2 columns.

enter image description here

I want to group it like this:

  • data from column B should be grouped by value from column A.

In this case:

  • for value 1 it will be 123, 63, 3 (rows 1, 2 and 6)
  • for value 2 it will be 3, 23 (rows 3 and 4)
  • for value 3 it will be 55, 234 (rows 5 and 7)
2
  • 1
    Hard to say if it is an duplicate. I know that automatically convert is not possible. I need help to write in PHP above logic implemented in JAVA, Commented Jan 5, 2017 at 13:42
  • Probably you're right, sorry. Commented Jan 5, 2017 at 13:44

1 Answer 1

1

The format of the input data must be changed, because in PHP the keys must be unique. I have tested this code: (it is much simpler than in JAVA) (So the input is an array of associative arrays.)

<?php

function groupByKey($input) {
    $result = array();

    foreach($input as $item) {
        foreach($item as $key => $value) {
            $result[$key][] = $value;
        }
    }

    return $result;
}

$data = array(
    array(1 => 22),
    array(1 => 33),
    array(2 => 44),
    array(2 => 55),
    array(3 => 66),
    array(1 => 77)
);

print_r($data);
echo "\n\n";

$result = groupByKey($data);

print_r($result);

Results:

Array
(
    [0] => Array
        (
            [1] => 22
        )

    [1] => Array
        (
            [1] => 33
        )

    [2] => Array
        (
            [2] => 44
        )

    [3] => Array
        (
            [2] => 55
        )

    [4] => Array
        (
            [3] => 66
        )

    [5] => Array
        (
            [1] => 77
        )

)


Array
(
    [1] => Array
        (
            [0] => 22
            [1] => 33
            [2] => 77
        )

    [2] => Array
        (
            [0] => 44
            [1] => 55
        )

    [3] => Array
        (
            [0] => 66
        )

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

1 Comment

In fact, in JAVA I parsed xls in format: 'row1, row2,..,rowN'. Every row has columns: col1, col2. Colums are not unique. I will look into your code. Thanks.

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.