基于键的PHP重新排序数组

时间:2019-07-07 08:57:39

标签: php arrays

考虑以下数组// ------------ HOW TO MAKE THIS WORKING ??????????????? app.get('/project-2/*', (req, res) => { res.render('../project-2/index', { req }); });

* installing *source* package 'burglr' ...
** using staged installation
Error in file(file, if (append) "a" else "w") : 
  (converti depuis l'avis) impossible d'ouvrir le fichier 
'C:/Users/Valirie/Documents/R/win-library/3.4/00LOCK- 
burglr/00new/burglr/DESCRIPTION' : No such file or directory
ERROR: installing package DESCRIPTION failed for package 'burglr'
* removing 'C:/Users/Valérie/Documents/R/win-library/3.4/burglr'

基于此数组,我想创建一个新的data = '''<p><span class="label">Venue</span> <span class="divider">:</span> AmCham Office, 1 Scotts Rd, Shaw Centre #23-03 S(228208) - J&amp;J Auditorium</p>, <p><span class="label">Date</span> <span class="divider">:</span> July 09, 2019</p>, <p><span class="label">Time</span> <span class="divider">:</span> 11:45 AM - 1:30 PM </p>, <p><span class="label">Price</span> <span class="divider">:</span> $25.00</p>''' from bs4 import BeautifulSoup soup = BeautifulSoup(data, 'lxml') data = [] for p in soup.select('p'): data.append([d.strip() for d in p.text.split(':', 1)]) for (header, info) in data: print('{: <10}{: <60}'.format(header, info)) ,如下所示:

Venue     AmCham Office, 1 Scotts Rd, Shaw Centre #23-03 S(228208) - J&J Auditorium
Date      July 09, 2019                                               
Time      11:45 AM -  1:30 PM                                         
Price     $25.00                                                      

如您所见,这里的要求是只要'trackName'保持与下一个相同,就一直合并'url'的值,但是如果相同的“ trackName”应该存在于其他位置(例如在本示例中,我们有2倍的“ road-america”,但是由于两者之间存在不同的“ trackName”,因此不应将它们合并)。

到目前为止,我花了大量时间试图弄清楚如何到达那里(我想这必须使用嵌套循环吗?)

更新

这是我尝试过的事情之一(但是很明显,这只是看下一个'trackName'-不知道如何从这里继续)

$arr_sessions

4 个答案:

答案 0 :(得分:2)

不要查看下一个轨道名称,而是将上一次迭代中的轨道名称保存在变量中。每当它更改时,就在结果数组中开始一个新元素。

void*

答案 1 :(得分:1)

这是带有输出的完整示例。您需要检查轨道名称是否与上一个相同,并相应地构建阵列。

$a = json_decode('[
  {
    "url": "2019_7_5_0_52_RACE.json",
    "trackName": "road-america"
  },
  {
    "url": "2019_7_5_0_27_RACE.json",
    "trackName": "road-america"
  },
  {
    "url": "2019_7_5_0_16_QUALIFY.json",
    "trackName": "road-america"
  },
  {
    "url": "2019_7_5_0_0_PRACTICE.json",
    "trackName": "road-america"
  },
  {
    "url": "2019_7_4_23_9_RACE.json",
    "trackName": "ttassen"
  },
  {
    "url": "2019_7_4_23_52_RACE.json",
    "trackName": "ttassen"
  },
  {
    "url": "2019_7_2_11_25_RACE.json",
    "trackName": "road-america"
  },
  {
    "url": "2019_7_2_11_03_RACE.json",
    "trackName": "road-america"
  }
]');

// Set previous track to null
$prev_track = NULL;
// New array
$new_a = [];
// Set counter
$i = 0;

foreach ($a as $value) {

  // If previous track name is different than current track name
  if ($prev_track !== $value->trackName) {

    // Increment counter
    $i++;
    // Create a new array entry
    $new_a[$i] = ['trackName' => $value->trackName, 'url' => [$value->url]];

  } else {
    // Append to previously created array entry
    $new_a[$i]['url'][] = $value->url;
  }

  // Set previous track to current track
  $prev_track = $value->trackName;
}

print json_encode(array_values($new_a));

这将输出:

[
  {
    "trackName": "road-america",
    "url": [
      "2019_7_5_0_52_RACE.json",
      "2019_7_5_0_27_RACE.json",
      "2019_7_5_0_16_QUALIFY.json",
      "2019_7_5_0_0_PRACTICE.json"
    ]
  },
  {
    "trackName": "ttassen",
    "url": [
      "2019_7_4_23_9_RACE.json",
      "2019_7_4_23_52_RACE.json"
    ]
  },
  {
    "trackName": "road-america",
    "url": [
      "2019_7_2_11_25_RACE.json",
      "2019_7_2_11_03_RACE.json"
    ]
  }
]

答案 2 :(得分:0)

假设$ arr_sessions实际上是JSON,则可以尝试以下代码:

$output = [];
$data = json_decode($input);

foreach($data as $key=>$value) {
    if(isset($output[$value->trackName])) {
        array_push($output[$value->trackName]['url'], $value->url);
    } else {
        $output[$value->trackName] = [
            'trackName' => $value->trackName,
            'url' => [$value->url]
        ];
    }
}
$your_desired_format = array_values($output);

答案 3 :(得分:0)

检查一下:

{
    "Version": "2012-10-17",
    "Statement": [{
        "Sid": "InvokeMyLambdaFunctionOnlyFromSNS",
        "NotPrincipal": {
            "Service": [
                "sns.amazon.com"
            ]
        },
        "Effect": "Deny",
        "Action": "lambda:InvokeFunction",
        "Resource": "arn:aws:lambda:region:AccountID:function:function_name"
    }]
}