【问题标题】:Create a multidimensional array from array从数组创建多维数组
【发布时间】:2014-11-03 10:14:20
【问题描述】:

我正在尝试将 Drupal 数据库中的关联数组转换为可以编码为 json 的多维数组。

我开始:

$notifications =`

    Array
    (
    [0] => Array
        (
            [rfp_id] => RFP-013-2014(C)
            [notification_type] => due_date
        )

    [1] => Array
        (
            [rfp_id] => RFP-013-2014(C)
            [notification_type] => changes
        )

    [2] => Array
        (
            [rfp_id] => RFP-013-2014(C)
            [notification_type] => due_date
        )

    [3] => Array
        (
            [rfp_id] => RFP-014-2014(C)
            [notification_type] => due_date
        )

    [4] => Array
        (
            [rfp_id] => RFP-014-2014(C)
            [notification_type] => changes
        )
    )

我想按 rfp_id 字段分组并最终得到如下内容:

Array (
  [0]=> Array (
    ["rfp_id"]=>"RFP-014-2014"
    ["notification_type"]=>
    Array (
      [0]=> "date_due"
      [1]=> "changes"
    )
  )
)

我将如何遍历这个数组来创建它?

【问题讨论】:

  • 您为此做了哪些努力?
  • @MikeBrant 我尝试了几个不同的选项来循环遍历数组。我得到的最接近的是每个 notification_type 作为一个数组,而不是所有 notification_type 作为一个数组。
  • @CharlotteDunois 不,它不一样。我需要按其中一个值分组。

标签: php arrays multidimensional-array


【解决方案1】:

数组的维度没有变化,只是重组了。你可以这样做:

foreach ($notifications as $notification)
{
  $rfp_id = $notification['rfp_id'];
  $newArray[$rfp_id]['rfp_id'] = $rfp_id;
  $newArray[$rfp_id]['notification_type'][] = $notification['notification_type'];
} 
echo '<pre>'.print_r($newArray,TRUE).'</pre>';

如您所见,我所做的与指定的略有不同,只是因为它更容易。如果你想要数字键,你可以这样做:

$newArray = array_values($newArray);

【讨论】:

  • @JonathanKuhn:不,我打算发布同样的内容。即时创建数组索引,无论更高级别是否存在,或者即使数组 exixts 没有生成通知。
  • 一旦我将 print_f 更改为 print_r,这将完美连接。一旦我调用了 array_values,我就得到了我正在寻找的输出!
【解决方案2】:
<?
$result = array();

foreach ($notifications as $key => $note) {
    $result[$note['rfp_id']]['rfp_id'] = $note['rfp_id'];
    $result[$note['rfp_id']]['notification_type'][] = $note['notification_type'];

}

echo '<pre>';
print_r($result);
echo '</pre>';

?>

应该这样做。由于您在我假设您不需要将键重置为数字之后立即插入数据库。

【讨论】:

    【解决方案3】:

    这样的事情会起作用:

    $array = array();

    //loop over the array
    foreach($notifications as $row){
        //if this rfp id doesn't exist, add it
        if(!isset($array[$row['rfp_id']])){
            //add the first entry in the array
            $array[$row['rfp_id']] = array(
                    'rfp_id'=>$row['rfp_id'],
                    'notification_type'=>array(
                        $row['notification_type']
                    )
                );
        } else {
            //rfp_id exists, so just append the notification type
            $array[$row['rfp_id']]['notification_type'][] = $row['notification_type'];
        }
    }
    //get just the values to reset the first level keys to numeric.
    $array = array_values($array);
    
    //display the array
    echo '<pre>'.print_r($array,true).'</pre>';
    

    【讨论】:

    • 可能在array_values 之后添加类似foreach ($array as &amp;$entry) { $entry["notification_type"] = array_unique($entry["notification_type"]); } 的内容,以消除重复的notification_types。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2011-01-18
    相关资源
    最近更新 更多