Gvice
Gvice

Reputation: 391

Getting data from multidimensional array

Array
(
[1] => Array
    (
        [0] => purple.txt
        [1] => pinky.txt
        [2] => original stuff.txt
    )

[2] => Array
    (
        [0] => purp1.txt
        [1] => pink1.txt
        [2] => original stuff1.txt
        [3] => increment1.txt
    )

)
Array
(
[1] => Array
    (
        [0] => C:\wamp\tmp\php6395.tmp
        [1] => C:\wamp\tmp\php63A6.tmp
        [2] => C:\wamp\tmp\php63A7.tmp
    )

[2] => Array
    (
        [0] => C:\wamp\tmp\php63A8.tmp
        [1] => C:\wamp\tmp\php63A9.tmp
        [2] => C:\wamp\tmp\php63AA.tmp
        [3] => C:\wamp\tmp\php63AB.tmp
    )

I would like to match the array keys with the proper array keys from the second array for example I would like to be able get the file name

each

[1] => Array
[2] => Array

is a new row and I would like to eventually save all the file names from that row in mysql so i can call the links....

[1] => Array
    (
        [0] => purple.txt

and match it with

[1] => Array
    (
        [0] => C:\wamp\tmp\php6395.tmp

so I can use them together for file storage and so on......

The following php is

if(isset($_FILES['file'])=== true){
$files = $_FILES['file']['name'];
$files_tmp = $_FILES['file']['tmp_name'];

echo '<pre>';
print_r ($files);
echo '<pre>';

echo '<pre>';
print_r ($files_tmp);
echo '<pre>';

Upvotes: 0

Views: 143

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173662

This is how you would iterate over multiple uploaded files. Assuming you have these input element names:

file[0][0]
file[0][1]
file[1][0]
...

foreach ($_FILES['file']['name'] as $row => $rowfiles) {
    foreach ($rowfiles as $index => $name) {
        $tmp_name = $_FILES['file']['tmp_name'][$row][$index];

        // do stuff with $name and $tmp_name
    }
}

Upvotes: 1

rlatief
rlatief

Reputation: 723

Something like this?

$rows = array();

foreach( $first_array as $key => $value )
{
    foreach( $value as $k => $v )
    {
        $rows[] = array( 'filename' => $v, 'path' => $second_array[$key][$k] );
    }
}

echo '<pre>' . print_r( $rows, true ) . '</pre>';

Upvotes: 1

Related Questions