Home > OS >  Create element on a nested multidimensional array with the keys based on a another array
Create element on a nested multidimensional array with the keys based on a another array

Time:01-07

Im trying to add a small array to a multidimensional array without knowing the keys for the multidimensional array

I have an array that look like this:

array:1 [▼
  3 => array:3 [▼
    "attribute_code" => "category"
    "attribute_id" => 3
    "attribute_value" => array:1 [▼
      4 => array:3 [▼
        "attribute_code" => "hardware"
        "attribute_id" => 4
        "attribute_value" => array:1 [▼
          5 => array:3 [▼
            "attribute_code" => "harddisk"
            "attribute_id" => 5
            "attribute_value" => array:1 [▼
              6 => array:7 [▼
                "attribute_code" => "small"
                "attribute_id" => 6
                "attribute_value" => "132gb"
                "attribute_value_id" => 3
                "attribute_parent_id" => 5
                "attribute_value_is_array" => false
                "attribute_value_is_multidimensional_array" => false
              ]
            ]
          ]
        ]
      ]
    ]
  ]
]

I have an another array:

array:1 [▼
  7 => array:6 [▼
    "attribute_code" => "medium"
    "attribute_id" => 7
    "attribute_value" => "264gb"
    "attribute_value_id" => 4
    "attribute_value_is_array" => false
    "attribute_value_is_multidimensional_array" => false
  ]
]

basically what im trying to do is to add the smaller array to the "harddisk" array so the result should look like this:

array:1 [▼
  3 => array:3 [▼
    "attribute_code" => "category"
    "attribute_id" => 3
    "attribute_value" => array:1 [▼
      4 => array:3 [▼
        "attribute_code" => "hardware"
        "attribute_id" => 4
        "attribute_value" => array:1 [▼
          5 => array:3 [▼
            "attribute_code" => "harddisk"
            "attribute_id" => 5
            "attribute_value" => array:1 [▼
              6 => array:7 [▼
                "attribute_code" => "small"
                "attribute_id" => 6
                "attribute_value" => "132gb"
                "attribute_value_id" => 3
                "attribute_parent_id" => 5
                "attribute_value_is_array" => false
                "attribute_value_is_multidimensional_array" => false
              ],
              7 => array:6 [▼
                "attribute_code" => "medium"
                "attribute_id" => 7
                "attribute_value" => "264gb"
                "attribute_value_id" => 4
                "attribute_value_is_array" => false
                "attribute_value_is_multidimensional_array" => false
              ]
            ]
          ]
        ]
      ]
    ]
  ]
]

My problem is that i want to add the smaller array dynamically based on an another array with keys

array:6 [▼
  0 => 3
  1 => "attribute_value"
  2 => 4
  3 => "attribute_value"
  4 => 5
  5 => "attribute_value"
]

so i try to build dynamically something like this:

$array[3]['attribute_value'][4]['attribute_value'][5]['attribute_value'] = $smallArray

CodePudding user response:

Recursive solution that modifies the existing array.

function appendToValues(&$array, $value)
{
    foreach ($array as &$item) {
        if ($item['attribute_code'] === 'harddisk') {
            $item['attribute_value'][] = $value;
        } elseif(is_array($item['attribute_value'])) {
            appendToValues($item['attribute_value'], $value);
        }
    }
}

appendToValues($array, ['attribute_code' => 'medium', 'attribute_value' => '256gb']);

Sample

  •  Tags:  
  • Related