1

I have an array containing strings of URLs, however some of the array elements are also an array. So some might be http://google.com, others might be:

Array([0] => http://yahoo.com [1] => http://msn.com)

I am trying to add a list of all these urls to a database. However, I need the list to expand for the multi-dimensional array. i.e. the list I am trying to establish should look like:

http://google.com
http://yahoo.com
http://msn.com

So I was initially doing this through a foreach, and then I thought I could expand the multi-dimensional arrays with a foreach within a foreach, but not sure that works. This is what I have currently, but doesnt work.

foreach($allfiles as $file) {
    if(is_array($file)) {
        $files = $file {
            foreach($files as $file)
        }
    }
    $qry2 = mysqli_query($con,"INSERT INTO files(name) VALUES($file)");
    echo '<br>'.$file;
    if($qry2) echo ' -success';
    else echo ' -error';
}
Fuzzy
  • 3,810
  • 2
  • 15
  • 33
Jack Queen
  • 214
  • 1
  • 7

2 Answers2

1

You can make a recursive function to dig into your array.

Take a look at this explanation

Community
  • 1
  • 1
Michael
  • 1,063
  • 14
  • 32
0
function recursiveInsert($allfiles) {
    foreach($allfiles as $file) {
        if(is_array($file)) {
            recursiveInsert($file);
            continue;
        }
        $qry2 = mysqli_query($con,"INSERT INTO files(name) VALUES($file)");
        echo '<br>'.$file;
        if($qry2) echo ' -success';
        else echo ' -error';
    }
}
Nick
  • 9,735
  • 7
  • 59
  • 89