如何使用密钥组合两个数组?

时间:2022-05-02 08:24:21

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.

我不确定为什么结果在两个索引中都有两个相同,但我也想知道如何保持$ _FILES提供的数组键名,例如:name,type,tmp_name,error和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):

这就是我所期待的(我包含了其他信息,比如我没有提供的密钥名称,也没有我的代码,对不起,已经12小时不停,但只是任何解释让我走上正确的道路会帮助我极大的):

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
            )

        )

)

4 个解决方案

#1


1  

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

希望这个会有所帮助。这里我们使用简单的foreach循环array_keys,array_combine和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);

#2


2  

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.

您需要编写一个自定义脚本,它根据您的逻辑组合这些数组。对于此任务,您可以使用此数组函数: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);

#3


0  

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

这将帮助你$ 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);

#4


0  

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));

#1


1  

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

希望这个会有所帮助。这里我们使用简单的foreach循环array_keys,array_combine和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);

#2


2  

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.

您需要编写一个自定义脚本,它根据您的逻辑组合这些数组。对于此任务,您可以使用此数组函数: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);

#3


0  

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

这将帮助你$ 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);

#4


0  

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));