0

I have a problem in converting multi-dimensional array to flat array I have an input array like this:

Array
(
    [0] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionCombine
            [aggregator] => all
            [conditions] => Array
                (
                    [0] => Array
                        (
                            [type] => MagentoCatalogRuleModelRuleConditionProduct
                            [attribute] => category_ids
                        )
                )
        )

    [1] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionProduct
            [attribute] => category_ids
        )

    [2] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionProduct
            [attribute] => attribute_set_id
        )
)

and I want the output is an array like this, the child array will be flattened as parent array level and it will be recognized by the index:

Array
(
    [1--1] => Array
        (
            [type] => Magento\CatalogRule\Model\Rule\Condition\Combine
            [aggregator] => all
        )

    [1--1--1] => Array
        (
            [type] => Magento\CatalogRule\Model\Rule\Condition\Product
            [attribute] => category_ids
        )

    [1--2] => Array
        (
            [type] => Magento\CatalogRule\Model\Rule\Condition\Product
            [attribute] => category_ids
        )

    [1--3] => Array
        (
            [type] => Magento\CatalogRule\Model\Rule\Condition\Product
            [attribute] => attribute_set_id
        )
)

the input array may contain many child layers and the output array must be index in that format. I'm stucking to write this logic into code. Thank you!

Tùng Nguyễn
  • 103
  • 1
  • 14

1 Answers1

1

You can use a recursive function like this:

function flatten($array, $parent_key = '1') {
  $flattened_array = [];
  foreach ($array as $key => $item) {
    $tmp = $item;
    unset($tmp['conditions']);
    $child_key = $parent_key . '--' . strval($key + 1);
    $flattened_array[$child_key] = $tmp;
    if (isset($item['conditions'])) {
      $flattened_array = array_merge($flattened_array, flatten($item['conditions'], $child_key));
    }
  }
  return $flattened_array;
}

$input = [
  0 => [
    'type' => 'MagentoCatalogRuleModelRuleConditionCombine',
    'aggregator' => 'all',
    'conditions' => [
      0 => [
        'type' => 'MagentoCatalogRuleModelRuleConditionProduct',
        'attribute' => 'category_ids'
      ]
    ]
  ],

  1 => [
    'type' => 'MagentoCatalogRuleModelRuleConditionProduct',
    'attribute' => 'category_ids'
  ],

  2 => [
    'type' => 'MagentoCatalogRuleModelRuleConditionProduct',
    'attribute' => 'attribute_set_id'
  ]
];

print_r(flatten($input));

Output:

Array
(
    [1--1] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionCombine
            [aggregator] => all
        )

    [1--1--1] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionProduct
            [attribute] => category_ids
        )

    [1--2] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionProduct
            [attribute] => category_ids
        )

    [1--3] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionProduct
            [attribute] => attribute_set_id
        )

)
Kien Nguyen
  • 2,616
  • 2
  • 7
  • 24