Stark
Stark

Reputation: 593

How to combine two arrays by using a key?

There are plenty of answers around but I just can't seem to get this right, at all, the post seems big but it just seems. Here is what I have, and what I've tried.

Array
(
    [0] => image 1
    [1] => image 2
)

Array
(
    [name] => Array
        (
            [0] => 0.14997300-1503597010599f11d2249df30.jpg
            [1] => 0.24654000-150113339659797a543c31f24.jpg
        )

    [type] => Array
        (
            [0] => image/jpeg
            [1] => image/jpeg
        )

    [tmp_name] => Array
        (
            [0] => C:\Users\--\AppData\Local\Temp\php509E.tmp
            [1] => C:\Users\--\AppData\Local\Temp\php509F.tmp
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 55560
            [1] => 9425
        )

)

I've tried:

$instructions = $_POST['instructions'];
$image = $_FILES['instructions_image'];

$result = array();
foreach($instructions as $index => $key){
    $t = array();
    foreach($image as $img){
        $t[] = $img;
    }
    $result[$key] = $t;
}

And the results is:

Array
(
    [image 1] => Array
        (
            [0] => Array
                (
                    [0] => 0.14997300 1503597010599f11d2249df30.jpg
                    [1] => 0.24654000 150113339659797a543c31f24.jpg
                )

            [1] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )

            [2] => Array
                (
                    [0] => C:\Users\--\AppData\Local\Temp\phpBAD5.tmp
                    [1] => C:\Users\--\AppData\Local\Temp\phpBAE6.tmp
                )

            [3] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [4] => Array
                (
                    [0] => 55560
                    [1] => 9425
                )

        )

    [image 2] => Array
        (
            [0] => Array
                (
                    [0] => 0.14997300 1503597010599f11d2249df30.jpg
                    [1] => 0.24654000 150113339659797a543c31f24.jpg
                )

            [1] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )

            [2] => Array
                (
                    [0] => C:\Users\--\AppData\Local\Temp\phpBAD5.tmp
                    [1] => C:\Users\--\AppData\Local\Temp\phpBAE6.tmp
                )

            [3] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [4] => Array
                (
                    [0] => 55560
                    [1] => 9425
                )

        )

)

I'm unsure why the results have 2 of the same in both indexes, but I was also wanting to know how we could keep the array key names provided by the $_FILES, ex: name, type, tmp_name, error and size.

Here is what I was expecting(I included additional information like the key names that I didn't provide nor did with my code, sorry about that, been at it for 12 hours non stop but just any explanation to set me on the right path would help me tremendously):

Array
(
    [0] => Array
        (
            [text] => image 1,
            [image_data] => Array (
                [name] => 0.14997300 1503597010599f11d2249df30.jpg
                [type] => image/jpeg
                [tmp_name] => C:\Users\--\AppData\Local\Temp\php509E.tmp
                [error] => 0
                [size] => 55560
            )

        )

    [1] => Array
        (
            [text] => image 2,
            [image_data] => Array (
                [name] => 0.24654000 150113339659797a543c31f24.jpg
                [type] => image/jpeg
                [tmp_name] => C:\Users\--\AppData\Local\Temp\php509E.tmp
                [error] => 0
                [size] => 9425
            )

        )

)

Upvotes: 1

Views: 73

Answers (4)

Sahil Gulati
Sahil Gulati

Reputation: 15141

Hope this one will be helpful. Here we are using simple foreach loop array_keys, array_combine and array_column

Try this code snippet here

$result=array();
foreach($images as $key => $image)
{
    $result[]=array(
        "text"=>$image,
        "image_data"=>array_combine(
                array_keys($instructions),
                array_column($instructions,$key))
    );
}
print_r($result);

Upvotes: 1

John Baker
John Baker

Reputation: 435

Try this

$fileData = array(
    'name' => ['0.14997300-1503597010599f11d2249df30.jpg','0.24654000-150113339659797a543c31f24.jpg'],
    'type' => ['image/jpeg','image/jpeg'],
    'tmp_name' => ['C:\Users\--\AppData\Local\Temp\php509E.tmp','C:\Users\--\AppData\Local\Temp\php509F.tmp'],
    'error' => [0,0],
    'size' => [55560,9425]);

$someArr = array('image 1','image 2');
$fileData['somedata'] = $someArr;


function reFileData($fileData) { 
    $arr = array();
    $keys = array_keys($fileData);
    for ($i=0; $i < count($fileData['name']); $i++) {
        foreach ($keys as $key) {
            $arr[$i][$key] = $fileData[$key][$i];
        }
    }
    return $arr;
}

print_r(reFileData($fileData));

Upvotes: 0

Lavanya E
Lavanya E

Reputation: 216

This Will help you $res=array(0 => 'image 1',1 => 'image 2');

            $valu=array('name' => array(0=> '0.14997300-1503597010599f11d2249df30.jpg',1 => '0.24654000-150113339659797a543c31f24.jpg'
                    ),'type' => array( 0 => 'image/jpeg',1 => 'image/jpeg'), 'tmp_name'=> array(0 => 'C:\Users\--\AppData\Local\Temp\php509E.tmp',1 => 'C:\Users\--\AppData\Local\Temp\php509F.tmp'),'error' => array(0 => 0,1 => 0),'size' => array(0 => 55560, 1 => 9425)
                    );

            $newarr=array();
            foreach($res as $key=>$val)
            {
                $newarr[$key]['text']=$val;
                $newarr[$key]['image_data']=array('name'=>$valu['name'][$key],'type'=>$valu['type'][$key],'tmp_name'=>$valu['tmp_name'][$key],'error'=>$valu['error'][$key],'size'=>$valu['size'][$key]);
            }
            echo '<pre>';
            print_r($newarr);

Upvotes: 0

Neodan
Neodan

Reputation: 5252

You need to write a custom script which combines these arrays by your logic. For this task you can use this array functions: array_combine, array_keys, array_column.

Example:

<?php
$a1 = ['image 1', 'image 2'];
$a2 = [
    'name' => ['0.14997300-1503597010599f11d2249df30.jpg', '0.24654000-150113339659797a543c31f24.jpg'],
    'type' => ['image/jpeg', 'image/jpeg'],
    'tmp_name' => ['C:\Users\--\AppData\Local\Temp\php509E.tmp', 'C:\Users\--\AppData\Local\Temp\php509F.tmp'],
    'error' => [0, 0],
    'size' => [55560, 9425]
];

$result = [];
foreach ($a1 as $k => $v) {
    $result[] = [
        'text' => $v,
        'image_data' => array_combine(array_keys($a2), array_column($a2, $k))
    ];
}

print_r($result);

Upvotes: 2

Related Questions