2

Initial array i've started with:

{
    "0":{
        "id":"1",
        "country_id":"Austria",
        "VAT_contact_person":"Business development",
        "name":"Mayrhofer Herbert",
        "title":"Mag.",
        "email":"[email protected]",
        "phone":"0"},
    "4":{
        "id":"11",
        "country_id":"Bulgaria",
        "VAT_contact_person":"Business development",
        "name":"Mayrhofer Herbert BG",
        "title":"Mag. BG",
        "email":"[email protected] BG",
        "phone":"0"},
    "1":{
        "id":"2",
        "country_id":"Austria",
        "VAT_contact_person":"Technical contact",
        "name":"Mayrhofer Herbert",
        "title":"Mag.",
        "email":"[email protected]",
        "phone":"0"},
    "5":{
        "id":"12",
        "country_id":"Bulgaria",
        "VAT_contact_person":"Technical contact",
        "name":"Mayrhofer Herbert BG",
        "title":"Mag. BG",
        "email":"[email protected] BG",
        "phone":"0"},
    "2":{
        "id":"3",
        "country_id":"Austria",
        "VAT_contact_person":"VAT leader",
        "name":"G\u00fcnther Mayrleitner",
        "title":"Mag",
        "email":"[email protected]",
        "phone":"0"},
    "3":{
        "id":"4",
        "country_id":"Austria",
        "VAT_contact_person":"VAT leader",
        "name":"Ziegler Verena",
        "title":"MA",
        "email":"[email protected]",
        "phone":"0"},
    "6":{
        "id":"13",
        "country_id":"Bulgaria",
        "VAT_contact_person":"VAT leader",
        "name":"G\u00fcnther Mayrleitner BG",
        "title":"Mag BG",
        "email":"[email protected] BG",
        "phone":"0"},
    "7":{
        "id":"14",
        "country_id":"Bulgaria",
        "VAT_contact_person":"VAT leader",
        "name":"Ziegler Verena BG",
        "title":"MA BG",
        "email":"[email protected] BG",
        "phone":"0"
    }
}

I've got an array that looks like:

{
    "Business development":[
        {
            "id":"1",
            "country_id":"Austria",
            "VAT_contact_person":"Business development",
            "name":"Mayrhofer Herbert",
            "title":"Mag.",
            "email":"[email protected]",
            "phone":"0"
        },{
            "id":"11",
            "country_id":"Bulgaria",
            "VAT_contact_person":"Business development",
            "name":"Mayrhofer Herbert BG",
            "title":"Mag. BG",
            "email":"[email protected] BG",
            "phone":"0"
        }
    ],
    "Technical contact":[
        {
            "id":"2",
            "country_id":"Austria",
            "VAT_contact_person":"Technical contact",
            "name":"Mayrhofer Herbert",
            "title":"Mag.",
            "email":"[email protected]",
            "phone":"0"
        },{
            "id":"12",
            "country_id":"Bulgaria",
            "VAT_contact_person":"Technical contact",
            "name":"Mayrhofer Herbert BG",
            "title":"Mag. BG",
            "email":"[email protected] BG",
            "phone":"0"
        }
    ],
    "VAT leader":[
        {
            "id":"3",
            "country_id":"Austria",
            "VAT_contact_person":"VAT leader",
            "name":"G\u00fcnther Mayrleitner",
            "title":"Mag",
            "email":"[email protected]",
            "phone":"0"
        },{
            "id":"4",
            "country_id":"Austria",
            "VAT_contact_person":"VAT leader",
            "name":"Ziegler Verena",
            "title":"MA",
            "email":"[email protected]",
            "phone":"0"
        },{
            "id":"13",
            "country_id":"Bulgaria",
            "VAT_contact_person":"VAT leader",
            "name":"G\u00fcnther Mayrleitner BG",
            "title":"Mag BG",
            "email":"[email protected] BG",
            "phone":"0"
        },{
            "id":"14",
            "country_id":"Bulgaria",
            "VAT_contact_person":"VAT leader",
            "name":"Ziegler Verena BG",
            "title":"MA BG",
            "email":"[email protected] BG",
            "phone":"0"
        }
    ]
}

