如何合并特定格式的数组?

发布于 2024-10-10 00:14:48 字数 2499 浏览 1 评论 0原文

我有以下数组:

1)对于总放置

Array
(
[0] => Array
    (
        [centers] => Array
            (
                [name] => delhi
                [id] => 1
            )

        [0] => Array
            (
                [totalplaced] => 8
            )

    )

[1] => Array
    (
        [centers] => Array
            (
                [name] => mumbai
                [id] => 2
            )

        [0] => Array
            (
                [totalplaced] => 1
            )

    )

)

2)对于总工作

 Array
(
[0] => Array
    (
        [centers] => Array
            (
                [name] => delhi
                [id] => 1
            )

        [0] => Array
            (
                [totalworking] => 4
            )

    )

[1] => Array
    (
        [centers] => Array
            (
                [name] => mumbai
                [id] => 2
            )

        [0] => Array
            (
                [totalworking] => 1
            )

    )

 )

3)对于总训练

Array
(
[0] => Array
    (
        [centers] => Array
            (
                [name] => delhi
                [id] => 1
            )

        [0] => Array
            (
                [totaltrained] => 8
            )

    )

[1] => Array
    (
        [centers] => Array
            (
                [name] => mumbai
                [id] => 2
            )

        [0] => Array
            (
                [totaltrained] => 1
            )

    )

)

我想合并这些数组,以便生成的数组应如下所示

[newarray] => Array(
[0] => Array (
    [centers] => Array
            (
                [name] => delhi
                [id] => 1
                [totalplaced] => 8
                [totalworking] => 4
                [totaltrained] => 8
             )
  )
[1]=> Array(
   [centers] => Array
            (
                [name] => mumbai
                [id] => 2
                [totalplaced] => 1
                [totalworking] => 1
                [totaltrained] => 1
             )
    )
 )

这是我要显示的上述数据的表格表示形式

centername      totalplaced    totalworking   totaltrained 
  delhi             8               4             8
  mumbai            1               1             1 

请帮我解决这个问题。

感谢潘卡吉

·库拉纳

I have following arrays:

1) for total placed

Array
(
[0] => Array
    (
        [centers] => Array
            (
                [name] => delhi
                [id] => 1
            )

        [0] => Array
            (
                [totalplaced] => 8
            )

    )

[1] => Array
    (
        [centers] => Array
            (
                [name] => mumbai
                [id] => 2
            )

        [0] => Array
            (
                [totalplaced] => 1
            )

    )

)

2) for total working

 Array
(
[0] => Array
    (
        [centers] => Array
            (
                [name] => delhi
                [id] => 1
            )

        [0] => Array
            (
                [totalworking] => 4
            )

    )

[1] => Array
    (
        [centers] => Array
            (
                [name] => mumbai
                [id] => 2
            )

        [0] => Array
            (
                [totalworking] => 1
            )

    )

 )

3) for total trained

Array
(
[0] => Array
    (
        [centers] => Array
            (
                [name] => delhi
                [id] => 1
            )

        [0] => Array
            (
                [totaltrained] => 8
            )

    )

[1] => Array
    (
        [centers] => Array
            (
                [name] => mumbai
                [id] => 2
            )

        [0] => Array
            (
                [totaltrained] => 1
            )

    )

)

I wanted to merge these arrays so that the resultant array should look like this

[newarray] => Array(
[0] => Array (
    [centers] => Array
            (
                [name] => delhi
                [id] => 1
                [totalplaced] => 8
                [totalworking] => 4
                [totaltrained] => 8
             )
  )
[1]=> Array(
   [centers] => Array
            (
                [name] => mumbai
                [id] => 2
                [totalplaced] => 1
                [totalworking] => 1
                [totaltrained] => 1
             )
    )
 )

This is the tabular representation of the above data which i want to display

centername      totalplaced    totalworking   totaltrained 
  delhi             8               4             8
  mumbai            1               1             1 

Please help me on this.

Thanks

Pankaj Khurana

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

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

发布评论

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

评论(1

要走就滚别墨迹 2024-10-17 00:14:48

这里的困难在于 PHP 的函数(例如 array_merge() 和 array_merge_recursive())不会将数据合并到数字键中,而是会重新键入任何重复的数字键。例如,给定两个数组:

array(
  'test' => 'abc',
  0 => 'xyz'
);

array(
  'test' => 'def',
  0 => 'uvw'
);

使用 array_merge() 将它们合并在一起将生成一个如下数组:

array(
  'test' => 'def',
  0 => 'xyz',
  1 => 'uvw'
);

因此,您需要一个自定义函数来对任何键进行“添加”,无论它是字符串键还是数字键。试试这个:

function mixed_key_array_merge() {

  $args = func_get_args();

  $result = array();

  foreach ($args as $arg) {

    // discard non-array arguments; maybe this could be better handled
    if (!is_array($arg)) {
      continue;
    }

    foreach ($arg as $key => $value) {

      if (!isset($result[$key])) {
        $result[$key] = $value;
      } else if (is_array($result[$key])) {
        $result[$key] = call_user_func_array('mixed_key_array_merge',array($result[$key],$value));
      }

    }

  }

  return $result;

}

The difficulty here is that PHP's functions such as array_merge() and array_merge_recursive() will not merge data into numeric keys, but rather will re-key any duplicate numeric key. So for example given two arrays:

array(
  'test' => 'abc',
  0 => 'xyz'
);

array(
  'test' => 'def',
  0 => 'uvw'
);

Merging them together with array_merge() will produce an array like:

array(
  'test' => 'def',
  0 => 'xyz',
  1 => 'uvw'
);

So, you need a custom function to be "additive" on any key, regardless of whether it is a string or numeric key. Try this:

function mixed_key_array_merge() {

  $args = func_get_args();

  $result = array();

  foreach ($args as $arg) {

    // discard non-array arguments; maybe this could be better handled
    if (!is_array($arg)) {
      continue;
    }

    foreach ($arg as $key => $value) {

      if (!isset($result[$key])) {
        $result[$key] = $value;
      } else if (is_array($result[$key])) {
        $result[$key] = call_user_func_array('mixed_key_array_merge',array($result[$key],$value));
      }

    }

  }

  return $result;

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