在php

时间:2017-07-31 11:21:35

标签: php arrays

我有一个数组$ list是一个多维数组,我需要为我的应用程序自定义,我尝试使用unset($list['title'])array_column但未实现。

     Array
    (
     [0] => Array
        (
        [id] => 11
        [title] => same title1          
        [content] => hi
        [sendername] => John
        )

  [1] => Array
    (
        [id] => 12
        [title] => same title1
        [content] => hello
        [sendername] => Karim
    )
)

我需要这个

Array
(
   [title] => same title1,

   [message] =>Array(
    [0] => Array
        (
           [id] => 11                 
           [content] => hi
          [sendername] => John
        )

   [1] => Array
       (
          [id] => 12
          [content] => hello
         [sendername] => Karim
       )
   )
  )

请帮助我在php中实现这一点,这对我有用。

1 个答案:

答案 0 :(得分:1)

您需要从每一行取消设置标题字段,并使用标题和消息键创建新数组。 的解决方案

$array = [
    'title' => '',
    'message' => []
];

$data = [
    [
        'id' => 11,
        'content' => 'hi',
        'title' => 'same title1'
        'sendername' => 'John'
    ],
    [
        'id' => 12,
        'content' => 'hello',
        'title' => 'same title1'
        'sendername' => 'Karim'
    ],
];
foreach ($data as $item) {
    $array['title'] = empty($array['title']) ? $item['title'] : $array['title'];
    unset($item['title']);
    $array['message'][] = $item;
}