And i want it to look like:

{
    "Business development":[
        "Austria": [
            "id":"1",
            "country_id":"Austria",
            "VAT_contact_person":"Business development",
            "name":"Mayrhofer Herbert",
            "title":"Mag.",
            "email":"[email protected]",
            "phone":"0"
        ], 
        "Bulgaria": [
            "id":"11",
            "country_id":"Bulgaria",
            "VAT_contact_person":"Business development",
            "name":"Mayrhofer Herbert BG",
            "title":"Mag. BG",
            "email":"[email protected] BG",
            "phone":"0"
        ]
    ],
    "Technical contact":[
        "Austria": [
            "id":"2",
            "country_id":"Austria",
            "VAT_contact_person":"Technical contact",
            "name":"Mayrhofer Herbert",
            "title":"Mag.",
            "email":"[email protected]",
            "phone":"0"
        ], 
        "Bulgaria": [
            "id":"12",
            "country_id":"Bulgaria",
            "VAT_contact_person":"Technical contact",
            "name":"Mayrhofer Herbert BG",
            "title":"Mag. BG",
            "email":"[email protected] BG",
            "phone":"0"
        ]
    ],
    "VAT leader":[
        "Austria": [
            {
                "id":"3",
                "country_id":"Austria",
                "VAT_contact_person":"VAT leader",
                "name":"G\u00fcnther Mayrleitner",
                "title":"Mag",
                "email":"[email protected]",
                "phone":"0"
            },{
                "id":"4",
                "country_id":"Austria",
                "VAT_contact_person":"VAT leader",
                "name":"Ziegler Verena",
                "title":"MA",
                "email":"[email protected]",
                "phone":"0"
            }
        ], 
        "Bulgaria": [
            {
                "id":"13",
                "country_id":"Bulgaria",
                "VAT_contact_person":"VAT leader",
                "name":"G\u00fcnther Mayrleitner BG",
                "title":"Mag BG",
                "email":"[email protected] BG",
                "phone":"0"
            },{
                "id":"14",
                "country_id":"Bulgaria",
                "VAT_contact_person":"VAT leader",
                "name":"Ziegler Verena BG",
                "title":"MA BG",
                "email":"[email protected] BG",
                "phone":"0"
            }
        ]
    ]
}

What i did so far:

function prepare_data($array, $primary_instance, $dependency=NULL){
    $handled_data = [];
    $cloned = $array;

    foreach ($array as $k => $v) {
        foreach ($cloned as $key => $value) {
            if ($k != $key && $v[$primary_instance] == $value[$primary_instance]) {
                if (!in_array($value[$primary_instance], $handled_data) ) {
                    $handled_data[$value[$primary_instance]][] = $v;
                    break;
                }
            }
        }
    }

    // if has dependencies, handle it
    if(!is_null($dependency)){
        $cloned = $handled_data;

        foreach ($handled_data as $k => $v) {
            foreach ($cloned as $key => $value) {
                if ($v != $value && $k[$dependency] == $key[$dependency]) {
                    if (!in_array($key[$dependency], $handled_data) ) {
                        $handled_data[$dependency][][$key[$dependency]][] = $v;
                        break;
                    }
                }
            }
        }
    }

    return $handled_data;
}

How i imagined it would work: Call the function with initial array and add the keys prepare_data($sorted_contacts, 'VAT_contact_person', 'country_id')

I've managed to make it from worst to the first example using the first part of the function without if(!is_null($dependency)) condition but now, i want to use the $dependency i'm passing in the function to structure from first example to the second example.

EDIT: The final form of returned array should contain only array

EDIT 2:

