如何细化另一个数组中关联数组数据的值

时间:2017-04-19 06:56:07

标签: php arrays

我在PHP中编写了以下代码

include '../../cloudinary/Cloudinary.php';
include '../../cloudinary/Uploader.php';

\Cloudinary::config(array
(
    "cloud_name" => "cloud_name",
    "api_key" => "api_key",
    "api_secret" => "some_key"
));

foreach ($_FILES["img"]["tmp_name"] as $array_number => $holding_value)
{
    $date =  getTime();
    $tmp_img_name = $_SESSION["username"].$count.$date["timestamp"]."."."jpg";

    //store image to temporary location
    if(move_uploaded_file($holding_value, $temp."/".$tmp_img_name))
        {
            $mainimage[] = \Cloudinary\Uploader::upload($temp."/".$tmp_img_name, $options = array('folder' => 'original'));

            for($i=0; $i<3; $i++)
                { 
                    //read image
                    $resize = new ResizeImage($temp."/".$tmp_img_name);
                    //resize to desire format
                    $resize->resizeTo($image_widths[$i], $image_heights[$i], 'exact');
                    //store after resizing
                    $resize->saveImage($store_paths[$i]."/".$tmp_img_name);

                    if($i == 0)
                        {
                            $thumb1[] = \Cloudinary\Uploader::upload($store_paths[$i]."/".$tmp_img_name,  $options = array('folder' => 'thumbnail_100x141'));
                            unlink($store_paths[$i]."/".$tmp_img_name);
                        }

                    if($i == 1)
                        {
                            $thumb2[] = \Cloudinary\Uploader::upload($store_paths[$i]."/".$tmp_img_name, $options = array('folder' => 'average_300x424'));
                            unlink($store_paths[$i]."/".$tmp_img_name);
                        }

                    if($i == 2)
                        {
                            $thumb3[] = \Cloudinary\Uploader::upload($store_paths[$i]."/".$tmp_img_name, $options = array('folder' => 'original_compressed_700x990'));
                            unlink($store_paths[$i]."/".$tmp_img_name);
                        }
                }
        }

    $count ++;
    unlink($temp."/".$tmp_img_name);
}

执行以下print_r()个变量=&gt; $mainimage$thumb1$thumb2$thumb3我获得以下输出

//主要原始图像数组

Array
(
[0] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583096
        [signature] => some_signature
        [width] => 461
        [height] => 358
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:24:56Z
        [tags] => Array
            (
            )

        [bytes] => 25954
        [type] => upload
        [etag] => 589bded2f63f2643503074989ec74dc6
        [url] => some_url
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

[1] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583115
        [signature] => some_signature
        [width] => 320
        [height] => 320
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:25:15Z
        [tags] => Array
            (
            )

        [bytes] => 15940
        [type] => upload
        [etag] => 9beb81ac72ace5e176d80086b97eeb13
        [url] => some_url
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

[2] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583134
        [signature] => some_signature
        [width] => 531
        [height] => 315
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:25:34Z
        [tags] => Array
            (
            )

        [bytes] => 18449
        [type] => upload
        [etag] => 0b8f33226b89b26655d00d7b8d04971e
        [url] => some_url
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

// thumb1 image array

Array
(
[0] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583099
        [signature] => some_signature
        [width] => 100
        [height] => 141
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:24:59Z
        [tags] => Array
            (
            )

        [bytes] => 14101
        [type] => upload
        [etag] => 1f13b4f8b152a3015700b5e1a8cd0c7c
        [url] => some_url
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

[1] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583119
        [signature] => some_signature
        [width] => 100
        [height] => 141
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:25:19Z
        [tags] => Array
            (
            )

        [bytes] => 14050
        [type] => upload
        [etag] => 816dec6dd5b611c8f85e25e8172b3092
        [url] => some_url
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

[2] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583138
        [signature] => some_signature
        [width] => 100
        [height] => 141
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:25:38Z
        [tags] => Array
            (
            )

        [bytes] => 11379
        [type] => upload
        [etag] => 98f9228b60c2f2bf9734e1c6ae729e47
        [url] => some_url
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

// thumb2 image array

Array
(
[0] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583104
        [signature] => some_signature
        [width] => 300
        [height] => 424
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:25:04Z
        [tags] => Array
            (
            )

        [bytes] => 77276
        [type] => upload
        [etag] => 26dc8f2d766ca830292d13612688e264
        [url] => some_url
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

[1] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583123
        [signature] => some_signature
        [width] => 300
        [height] => 424
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:25:23Z
        [tags] => Array
            (
            )

        [bytes] => 73831
        [type] => upload
        [etag] => f0fc538ba3afaabcb72fda891361834a
        [url] => some_url
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

[2] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583143
        [signature] => some_signature
        [width] => 300
        [height] => 424
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:25:43Z
        [tags] => Array
            (
            )

        [bytes] => 67627
        [type] => upload
        [etag] => a039768e2ac21671317c5867ec3f5671
        [url] => some_url
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

// thumb3 image array

Array
(
[0] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583111
        [signature] => some_signature
        [width] => 700
        [height] => 990
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:25:11Z
        [tags] => Array
            (
            )

        [bytes] => 298594
        [type] => upload
        [etag] => 26d2f4ece088bc30bcb02535ef8f9bc8
        [url] => some_urlh1axjsxkt4dpgog25kfq.jpg
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

[1] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583130
        [signature] => some_signature
        [width] => 700
        [height] => 990
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:25:30Z
        [tags] => Array
            (
            )

        [bytes] => 301400
        [type] => upload
        [etag] => 96913a8659a22a459c049b47b8862d80
        [url] => some_urlz9ciicghziugi9nckzug.jpg
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )

[2] => Array
    (
        [public_id] => some_public_id
        [version] => 1492583150
        [signature] => some_signature
        [width] => 700
        [height] => 990
        [format] => jpg
        [resource_type] => image
        [created_at] => 2017-04-19T06:25:50Z
        [tags] => Array
            (
            )

        [bytes] => 285431
        [type] => upload
        [etag] => 42a605e4916a6073b5e7b50a66dbcd37
        [url] => some_urlbxocbgwgp98nzozekj4f.jpg
        [secure_url] => some_secure_url
        [original_filename] => some_original_filename
    )
)

我想做的是制作4个名为=&gt;的新数组变量。 $pic1 = $pic2 = $pic3 = array();

我想将所有array[0]值存储在$pic1中,将所有array[1]值存储在$pic2&amp; array[2]中的所有$pic3值。之后,我将序列化它并以blob格式存储在数据库中。我应该使用哪种逻辑来实现这一目标,任何人都可以通过为脚本提供逻辑来帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:-1)

只要您的代码未被复制,我将假设以下示例:

$ar1 = [
    [1,2,3],[4,5,6],[7,8,9],
];
$ar2 = [
    [1,2,3],[4,5,6],[7,8,9],
];
$ar3 = [
    [1,2,3],[4,5,6],[7,8,9],
];

foreach ($ar1 as $k => $ar) {
    $pics[$k] = array($ar, $ar2[$k], $ar3[$k]);
}

print_r($pics);

这将重新安排您的阵列,您需要重现代码以满足您的需求,例如:https://3v4l.org/lYQp6