数组的操作或修改
Manipulation or modification of array
我是 PHP 的新手。我正在用 php 做我的一个项目,我对数组函数和所有这些东西都不熟悉。我已经尝试过但没有成功。让我向您展示我的 sql 查询数组。
我有一个数组,如下所示:
Array
(
[0] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Rutul
[ulname] => Shah
[name] => Clinic
)
[1] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Rutul
[ulname] => Shah
[name] => Clinic
)
[2] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[3] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[4] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[5] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Amit
[ulname] => Mahida
[name] => Cancer Specialist
)
[6] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Amit
[ulname] => Mahida
[name] => Breach Candy Hospital
)
)
现在我想要我的结果数组如下:
Array
(
[2016-08-25] => Array
(
[ Clinic] => Array
(
[Rutul Shah] => Array
(
[appointments] => 1
)
[Administrator Administrator] => Array
(
[appointments] => 2
)
)
)
[2016-08-26] => Array
(
[Clinic] => Array
(
[Rutul Shah] => Array
(
[appointments] => 1
)
[Administrator Administrator] => Array
(
[appointments] => 1
)
)
[Cancer Specialist] => Array
(
[Amit Mahida] => Array
(
[appointments] => 1
)
)
[Breach Candy Hospital] => Array
(
[Amit Mahida] => Array
(
[appointments] => 1
)
)
)
)
您想遍历约会数组并使用其内容生成其他数据结构。让我们调用你的第一个数组 $input
和你的第二个数组 $output
:
// initialize output array
$output = [];
// loop through each $appt in the $input array
foreach($input as $appt) {
// get shorter var names for appt data
$date = $appt['pc_eventDate'];
$name = $appt['name'];
$uname = $appt['ufname'].' '.$appt['ulname'];
// initialize each level of the data structure if it doesn't already exist
if(!isset($output[$date])) $output[$date] = [];
if(!isset($output[$date][$name])) $output[$date][$name] = [];
if(!isset($output[$date][$name][$uname])) $output[$date][$name][$uname] = [];
// initialize the number of appts to 0
if(!isset($output[$date][$name][$uname]['appointments'])) $output[$date][$name][$uname]['appointments'] = 0;
// increment the number of appts
$output[$date][$name][$uname]['appointments']++;
}
重要的是根据旧结构中的数据对新结构中的每个子数组进行初始化——从那里我们只是计算与新数据匹配的约会数量。
祝你好运!
说数组是问题是$arr
这将按照您想要的方式排列数组作为解决方案
foreach ($arr as $key => $value) {
if(!is_array($value['pc_eventDate]))
$value['pc_eventDate] = [];
if(!is_array($value['pc_eventDate]['name']))
$value['pc_eventDate]['name'] = [];
if(!is_array($value['pc_eventDate']['name']['ufname'.' ulname'])){
$value['pc_eventDate']['name']['ufname'.' ulname'] = [];
$value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] = 1;
}else{
$value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] += 1;
}
}
您需要执行与上述类似的操作。
尝试 运行 上面的代码,可能有一些错字。如果它没有产生您想要的结果,请尝试注释掉 foreach 正文中的所有行和 var_dump()
每个步骤以测试数组结构的构建。
谢谢
我是 PHP 的新手。我正在用 php 做我的一个项目,我对数组函数和所有这些东西都不熟悉。我已经尝试过但没有成功。让我向您展示我的 sql 查询数组。 我有一个数组,如下所示:
Array
(
[0] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Rutul
[ulname] => Shah
[name] => Clinic
)
[1] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Rutul
[ulname] => Shah
[name] => Clinic
)
[2] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[3] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[4] => Array
(
[pc_eventDate] => 2016-08-25
[ufname] => Administrator
[ulname] => Administrator
[name] => Clinic
)
[5] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Amit
[ulname] => Mahida
[name] => Cancer Specialist
)
[6] => Array
(
[pc_eventDate] => 2016-08-26
[ufname] => Amit
[ulname] => Mahida
[name] => Breach Candy Hospital
)
)
现在我想要我的结果数组如下:
Array
(
[2016-08-25] => Array
(
[ Clinic] => Array
(
[Rutul Shah] => Array
(
[appointments] => 1
)
[Administrator Administrator] => Array
(
[appointments] => 2
)
)
)
[2016-08-26] => Array
(
[Clinic] => Array
(
[Rutul Shah] => Array
(
[appointments] => 1
)
[Administrator Administrator] => Array
(
[appointments] => 1
)
)
[Cancer Specialist] => Array
(
[Amit Mahida] => Array
(
[appointments] => 1
)
)
[Breach Candy Hospital] => Array
(
[Amit Mahida] => Array
(
[appointments] => 1
)
)
)
)
您想遍历约会数组并使用其内容生成其他数据结构。让我们调用你的第一个数组 $input
和你的第二个数组 $output
:
// initialize output array
$output = [];
// loop through each $appt in the $input array
foreach($input as $appt) {
// get shorter var names for appt data
$date = $appt['pc_eventDate'];
$name = $appt['name'];
$uname = $appt['ufname'].' '.$appt['ulname'];
// initialize each level of the data structure if it doesn't already exist
if(!isset($output[$date])) $output[$date] = [];
if(!isset($output[$date][$name])) $output[$date][$name] = [];
if(!isset($output[$date][$name][$uname])) $output[$date][$name][$uname] = [];
// initialize the number of appts to 0
if(!isset($output[$date][$name][$uname]['appointments'])) $output[$date][$name][$uname]['appointments'] = 0;
// increment the number of appts
$output[$date][$name][$uname]['appointments']++;
}
重要的是根据旧结构中的数据对新结构中的每个子数组进行初始化——从那里我们只是计算与新数据匹配的约会数量。
祝你好运!
说数组是问题是$arr
这将按照您想要的方式排列数组作为解决方案
foreach ($arr as $key => $value) {
if(!is_array($value['pc_eventDate]))
$value['pc_eventDate] = [];
if(!is_array($value['pc_eventDate]['name']))
$value['pc_eventDate]['name'] = [];
if(!is_array($value['pc_eventDate']['name']['ufname'.' ulname'])){
$value['pc_eventDate']['name']['ufname'.' ulname'] = [];
$value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] = 1;
}else{
$value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] += 1;
}
}
您需要执行与上述类似的操作。
尝试 运行 上面的代码,可能有一些错字。如果它没有产生您想要的结果,请尝试注释掉 foreach 正文中的所有行和 var_dump()
每个步骤以测试数组结构的构建。
谢谢