ytse_jam
ytse_jam

Reputation: 185

Create an associative 2d array from a flat array of keys and a static associative array

I need to create a 2d array using one array's values as keys as well as a new column value, and each row should have several associative elements provided by another flat associative array.

stores_array:

Array
(
    [0] => store1
    [1] =>store2
)  

items_array:

Array  
(  
  [electronics]=>led tv  
  [cosmetics]=>eyeliner  
  [fruits]=>apple 
  [vegetables]=>cabbage  
)  

Here is what I have so far:

$new_array = array();
foreach($stores_array as $t) {
    $new_array[$t] = $items_array;
}
  
echo '<pre>';
    print_r($new_array);  
echo '<pre/>';  

Here is the current output:

Array
(
[store1] => Array
    (
      [electronics]=>led tv  
      [cosmetics]=>eyeliner  
      [fruits]=>apple 
      [vegetables]=>cabbage  
    )  
[store2] => Array
    (
      [electronics]=>led tv  
      [cosmetics]=>eyeliner  
      [fruits]=>apple 
      [vegetables]=>cabbage  
    )
)  

What I want to achieve is to add some other values in each of the array rows. See the arrows regarding what/where I intend to add data.

Array
(
[store1] => Array
    (
      [electronics]=>led tv  
      [cosmetics]=>eyeliner  
      [fruits]=>apple 
      [vegetables]=>cabbage
      [store]=>store1  <------- how can I add these?
    )  
[store2] => Array
    (
      [electronics]=>led tv  
      [cosmetics]=>eyeliner  
      [fruits]=>apple 
      [vegetables]=>cabbage 
      [store]=>store2  <------- how can I add these?
    )
)  

Upvotes: 0

Views: 138

Answers (6)

mickmackusa
mickmackusa

Reputation: 47873

For a functional-style approach, use array_reduce() to declare associative keys while populating rows from static and dynamic data. Demo

var_export(
    array_reduce(
        $stores,
        fn($result, $s) => $result + [$s => $items + ['store' => $s]],
        []
    )
);

Output:

array (
  'store1' => 
  array (
    'electronics' => 'led tv',
    'cosmetics' => 'eyeliner',
    'fruits' => 'apple',
    'vegetables' => 'cabbage',
    'store' => 'store1',
  ),
  'store2' => 
  array (
    'electronics' => 'led tv',
    'cosmetics' => 'eyeliner',
    'fruits' => 'apple',
    'vegetables' => 'cabbage',
    'store' => 'store2',
  ),
)

Upvotes: 0

Hendyanto
Hendyanto

Reputation: 335

As Marc B said, it is sort of redundant data, but here is my answer:

$new_array = array();

foreach($stores_array as $t) {
    $new_array[$t] = $items_array;
    $new_array[$t]['store'] = $t;
}

Upvotes: 0

Alvaro
Alvaro

Reputation: 41595

You just have to add the value at the end in the array.

  $new_array = array();

  foreach($stores_array as $t) {
      $items_array[] = $t;
      $new_array[$t] = $items_array;
  }

Anyway, if you just want to retrieve the key somehow, you dont need to store it anywhere, you can always access to it using key($array)

In your case:

foreach($new_array as $data){
    echo key($data);
}

You can find more information about the key function at PHP documentation.

Upvotes: 0

Aaron Saray
Aaron Saray

Reputation: 1177

This should be relatively easy, if I understand the problem. Try this:

$new_array = array();
foreach($stores_array as $t) {
    $new_array[$t] = $items_array;
    $new_array['store'] = $t;
}

Upvotes: 0

Passerby
Passerby

Reputation: 10070

Try this:

$new_array = array();
foreach($stores_array as $t) {
    $new_array[$t] = $items_array;
    $new_array[$t]["store"]=$t;
}

echo '<pre>';
print_r($new_array);  
echo '<pre/>';

Upvotes: 1

Waleed Khan
Waleed Khan

Reputation: 11467

foreach (array_keys($my_array) as $key) {
    $my_array[$key]['store'] = $key;
}

Upvotes: 0

Related Questions