从数组合并json然后重新创建数组

时间:2016-06-10 16:41:56

标签: php arrays json

我有一个数组如下

Array
(
    [photo] => Array
        (
            [0] => {"file_name":"Penguins.jpg", "content_type": "image/jpeg", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699176"}
            [1] => {"file_name":"Penguins.jpg", "sha256" : "7e5bdd023b6cf21efe42a8ec90bc1993fc853980d4b564688e5ac2d28c64223c" , "size" : "777835"}
            [2] => {"file_name":"sample.png", "content_type": "image/png", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699177"}
            [3] => {"file_name":"sample.png", "sha256" : "e6aa1bf1cdb7ca546576cecd61973939be4f1dc8cec3a4f3b49b31d8f60e202f" , "size" : "278383"}
        )

    [submit] => Upload
)

我想按如下方式创建一个数组

Array
(
    [photo] => Array
        (
            [0] => {"file_name":"Penguins.jpg", "content_type": "image/jpeg", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699176" , "sha256" : "7e5bdd023b6cf21efe42a8ec90bc1993fc853980d4b564688e5ac2d28c64223c" , "size" : "777835"}

            [1] => {"file_name":"sample.png", "content_type": "image/png", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699177" , "sha256" : "e6aa1bf1cdb7ca546576cecd61973939be4f1dc8cec3a4f3b49b31d8f60e202f" , "size" : "278383"}
        )

    [submit] => Upload
)

4 个答案:

答案 0 :(得分:2)

这是一个解决方案,它使用array_reduce构建一个带有已解码的JSON对象的地图,并按其 file_name 值键入,然后应用array_map转换其中的对象映射回JSON字符串。

中间array_values将关联数组转换为索引数组:

$data['photo'] = array_map('json_encode', array_values(array_reduce($data['photo'], 
    function($dict, $json) {
        $obj = json_decode($json, true);
        $key = $obj['file_name'];
        if (!isset($dict[$key])) $dict[$key] = [];
        $dict[$key] += $obj; // merging arrays with plus operator
        return $dict;
    }, []))
);

此解决方案不需要两个相关的JSON字符串是连续的。如果数组是任何顺序,它将工作。如果有两个以上的JSON字符串具有相同的file_name属性,它也可以工作,加入那些额外的JSON字符串中的其他任何属性。

eval.in上看到它。

答案 1 :(得分:1)

$array = ['photo'=>
    ['{"file_name":"Penguins.jpg", "content_type": "image/jpeg", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699176"}','{"file_name":"Penguins.jpg", "sha256" : "7e5bdd023b6cf21efe42a8ec90bc1993fc853980d4b564688e5ac2d28c64223c" , "size" : "777835"}','{"file_name":"sample.png", "content_type": "image/png", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699177"}','{"file_name":"sample.png", "sha256" : "e6aa1bf1cdb7ca546576cecd61973939be4f1dc8cec3a4f3b49b31d8f60e202f" , "size" : "278383"}'],

    'submit'=> 'Upload'];

//这是你给定的数组

$result_array = [
    'photos' => [],
    'submit' => 'Upload'
    ];
for($i=0; $i < count($array['photo'])-1; $i++){
    $array_one = json_decode($array['photo'][$i], true);
    $array_two = json_decode($array['photo'][$i++], true);
        array_push($result_array['photos'], array_merge($array_one, $array_two));
}
print_r($result_array);

答案 2 :(得分:1)

<?php

$array = array(
    'photo' => array(
                '0' => '{"file_name":"Penguins.jpg", "content_type": "image/jpeg", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699176"}',
                '1' => '{"file_name":"Penguins.jpg", "sha256" : "7e5bdd023b6cf21efe42a8ec90bc1993fc853980d4b564688e5ac2d28c64223c" , "size" : "777835"}',
                '4' => '{"file_name":"Penguins.jpg", "content_type": "image/jpeg", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699176"}',
                '2' => '{"file_name":"sample.png", "content_type": "image/png", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699177"}',
                '3' => '{"file_name":"sample.png", "sha256" : "e6aa1bf1cdb7ca546576cecd61973939be4f1dc8cec3a4f3b49b31d8f60e202f" , "size" : "278383"}',
            ),
    'submit' => 'Upload'
); // Your array representation

function distinct($item, $key, $array) {
    $file_name = json_decode($item)->file_name; // saving file name
    $find_count = 0; // initializing found count

    foreach($array['photo'] as $i => $json) {
        $json = json_decode($json);

        if($json->file_name == $file_name) {
            if($find_count >= 1) {
                unset($array['photo'][$i]);
            }
            if(property_exists($json, 'content_type')) { // This keep the json with a content type property
                $find_count++;
            }
        }
    }
}

array_walk_recursive($array['photo'], 'distinct', $array);

var_dump($array);

?>

这适用于在sandbox.onlinephpfunctions.com上测试的php 7:

我希望这对你有所帮助,相反,这可能会激励你和其他人。

答案 3 :(得分:1)

您的结构

[
    'photo'  => [
        '0' => '{"file_name":"Penguins.jpg", "content_type": "image/jpeg", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699176"}',
        '1' => '{"file_name":"Penguins.jpg", "sha256" : "7e5bdd023b6cf21efe42a8ec90bc1993fc853980d4b564688e5ac2d28c64223c" , "size" : "777835"}',
        '4' => '{"file_name":"Penguins.jpg", "content_type": "image/jpeg", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699176"}',
        '2' => '{"file_name":"sample.png", "content_type": "image/png", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699177"}',
        '3' => '{"file_name":"sample.png", "sha256" : "e6aa1bf1cdb7ca546576cecd61973939be4f1dc8cec3a4f3b49b31d8f60e202f" , "size" : "278383"}',
    ],
    'submit' => 'Upload'
];

所需输出

[
    [photo] => [
        [0] => {"file_name":"Penguins.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/servergreek.com\/public_html\/www\/imgscript\/tmp\/0048699176","sha256":"7e5bdd023b6cf21efe42a8ec90bc1993fc853980d4b564688e5ac2d28c64223c","size":"777835"}
        [1] => {"file_name":"sample.png","content_type":"image\/png","tmp_path":"\/var\/www\/servergreek.com\/public_html\/www\/imgscript\/tmp\/0048699177","sha256":"e6aa1bf1cdb7ca546576cecd61973939be4f1dc8cec3a4f3b49b31d8f60e202f","size":"278383"}
    ]
    [submit] => Upload
]

代码

    <?php
    $array = getYourArrayStrcuture(); // you don't need this as you already have this array strcuture
    $photoByName = getArrayByFileName($array); // Get array back with file name as key
    $result = convertArrayToJson($photoByName,$array); // convert array to json
    echo "<pre>";
    print_r($result);

    /**
     * Convert array to json
     */
    function convertArrayToJson($photoByName,$array)
    {
        $result = [];
        foreach($photoByName as $key=>$value){
            $result[] = json_encode($value);
        }
        $array['photo'] = $result;
        return $array;
    }

    /**
     * Get array back with file name as key
     */
    function getArrayByFileName($array)
    {
        $photoArray = $array['photo'];
        $photoByName = [];

        foreach ($photoArray as $photo) {
            $itemJsonToArray = json_decode($photo, true);
            if (!isset($photoByName[ $itemJsonToArray['file_name'] ])) {
                $photoByName[ $itemJsonToArray['file_name'] ] = $itemJsonToArray;
            } else {
                $photoByName[ $itemJsonToArray['file_name'] ] = array_merge($photoByName[ $itemJsonToArray['file_name'] ], $itemJsonToArray);
            }
        }

        return $photoByName;
    }

    /**
     * You don't need this as you already have this array structure
     */
    function getYourArrayStrcuture()
    {
        $yourArrayStructure = [
            'photo'  => [
                '0' => '{"file_name":"Penguins.jpg", "content_type": "image/jpeg", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699176"}',
                '1' => '{"file_name":"Penguins.jpg", "sha256" : "7e5bdd023b6cf21efe42a8ec90bc1993fc853980d4b564688e5ac2d28c64223c" , "size" : "777835"}',
                '4' => '{"file_name":"Penguins.jpg", "content_type": "image/jpeg", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699176"}',
                '2' => '{"file_name":"sample.png", "content_type": "image/png", "tmp_path": "/var/www/servergreek.com/public_html/www/imgscript/tmp/0048699177"}',
                '3' => '{"file_name":"sample.png", "sha256" : "e6aa1bf1cdb7ca546576cecd61973939be4f1dc8cec3a4f3b49b31d8f60e202f" , "size" : "278383"}',
            ],
            'submit' => 'Upload'
        ];

        return $yourArrayStructure;
    }
?>