二维数组的回波值
Posted
技术标签:
【中文标题】二维数组的回波值【英文标题】:echo values of 2 dimensional array 【发布时间】:2017-02-17 02:20:17 【问题描述】:我有一个用于显示调查的二维数组。该数组包含 63 行(问题)。每行包含一个 GpDesc(分组,即领导特质)和一个 QueDesc(问题)。有 9 种不同的 GpDesc。我想在第一次出现时回显 GpDesc,然后回显该组中的问题和单选按钮,直到下一个 GpDesc。我知道我需要一个用于 GpDesc 数量的外部循环和一个用于该组内问题的内部循环,但我不知道该怎么做。这是 $varArray 的一部分:
Array (
[0] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10266
[QueDesc] => Is visionary
[GpQue] => 0
)
[1] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10267
[QueDesc] => Possesses high degree of integrity
[GpQue] => 0
)
[2] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10268
[QueDesc] => Displays an enthusiastic, positive outlook
[GpQue] => 0
)
[3] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10269
[QueDesc] => Uses good executive judgment
[GpQue] => 0
)
[4] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10270
[QueDesc] => Provides effective leadership
[GpQue] => 0
)
[5] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10271
[QueDesc] => Organizes and administers in a confident manner
[GpQue] => 0
)
[6] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10272
[QueDesc] => Acts fairly and decisively on important issues
[GpQue] => 0
)
[7] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10273
[QueDesc] => Displays effective strategic planning
[GpQue] => 0
)
[8] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10274
[QueDesc] => Establishes appropriate goals and objectives
[GpQue] => 0
)
[9] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10275
[QueDesc] => Defines appropriate priorities
[GpQue] => 0
)
[10] => Array (
[GpID] => 1168
[GpDesc] => LEADERSHIP TRAITS
[QueID] => 10276
[QueDesc] => Entertains new ideas
[GpQue] => 0
)
[11] => Array (
[GpID] => 1169
[GpDesc] => MANAGEMENT TRAITS
[QueID] => 10277
[QueDesc] => Involves faculty in strategic planning
[GpQue] => 0
)
[12] => Array (
[GpID] => 1169
[GpDesc] => MANAGEMENT TRAITS
[QueID] => 10278
[QueDesc] => Utilizes departmental advisory group or other clientele in decision making
[GpQue] => 0
)
[13] => Array (
[GpID] => 1169
[GpDesc] =>
有人可以帮忙吗?
【问题讨论】:
你能用print_r($array);
的结果或至少出现几次的结果替换那个转储
抱歉弄得一团糟,但这里是其中的一部分。
Cannot output multi-dimensional array的可能重复
【参考方案1】:
<?php
$survey =
Array(
Array (
'GpID' => 1168,
'GpDesc' => 'LEADERSHIP TRAITS',
'QueID' => 10266,
'QueDesc' => 'Is visionary',
'GpQue' => 0
),
Array (
'GpID' => 1168,
'GpDesc' => 'LEADERSHIP TRAITS',
'QueID' => 10267,
'QueDesc' => 'Possesses high degree of integrity',
'GpQue' => 0
),
Array (
'GpID' => 1169,
'GpDesc' => 'MANAGEMENT TRAITS',
'QueID' => 10277,
'QueDesc' => 'Involves faculty in strategic planning',
'GpQue' => 0
),
Array (
'GpID' => 1169,
'GpDesc' => 'MANAGEMENT TRAITS',
'QueID' => 10278,
'QueDesc' => 'Utilizes departmental advisory group or other clientele in decision making',
'GpQue' => 0
)
);
echo $survey[0]['GpDesc'].PHP_EOL;
$prevGpId = $survey[0]['GpID'];
foreach ($survey as $line)
if ($line['GpID'] !== $prevGpId)
echo $line['GpDesc'].PHP_EOL;
$prevGpId = $line['GpID'];
echo "\t".$line['QueDesc'].PHP_EOL;
【讨论】:
非常感谢您的帮助!这正是我所需要的。以上是关于二维数组的回波值的主要内容,如果未能解决你的问题,请参考以下文章