0

I had it in an array and it worked fine now I want to insert it in a Multidimensional Array.

This is my code, not sure what I am doing or if even its poasable,

I want that the getFilename() should be assigned to a key,

Thanks for any help!

     $array_name = 1;

    $fileSystemIterator = new FilesystemIterator('work/');
    
    $someArray = array();
    foreach ($fileSystemIterator as $fileInfo){
       $array_name = array(
            'alt' => "some text",
            'link_to' => "some text",
            'img_url' => "$fileInfo->getFilename()"
        ),
$array_name++
}
        )
Peneh
  • 195
  • 1
  • 10

1 Answers1

1

Check your double quotes in your array value for img_url.

PS: Notice I also changed the comma to a semicolon after your array.

(and... ident your code!)

$fileSystemIterator = new FilesystemIterator('work/');

$someArray = array();
$array_name = array();
foreach ($fileSystemIterator as $fileInfo) {
    $array_name[] = array(
    'alt' => "some text",
    'link_to' => "some text",
    'img_url' => $fileInfo->getFilename()
    );
}
print_r($array_name);
Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36
  • How can I rename every array with another name so PHP displays all arrays? – Peneh Jul 20 '21 at 22:14
  • Your question is unclear. Please be more specific what you're trying to do and also what you have tried as per SO guidelines. – Oliver M Grech Jul 20 '21 at 22:57
  • I think I understood what you need, I edited the code. Check it out. print_r is just to show you the contents of $array_name. Good luck – Oliver M Grech Jul 20 '21 at 22:59
  • Thanks, I accepted your answer, can you Please explain how it works. – Peneh Jul 20 '21 at 23:20
  • 1
    Thanks and sure.. so $array_name is an array, $array_name++ would only increment $array_name + 1 if it was an integer not an array, so the ++ doesn't do any good for arrays. So... $array_name[] will create a new array item dynamically, this means that $array_name is now a 2 dimensional array. The first key would be numerical, and the second dimension an associative array. Example you can can echo $array_name[1]['alt']; or better if you foreach($array_name as $arr) { echo $arr['alt']; } – Oliver M Grech Jul 21 '21 at 17:52