"VAT leader":{
        "Austria":[
            {
                "id":"3",
                "country_id":"Austria",
                "VAT_contact_person":"VAT leader",
                "name":"Ziegler Verena",
                "title":"MA",
                "email":"[email protected]",
                "phone":"0"
            },
            {
                "id":"4",
                "country_id":"Austria",
                "VAT_contact_person":"VAT leader",
                "name":"Ziegler Verena",
                "title":"MA",
                "email":"[email protected]",
                "phone":"0"
            }
        ],
        "Bulgaria":[
            {
                "id":"13",
                "country_id":"Bulgaria",
                "VAT_contact_person":"VAT leader",
                "name":"Ziegler Verena",
                "title":"MA",
                "email":"[email protected]",
                "phone":"0"
            },
            {
                "id":"14",
                "country_id":"Bulgaria",
                "VAT_contact_person":"VAT leader",
                "name":"Ziegler Verena",
                "title":"MA",
                "email":"[email protected]",
                "phone":"0"
            }
        ]
    }
2
  • in the "what you want" half of the object are object, and the other half are arrays... can you please define when make them array or object? Commented Jan 24, 2020 at 2:06
  • If it can be turned from "initial array" (first array) to "partially handled" (second array) as array only the "final form" (final array) should be only arrays inside. Commented Jan 24, 2020 at 2:10

1 Answer 1

2

So if we start with this array...

