更改数组值并在PHP中保存到数据库

时间:2016-06-12 08:30:04

标签: php arrays replace

我如何在PHP中更改特定的数组值?我有一个电影投射信息是一个数组:

array (
  0 => 
  array (
    'name' => 'Mia Wasikowska',
    'id' => 'nm1985859',
    'url' => 'http://www.imdb.com/name/nm1985859/',
    'image' => 'http://ia.media-imdb.com/images/M/MV5BMTUzNzI0MDczN15BMl5BanBnXkFtZTgwNTQ0MjMxOTE@._V1_.jpg',
    'character' => 'Alice Kingsleigh',
  ),
  1 => 
  array (
    'name' => 'Johnny Depp',
    'id' => 'nm0000136',
    'url' => 'http://www.imdb.com/name/nm0000136/',
    'image' => 'http://ia.media-imdb.com/images/M/MV5BMTM0ODU5Nzk2OV5BMl5BanBnXkFtZTcwMzI2ODgyNQ@@._V1_.jpg',
    'character' => 'Hatter Tarrant Hightopp',
  ),
  2 => 
  array (
    'name' => 'Helena Bonham Carter',
    'id' => 'nm0000307',
    'url' => 'http://www.imdb.com/name/nm0000307/',
    'image' => 'http://ia.media-imdb.com/images/M/MV5BMTUzMzUzMDg5MV5BMl5BanBnXkFtZTcwMDA5NDMwNA@@._V1_.jpg',
    'character' => 'Iracebeth',
  ),
  3 => 
  array (
    'name' => 'Anne Hathaway',
    'id' => 'nm0004266',
    'url' => 'http://www.imdb.com/name/nm0004266/',
    'image' => 'http://ia.media-imdb.com/images/M/MV5BNjQ5MTAxMDc5OF5BMl5BanBnXkFtZTcwOTI0OTE4OA@@._V1_.jpg',
    'character' => 'Mirana',
  ),
)

我需要将图像值更改为不同的值。例如,改变这个:

'image' => 'http://ia.media-imdb.com/images/M/MV5BNjQ5MTAxMDc5OF5BMl5BanBnXkFtZTcwOTI0OTE4OA@@._V1_.jpg',

到此:

'image' => '/uploads/images/artirst_0214.jpg',

最后我需要这样:

array (
  0 => 
  array (
    'name' => 'Mia Wasikowska',
    'id' => 'nm1985859',
    'url' => 'http://www.imdb.com/name/nm1985859/',
    'image' => '/uploads/images/artirst_032.jpg',
    'character' => 'Alice Kingsleigh',
  ),
  1 => 
  array (
    'name' => 'Johnny Depp',
    'id' => 'nm0000136',
    'url' => 'http://www.imdb.com/name/nm0000136/',
    'image' => '/uploads/images/artirst_07897.jpg',
    'character' => 'Hatter Tarrant Hightopp',
  ),
  2 => 
  array (
    'name' => 'Helena Bonham Carter',
    'id' => 'nm0000307',
    'url' => 'http://www.imdb.com/name/nm0000307/',
    'image' => '/uploads/images/artirst_0987.jpg',
    'character' => 'Iracebeth',
  ),
  3 => 
  array (
    'name' => 'Anne Hathaway',
    'id' => 'nm0004266',
    'url' => 'http://www.imdb.com/name/nm0004266/',
    'image' => '/uploads/images/artirst_0214.jpg',
    'character' => 'Mirana',
  ),
)

谢谢

1 个答案:

答案 0 :(得分:1)

$ArrayKeys=array_keys($data);
$cntKeys=count($ArrayKeys);
for ($i=0;$i<$cntKeys;$i++)
{
$data[$ArrayKeys[$i]]["image"]="ciro";
}

print_r ($data);

假设你的数组被称为“数据”。 通过这种方式,您可以获得数组的键(0,1,2 ....),并在每个键上迭代以访问img值... 然后,您可以使用$ data [key_your_array] [“image”] =“something”访问该值。我用

$data[$ArrayKeys[$i]]

因为顾名思义,$ ArrayKeys是一个数组,我想访问第i个元素