twmk
twmk

Reputation: 1

Best way to modify an multidimensional array in PHP

There are many ways to modify an array but which is the best way to modify the following multidimensional array?

This array contain two other arrays, category 1 and 2.

$arr = array(
        'category 1' => array(
                        'item 1' => 'lorem',
                        'item 2' => 'lorem',
                        'item 3' => 'lorem',
                        'item 4' => 'lorem',
                        ),

        'category 2' => array(
                        'item 1' => 'lorem',
                        'item 2' => 'lorem',
                        'item 3' => 'lorem',
                        'item 4' => 'lorem',
                        ),
);

If I var_dump the array it will look like this.

array(2) {
  ["category 1"]=>
  array(4) {
    ["item 1"]=>
    string(5) "lorem"
    ["item 2"]=>
    string(5) "lorem"
    ["item 3"]=>
    string(5) "lorem"
    ["item 4"]=>
    string(5) "lorem"
  }
  ["category 2"]=>
  array(4) {
    ["item 1"]=>
    string(5) "lorem"
    ["item 2"]=>
    string(5) "lorem"
    ["item 3"]=>
    string(5) "lorem"
    ["item 4"]=>
    string(5) "lorem"
  }
}

So, what is the best way to add [category 3] and [subcategory 1], so it looks like below.

array(2) {
  ["category 1"]=>
  array(4) {
    ["item 1"]=>
    string(5) "lorem"
    ["item 2"]=>
    string(5) "lorem"
    ["item 3"]=>
    string(5) "lorem"
    ["item 4"]=>
    string(5) "lorem"
  }
  ["category 2"]=>
  array(4) {
    ["item 1"]=>
    string(5) "lorem"
    ["item 2"]=>
    string(5) "lorem"
    ["item 3"]=>
    string(5) "lorem"
    ["item 4"]=>
    string(5) "lorem"
        ["subcategory 1"]=>
        array(2) {
        ["item 1"]=>
        string(5) "lorem"
        ["item 2"]=>
        string(5) "lorem"
  }
  ["category 3"]=>
  array(4) {
    ["item 1"]=>
    string(5) "lorem"
    ["item 2"]=>
    string(5) "lorem"
    ["item 3"]=>
    string(5) "lorem"
    ["item 4"]=>
    string(5) "lorem"

  }
}

Upvotes: 0

Views: 42

Answers (2)

Mario Catillo
Mario Catillo

Reputation: 155

Generate the monodimensional arrays you need to add and simply add them to the first array:

$array['category_2']['subcategory_1'] = $subcategory_1_array;
$array['category_3'] = $category_3_array;

Upvotes: 1

HumbleWebDev
HumbleWebDev

Reputation: 575

I don't think there is one catch-all "best" way but for your specific case, this would suffice:

$arr = array(
    'category 1' => array(
                    'item 1' => 'lorem',
                    'item 2' => 'lorem',
                    'item 3' => 'lorem',
                    'item 4' => 'lorem',
                    ),

    'category 2' => array(
                    'item 1' => 'lorem',
                    'item 2' => 'lorem',
                    'item 3' => 'lorem',
                    'item 4' => 'lorem',
                    ),
);
$arr['category 3'] = array();

for($i = 1; $i < 5; ++$i){
    $arr['category 3']["item $i"] = 'lorem';
}

Upvotes: 0

Related Questions