在PHP或CakePHP中更改给定数组的结构?

时间:2012-02-18 04:38:34

标签: php arrays cakephp cakephp-1.3

我有以下数组。

 Array
 (

  [Image] => Array
    (
        [0] => Array
            (
                [thumbnail_url] => http://www.xyz.com/img/uploadsChatImage/42/thumbnails/chicken_1.png
            )

        [1] => Array
            (
                [name] => chicken_1.png
            )

        [2] => Array
            (
                [thumbnail_url] => http://www.xyz.com/img/uploadsChatImage/42/thumbnails/chicken_2.png
            )

        [3] => Array
            (
                [name] => chicken_2.png
            )

        [4] => Array
            (
                [thumbnail_url] => http://www.xyz.com/img/uploadsChatImage/42/thumbnails/chicken_3.png
            )

        [5] => Array
            (
                [name] => chicken_3.png
            )

    )

)

但我想要这样的事情

  Array
 (
  [Image] => Array
    (
        [0] => Array
            (
                [thumbnail_url] => http://www.xyz.com/img/uploadsChatImage/42/thumbnails/chicken_1.png
                 [name] => chicken_1.png
            )

        [1] => Array
            (
                [thumbnail_url] => http://www.xyz.com/img/uploadsChatImage/42/thumbnails/chicken_2.png
             [name] => chicken_2.png
            )

        [2] => Array
            (
                [thumbnail_url] => http://www.xyz.com/img/uploadsChatImage/42/thumbnails/chicken_3.png
             [name] => chicken_3.png
            )

    )

  )

我该怎么做?

提前致谢

2 个答案:

答案 0 :(得分:1)

像这样,例如

  $arr = array('image'=>array(
    array('thumbnail_url'=>'url1'),
    array('name'=>'name1'),
    array('thumbnail_url'=>'url2'),
    array('name'=>'name2'),
    array('thumbnail_url'=>'url3'),
    array('name'=>'name3'),
    array('thumbnail_url'=>'url4'),
    array('name'=>'name4')
   )
  );

  $l = count($arr['image']);
  for($i=$l-2; $i>=0; $i-=2)
  {
      $arr['image'][$i] = array_merge($arr['image'][$i], $arr['image'][$i+1]);
      unset($arr['image'][$i+1]);
  }
  $arr['image'] = array_values($arr['image']);
  print_r($arr);

  $ar = &$arr['image'];
  $l = count($ar);
  for($i=$l-2; $i>=0; $i-=2)
  {
      $ar[$i] = array_merge($ar[$i], $ar[$i+1]);
      unset($ar[$i+1]);
  }
  $ar = array_values($ar);
  print_r($arr);

结果是

Array
(
    [image] => Array
        (
            [0] => Array
                (
                    [thumbnail_url] => url1
                    [name] => name1
                )

            [1] => Array
                (
                    [thumbnail_url] => url2
                    [name] => name2
                )

            [2] => Array
                (
                    [thumbnail_url] => url3
                    [name] => name3
                )

            [3] => Array
                (
                    [thumbnail_url] => url4
                    [name] => name4
                )

        )

)

答案 1 :(得分:0)

你也可以这样做:

<?php
$arr = array(
    'image'=>array(
        array('thumbnail_url'=>'url1'),
        array('name'=>'name1'),
        array('thumbnail_url'=>'url2'),
        array('name'=>'name2'),
        array('thumbnail_url'=>'url3'),
        array('name'=>'name3'),
        array('thumbnail_url'=>'url4'),
        array('name'=>'name4')
    )
);
$arr['image'] =
    array_map(
        function($i) use ($arr){
            return array(
                'thumbnail_url' => $arr['image'][$i]['thumbnail_url'],
                'name'  => $arr['image'][$i+1]['name'],
            );
        },
        range(0, count($arr['image'])-1, 2)
    );
print_r($arr);
?>