将数组添加到关联数组

I'm trying to add an array to an associative array.

$contents = glob(DIR_IMAGE."catalog/Data_PDF/*");

$contents_pregged['name'] = preg_replace("(".DIR_IMAGE.")", "", $contents); 
$contents_pregged['link'] = preg_replace("(catalog/Data_PDF/)", "", $contents_pregged['name']); 

$data['contents'][] = array(
    'name' => $contents_pregged['name'],
    'link' => $contents_pregged['link']
);

The above example is within the controller and if i send the data alone it echos out all the files listed but i would like to use the arrays in an associative array so i can echo out the name and links using the example below.

{% for content in contents %} 
      <li><a href = "/image/{{content.link}}">{{content.name}}</a></li>
{% endfor %}

The responsive i get on the front end is Array

I have at the moment 3 files in the directory so the array should return 3 values

glob() returns an array.

It will be easier if you would first iterate over the files you get from glob(). And you don't really need regular expressions to extract the file name parts:

foreach($contents as $file) {
    $data['contents'][] = [
        "name" => str_replace(DIR_IMAGE, "", $file),
        "link" => basename($file)
    ];
}

This happens because both $contents_pregged['name'] and $contents_pregged['link'] are arrays, because $contents is array also.

So you need somehow iterate over your $contents_pregged so as in $data['contents'] you will have data which you need. A simple solution can be:

foreach ($contents_pregged['name'] as $k => $v) {
    $data['contents'][] = array(
        'name' => $v,
        // here we get value of `$contents_pregged['link']` with same key
        'link' => $contents_pregged['link'][$k],
    );
}

After that you can iterate over $contents in a template as you want.