$data = json_decode('
{
    "0":{
        "id":"1",
        "country_id":"Austria",
        "VAT_contact_person":"Business development",
        "name":"Mayrhofer Herbert",
        "title":"Mag.",
        "email":"[email protected]",
        "phone":"0"},
    "4":{
        "id":"11",
        "country_id":"Bulgaria",
        "VAT_contact_person":"Business development",
        "name":"Mayrhofer Herbert BG",
        "title":"Mag. BG",
        "email":"[email protected] BG",
        "phone":"0"},
    "1":{
        "id":"2",
        "country_id":"Austria",
        "VAT_contact_person":"Technical contact",
        "name":"Mayrhofer Herbert",
        "title":"Mag.",
        "email":"[email protected]",
        "phone":"0"},
    "5":{
        "id":"12",
        "country_id":"Bulgaria",
        "VAT_contact_person":"Technical contact",
        "name":"Mayrhofer Herbert BG",
        "title":"Mag. BG",
        "email":"[email protected] BG",
        "phone":"0"},
    "2":{
        "id":"3",
        "country_id":"Austria",
        "VAT_contact_person":"VAT leader",
        "name":"G\u00fcnther Mayrleitner",
        "title":"Mag",
        "email":"[email protected]",
        "phone":"0"},
    "3":{
        "id":"4",
        "country_id":"Austria",
        "VAT_contact_person":"VAT leader",
        "name":"Ziegler Verena",
        "title":"MA",
        "email":"[email protected]",
        "phone":"0"},
    "6":{
        "id":"13",
        "country_id":"Bulgaria",
        "VAT_contact_person":"VAT leader",
        "name":"G\u00fcnther Mayrleitner BG",
        "title":"Mag BG",
        "email":"[email protected] BG",
        "phone":"0"},
    "7":{
        "id":"14",
        "country_id":"Bulgaria",
        "VAT_contact_person":"VAT leader",
        "name":"Ziegler Verena BG",
        "title":"MA BG",
        "email":"[email protected] BG",
        "phone":"0"
    }
}
', true);

We can then build a new multidimensional array with two taxonomies: namely VAT_contact_person and country_id, by simply inserting each element in the list with those values as keys in the new array.

Like this...

foreach ($data as $v) {
    $finalData[$v["VAT_contact_person"]][$v["country_id"]][] = $v;
}

var_dump($finalData);

So the result:

array(3) {
  ["Business development"]=>
  array(2) {
    ["Austria"]=>
    array(1) {
      [0]=>
      array(7) {
        ["id"]=>
        string(1) "1"
        ["country_id"]=>
        string(7) "Austria"
        ["VAT_contact_person"]=>
        string(20) "Business development"
        ["name"]=>
        string(17) "Mayrhofer Herbert"
        ["title"]=>
        string(4) "Mag."
        ["email"]=>
        string(27) "[email protected]"
        ["phone"]=>
        string(1) "0"
      }
    }
    ["Bulgaria"]=>
    array(1) {
      [0]=>
      array(7) {
        ["id"]=>
        string(2) "11"
        ["country_id"]=>
        string(8) "Bulgaria"
        ["VAT_contact_person"]=>
        string(20) "Business development"
        ["name"]=>
        string(20) "Mayrhofer Herbert BG"
        ["title"]=>
        string(7) "Mag. BG"
        ["email"]=>
        string(30) "[email protected] BG"
        ["phone"]=>
        string(1) "0"
      }
    }
  }
  ["Technical contact"]=>
  array(2) {
    ["Austria"]=>
    array(1) {
      [0]=>
      array(7) {
        ["id"]=>
        string(1) "2"
        ["country_id"]=>
        string(7) "Austria"
        ["VAT_contact_person"]=>
        string(17) "Technical contact"
        ["name"]=>
        string(17) "Mayrhofer Herbert"
        ["title"]=>
        string(4) "Mag."
        ["email"]=>
        string(27) "[email protected]"
        ["phone"]=>
        string(1) "0"
      }
    }
    ["Bulgaria"]=>
    array(1) {
      [0]=>
      array(7) {
        ["id"]=>
        string(2) "12"
        ["country_id"]=>
        string(8) "Bulgaria"
        ["VAT_contact_person"]=>
        string(17) "Technical contact"
        ["name"]=>
        string(20) "Mayrhofer Herbert BG"
        ["title"]=>
        string(7) "Mag. BG"
        ["email"]=>
        string(30) "[email protected] BG"
        ["phone"]=>
        string(1) "0"
      }
    }
  }
  ["VAT leader"]=>
  array(2) {
    ["Austria"]=>
    array(2) {
      [0]=>
      array(7) {
        ["id"]=>
        string(1) "3"
        ["country_id"]=>
        string(7) "Austria"
        ["VAT_contact_person"]=>
        string(10) "VAT leader"
        ["name"]=>
        string(20) "Günther Mayrleitner"
        ["title"]=>
        string(3) "Mag"
        ["email"]=>
        string(30) "[email protected]"
        ["phone"]=>
        string(1) "0"
      }
      [1]=>
      array(7) {
        ["id"]=>
        string(1) "4"
        ["country_id"]=>
        string(7) "Austria"
        ["VAT_contact_person"]=>
        string(10) "VAT leader"
        ["name"]=>
        string(14) "Ziegler Verena"
        ["title"]=>
        string(2) "MA"
        ["email"]=>
        string(24) "[email protected]"
        ["phone"]=>
        string(1) "0"
      }
    }
    ["Bulgaria"]=>
    array(2) {
      [0]=>
      array(7) {
        ["id"]=>
        string(2) "13"
        ["country_id"]=>
        string(8) "Bulgaria"
        ["VAT_contact_person"]=>
        string(10) "VAT leader"
        ["name"]=>
        string(23) "Günther Mayrleitner BG"
        ["title"]=>
        string(6) "Mag BG"
        ["email"]=>
        string(33) "[email protected] BG"
        ["phone"]=>
        string(1) "0"
      }
      [1]=>
      array(7) {
        ["id"]=>
        string(2) "14"
        ["country_id"]=>
        string(8) "Bulgaria"
        ["VAT_contact_person"]=>
        string(10) "VAT leader"
        ["name"]=>
        string(17) "Ziegler Verena BG"
        ["title"]=>
        string(5) "MA BG"
        ["email"]=>
        string(27) "[email protected] BG"
        ["phone"]=>
        string(1) "0"
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

13 Comments

Yup, it's that simple. If you take the array you've already got and run this code on it, it would give you exactly that.
It works flawless BUT the "VAT leader" has multiple values in country. For example, Austria has 2 VAT leader objects... check the "I've got an array that looks like:" object and you'll see there is missing 1 object for each country
The objects with id: 3 and id: 13 in the VAT leader are missing
Of course, you can only have one key with the same name. That's just how arrays work in PHP.
Look, I've edited the answer to more accurately reflect what you're asking. The original question simply alludes to a solution that doesn't make sense.
|

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.