2017-04-19 18 views
-1

我已經做以下變量的print_r()後,用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); 
} 

下面的代碼關聯數組數據的值=>$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圖像陣列

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圖像陣列

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圖像陣列

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個新的數組變量=>$pic1 = $pic2 = $pic3 = array();

我想所有的array[0]值存儲在$pic1,所有$pic2 &所有$pic3array[2]array[1]值。之後,我將序列化它並以blob格式存儲在數據庫中。我應該使用哪種邏輯來實現,任何人都可以通過爲腳本提供邏輯來幫助我。

在此先感謝。

+0

你有固定數量的數組嗎?並且在每個數組中你是否有固定數量的元素? – hassan

+0

是的,這些都是固定的,它不會超過這個數量,最少2個數組將會在那裏 – user7886262

回答

-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

+0

謝謝@hassan邏輯工作完美 – user7886262