我希望你们还好,从最近3-4个小时起,我尝试将年份合并到同一数组集中,并取消所有其他重复的数组的设置(我无法正确解释,请检查下面的数组示例)。到目前为止,没有运气,我尝试过,我知道这不是正确的方法,如果有人对此有解决方案,请分享。
有人能告诉我如何将同一年合并到一个数组中,同时取消设置其他数组吗?我试过但失败了,没有运气。
尝试这样做,但失败了:
<?php
$json = <<<JSON
{
"-LYd55ZsqtoktfA58X91": {
"city": "NY",
"department": "abc0",
"email": "awesomemail@test.com",
"fullName": "David Awesome",
"gender": 1,
"hireDate": "2019-02-04",
"isPermanent": false,
"mobile": "123456789"
},
"-LYd55ZsqtoktfA58X92": {
"city": "CH",
"department": "abc0",
"email": "awesomemail@test.com",
"fullName": "David Awesome",
"gender": 1,
"hireDate": "2019-02-04",
"isPermanent": false,
"mobile": "123456789"
}
}
JSON;
$cities = array_column(json_decode($json, true), 'city');
print_r($cities);
实际数组:
foreach ($class as $i=>$class_list){
echo $i;
foreach ($class_list as $key=>$value){
$e=0;
foreach ($class_list as $value2){
if ($value['name']==$value2['name']){
echo 'match';
$class['packages'][$key]['year'][$e]=$value['year'];
}
$e++;
}
}
}
需要这样的输出:
Array
(
[packages] => Array
(
[0] => Array
(
[name] => atif.ali
[year] => 2019
)
[1] => Array
(
[name] => atif.ali
[year] => 2018
)
[2] => Array
(
[name] => atif.ali
[year] => 2017
)
[3] => Array
(
[name] => khatija.abbas
[year] => 2017
)
)
)
P.S:数组可以不同,但“年”和“名称”结构始终相同。
答案 0 :(得分:2)
以所需的格式创建一个新数组可能是最简单的。您可以通过遍历packages
内的值,检查新数组中是否存在name
,如果存在,将当前年份添加到该值中来实现。如果不存在,只需为该名称创建一个新条目。
$new_array = array('packages' => array());
foreach ($array['packages'] as $value) {
if (($k = array_search($value['name'], array_column($new_array['packages'], 'name'))) !== false) {
$new_array['packages'][$k]['year'][] = $value['year'];
}
else {
$new_array['packages'][] = array('name' => $value['name'], 'year' => array($value['year']));
}
}
print_r($new_array);
输出:
Array
(
[packages] => Array
(
[0] => Array
(
[name] => atif.ali
[year] => Array
(
[0] 2019
[1] 2018
[2] 2017
)
)
[1] => Array
(
[name] => khatija.abbas
[year] => Array (
[0] => 2017
)
)
)
)
更新
如果您真的希望year
字段在只有一个值时不为数组,则此代码将为您完成该操作;它只会在看到第二个或后续值时才将其放入数组。
$new_array = array('packages' => array());
foreach ($array['packages'] as $value) {
if (($k = array_search($value['name'], array_column($new_array['packages'], 'name'))) !== false) {
if (is_array($new_array['packages'][$k]['year'])) {
$new_array['packages'][$k]['year'][] = $value['year'];
}
else {
$new_array['packages'][$k]['year'] = array($new_array['packages'][$k]['year'], $value['year']);
}
}
else {
$new_array['packages'][] = $value;
}
}
print_r($new_array);
答案 1 :(得分:1)
尝试一下
$class = ['packages' => [['name' => 'atif.ali', 'year' => 2019], ['name' => 'atif.ali', 'year' => 2018], ['name' => 'atif.ali', 'year' => 2017], ['name' => 'khatija.abbas', 'year' => 2017]]];
$new_class = [];
foreach ($class['packages'] as $key => $class_list) {
if( ! isset($new_class[$class_list['name']])){
$new_class[$class_list['name']] = $class_list;
}else{
if(is_array($new_class[$class_list['name']]['year'])){
$new_class[$class_list['name']]['year'][] = $class_list['year'];
}else{
$prev_year = $new_class[$class_list['name']]['year'];
$new_class[$class_list['name']]['year'] = [$prev_year, $class_list['year']];
}
}
}
$class['packages'] = $new_class;
答案 2 :(得分:1)
我看到自己写的其他一些回应。首先,这里有一段代码:
<?php
// Input data
$input = array('packages' => array(
array('name' => 'atif.ali', 'year' => 2019),
array('name' => 'atif.ali', 'year' => 2018),
array('name' => 'atif.ali', 'year' => 2017),
array('name' => 'khatija.abbas', 'year' => 2017),
));
$output = array(); // Create new array to store the final result
foreach ($input['packages'] as $package) { // we are only interested in 'packages', let's iterate over them
$name = $package['name'];
$year = $package['year'];
if (!isset($output[$name])) { // First time we see this name?
$output[$name] = array('name' => $name, 'year' => []); // Let's create an "empty" structure.
}
if (!in_array($year, $output[$name]['year'])) { // First time we see this year for this name?
$output[$name]['year'][] = $year; // Let's add it
}
}
// Reformatting to fit your needs (nest everything under 'packages' and remove the names from resulting dictionary keys)
$output = array('packages' => array_values($output));
var_dump($output);
让我注意关键问题/概念:
给出代码并注释一下,如果您有任何疑问,请告诉我。
答案 3 :(得分:1)
另一种选择是在键packages
已存在的情况下创建新结果。然后使用名称作为密钥,以便您可以检查密钥是否已经存在。
如果没有,请添加一个新条目。如果已经存在,则索引到新的$result
中,将已经存在的内容转换为数组,以便作为字符串的第一个条目成为数组。
$arrays = [
"packages" => [
["name" => "atif.ali", "year" => "2019"],
["name" => "atif.ali", "year" => "2018"],
["name" => "atif.ali", "year" => "2017"],
["name" => "khatija.abbas", "year" => "2019"]
]
];
$result = ["packages" => []];
foreach ($arrays["packages"] as $array) {
if(!array_key_exists($array['name'], $result["packages"])) {
$result["packages"][$array['name']] = $array;
continue;
}
$result["packages"][$array['name']]['year'] = (array)$result["packages"][$array['name']]['year'];
$result["packages"][$array['name']]['year'][] = $array['year'];
}
print_r($result);
查看php demo
如果您想使用数字索引,则可以像array_values一样使用this example
答案 4 :(得分:0)
我很快就写了这样的书-我希望早晨有机会休息一下,并重新思考如何使用一个循环来做这件事,所以我会在早上再次查看。这似乎为我工作。 注意,我没有包含“ packages”键,但是可以通过在该特定部分的前面添加代码来修改代码。
想一想我做了什么,也许这就是您想要的:
<?php
$raw = [
[
'name'=>'atif.ali',
'year'=>2018
],
[
'name'=>'atif.ali',
'year'=>2019
],
[
'name'=>'atif.ali',
'year'=>2017
],
[
'name'=>'kahitja.abal',
'year'=>2019
],
[
'name'=>'kahitja.abal',
'year'=>2018
],
[
'name'=>'john.smith',
'year'=>2017
],
[
'name'=>'john.smith',
'year'=>2018
],
];
$map_index = [];
$final = [];
for($i = 0; $i< count($raw); $i++) {
if(!isset($map_index[$raw[$i]['name']])){
$map_index[$raw[$i]['name']] = count($final);
$final[count($final)] = ['name'=>$raw[$i]['name'], 'years'=>[]];
}
$index = $map_index[$raw[$i]['name']];
$final[$index]['years'][]=$raw[$i]['year'];
}
echo '<pre>';
print_r($final);
?>
答案 5 :(得分:0)
这里是一个示例,只需构建一个新数组并交换。
<?php
$data =
[
[
'name' => 'Bilbo Baggins',
'year' => 2010
],
[
'name' => 'Bilbo Baggins',
'year' => 2011
],
[
'name' => 'Frodo Baggins',
'year' => 1999
]
];
foreach($data as $item) {
$output[$item['name']]['name'] = $item['name'];
$output[$item['name']]['year'][] = $item['year'];
}
print_r($output);
输出:
Array
(
[Bilbo Baggins] => Array
(
[name] => Bilbo Baggins
[year] => Array
(
[0] => 2010
[1] => 2011
)
)
[Frodo Baggins] => Array
(
[name] => Frodo Baggins
[year] => Array
(
[0] => 1999
)
)
)
如果您不喜欢新键,请在结果上调用array_values
。