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

时间:2017-08-31 07:30:21

标签: php arrays sorting

周围有很多答案,但我似乎无法做到这一点,根本没有,这个帖子似乎很大但似乎只是。这就是我所拥有的,以及我所尝试过的。

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
        )

)

我试过了:

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

结果是:

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
                )

        )

)

我不确定为什么两个索引中的结果都有两个相同,但我也想知道如何保存$ _FILES提供的数组键名,例如:name,type,tmp_name,错误和大小。

这就是我所期待的(我提供了其他信息,例如我没有提供的密钥名称,也没有使用我的代码,对不起,已经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 个答案:

答案 0 :(得分:2)

您需要编写一个自定义脚本,它根据您的逻辑组合这些数组。对于此任务,您可以使用此数组函数:array_combinearray_keysarray_column

示例:

RewriteRule ^pages\/(.+)\.html$ page.php?id=$1 [L,NC]
Redirect 301 /pages/features.html /pages/ost-to-pst-ost-to-office365-ost-to-exchange-migrator.‌html?

答案 1 :(得分:1)

希望这个会有所帮助。我们在这里使用简单的foreach循环array_keysarray_combinearray_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 :(得分:0)

这会对你有所帮助     $ 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);

答案 3 :(得分:0)

试试这个

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