Title: var_export with multiple variables of the same name
Tags: php
Question: If I have this, simplified:

```<?php 
$image='henry.jpg'; 
$name='henry'; 
echo '<img src="'.$image.'"/&gt;<p&gt;'.$name.'</p&gt;; 
?&gt;

Some other code

<?php 
$image='walter.jpg'; 
$name='walter'; 
echo '<img src="'.$image.'"/&gt;<p&gt;'.$name.'</p&gt;; 
?&gt;
```

I want to convert ```$image``` and ```$name``` into an associative array. Using ```$arr = compact('image', 'name');``` and ```var_export``` only creates an array for walter thusly 

```array ( 'image' =&gt; 'walter.jpg', 'name' =&gt; 'walter', )```. 

Desired output should be 

```array ( 
'image' =&gt; 'henry.jpg', 'name' =&gt; 'henry', 
'image' =&gt; 'walter.jpg', 'name' =&gt; 'walter', 
)```

How do I achieved this? I have tried ```foreach``` but it only duplicates ```walter```.


Here is the accepted answer: You can't achieve the exact result you want as that would require having an array with multiple identical keys. However you can generate an array of arrays instead by changing your code slightly:

```$image='henry.jpg'; 
$name='henry'; 
$arr[] = compact('image', 'name');

$image='walter.jpg'; 
$name='walter'; 
$arr[] = compact('image', 'name');

print_r($arr);
```

Output:

```Array (
  [0] =&gt; Array (
     [image] =&gt; henry.jpg
     [name] =&gt; henry
  )
  [1] =&gt; Array (
     [image] =&gt; walter.jpg
     [name] =&gt; walter
  ) 
)
```

Demo on 3v4l.org

Comment for this answer: All right, I can work with that. Thank you for the knowledge. It's certainly a time-saver.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:05/22/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php>', 'Title': 'var_export with multiple variables of the same name', 'ViewCount': 114, 'id': 56248013, 'user_AboutMe': None, 'user_AccountId': 15986271.0, 'user_DisplayName': 'albert7838', 'user_Reputation': 19.0, 'user_Views': 4.0, 'user_id': 11536644.0}}