0

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
2
  • What have you tried so far? I think you may just loop the array with keys to assign a value to it. You may better check if the key by isset and early break if there is any error. Commented Jan 6, 2022 at 15:00
  • Can you give me an example how to do it with a loop and assign the value to it? Commented Jan 6, 2022 at 15:10

1 Answer 1

1

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

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

1 Comment

thank you this solved the problem for me!

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.