如何在PHP中替换数组项?

时间:2017-03-10 11:39:53

标签: php arrays multidimensional-array replace foreach

我有2个阵列:

1st:$ data with old img and link

journalWriter

第二名:$图片新图片

array:4 [▼
  0 => array:2 [▼
    "img" => "Koala.jpg"
    "link" => "http://site.loc/1"
  ]
  1 => array:2 [▼
    "img" => "Jellyfish.jpg"
    "link" => "http://site.loc/2"
  ]
  2 => array:2 [▼
    "img" => "Hydrangeas.jpg"
    "link" => "http://site.loc/3"
  ]
  3 => array:2 [▼
    "img" => "Penguins.jpg"
    "link" => "http://site.loc/4"
  ]
]

如何正确地替换它:

array:4 [▼
  "slide0" => UploadedFile {#241 ▼
    -test: false
    -originalName: "Lighthouse.jpg"
    ....
  }
  "slide1" => UploadedFile {#239 ▼
    -test: false
    -originalName: "Penguins.jpg"
   ....
  }
  "slide2" => UploadedFile {#234 ▼
    -test: false
    -originalName: "Desert.jpg"
    ....
  }
  "slide3" => UploadedFile {#238 ▼
    -test: false
    -originalName: "Tulips.jpg"
   ....
  }
]

我试图这样做:

"img" => "Koala.jpg" to "img" => "Lighthouse.jpg",
"img" => "Jellyfish.jpg" to "img" => "Penguins.jpg",
"img" => "Hydrangeas.jpg" to "img" => "Desert.jpg",
"img" => "Penguins.jpg" to "img" => "Tulips.jpg",

但最后一个数组与前一个数据重复:

foreach($data as $key=>&$item){
            if(isset($images['slide'.$key])){
                $item['img']=$images['slide'.$key]->getClientOriginalName();
            }
        }

是否可以防止重复,或者我应该使用其他东西然后预告?

另外,我注意到当我用foreach替换时,最后一个多维数字阵列数组有&

array(2) { ["img"]=> string(14) "Hydrangeas.jpg" ["link"]=> string(19) "http://site.loc/1" }
array(2) { ["img"]=> string(14) "Lighthouse.jpg" ["link"]=> string(19) "http://site.loc/2" }
array(2) { ["img"]=> string(13) "Jellyfish.jpg" ["link"]=> string(19) "http://site.loc/3" } 
array(2) { ["img"]=> string(13) "Jellyfish.jpg" ["link"]=> string(19) "http://site.loc/3" } 

也许这就是重复的原因?

3 个答案:

答案 0 :(得分:0)

为此,您必须有两个数组,然后您可以使用array_replace()替换它们。

实施例

<?php
$a1=array("abc","def");
$a2=array("xyz","lmn");
print_r(array_replace($a1,$a2));
?>

输出将是

Array ( [0] => xyz [1] => lmn ) 

答案 1 :(得分:0)

@Batmannn你可以像下面这样:

<?php
    $arr1 = array(
               array(
                "img" => "Koala.jpg",
                "link" => "http://site.loc/1"
               ),
               array(
                "img" => "Jellyfish.jpg",
                "link" => "http://site.loc/2"
               ),
               array(
                "img" => "Hydrangeas.jpg",
                "link" => "http://site.loc/3"
               ),
               array(
                "img" => "Penguins.jpg",
                "link" => "http://site.loc/4"
               )
            );

    $arr2 = array(
                  "slide0" =>
                  array(
                    "test"=> false,
                    "originalName"=> "Lighthouse.jpg"
                  ),
                  "slide1" =>
                  array(
                    "test"=> false,
                    "originalName"=> "Penguins.jpg"
                  ),
                  "slide2" =>
                  array(
                    "test"=> false,
                    "originalName"=> "Desert.jpg"
                  ),
                  "slide3" =>
                  array(
                    "test"=> false,
                    "originalName"=> "Tulips.jpg"
                  )
            );
    $newImageName = array_column($arr2, "originalName");

    foreach ($arr1 as $key => $value) {
        $arr1[$key]["img"] = $newImageName[$key];
    }

    echo "<pre>";
    print_r($arr1); // final result

答案 2 :(得分:0)

试试这个,没有任何功能你可以实现它,

<?php
$arr1 = array(
   array(
    "img" => "Koala.jpg",
    "link" => "http://site.loc/1"
   ),
   array(
    "img" => "Jellyfish.jpg",
    "link" => "http://site.loc/2"
   ),
   array(
    "img" => "Hydrangeas.jpg",
    "link" => "http://site.loc/3"
   ),
   array(
    "img" => "Penguins.jpg",
    "link" => "http://site.loc/4"
   )
);

  $arr2 = array(
      "slide0" =>
      array(
        "test"=> false,
        "originalName"=> "Lighthouse.jpg"
      ),
      "slide1" =>
      array(
        "test"=> false,
        "originalName"=> "Penguins.jpg"
      ),
      "slide2" =>
      array(
        "test"=> false,
        "originalName"=> "Desert.jpg"
      ),
      "slide3" =>
      array(
        "test"=> false,
        "originalName"=> "Tulips.jpg"
      )
);

foreach ($arr1 as $key => $value) {
    $arr1[$key]["img"] = $arr2['slide'.$key]['originalName'];
}
var_dump($arr1);