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

发布于 2025-01-06 23:48:23 字数 1577 浏览 0 评论 0原文

我有下面的数组。

 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
            )

    )

  )

我该怎么办?

提前致谢

I have below array.

 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
            )

    )

)

But I want to something like this

  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
            )

    )

  )

What should I do for this?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宫墨修音 2025-01-13 23:48:23

像这样,例如

  $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
                )

        )

)

Like this, for example

  $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);

or

  $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);

Result is

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
                )

        )

)
情绪失控 2025-01-13 23:48:23

你也可以这样做:

<?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);
?>

You could also do it like this:

<?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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文