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';
}
1
  • Is it me or you have no query in your second foreach? Commented Feb 18, 2016 at 21:00

2 Answers 2

1

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

Take a look at this explanation

Sign up to request clarification or add additional context in comments.

Comments

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';
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.