<?php
$keywordsUrlsArray = array(
0 => array
(
'url' => 'https://www.example.pl/?test=19',
'keyword' => 'test',
'score1' => 100,
'score2' => 50,
'position' => 4
),
1 => array
(
'url' => 'https://www.example.pl/?test=2',
'keyword' => 'test',
'score1' => 100,
'score2' => 50,
'position' => 1
),
2 => array
(
'url' => 'https://www.example.pl/?test=3',
'keyword' => 'test',
'score1' => 100,
'score2' => 50,
'position' => 3
),
3 => array
(
'url' => 'https://www.example.pl/other-site?test=3',
'keyword' => 'test',
'score1' => 100,
'score2' => 50,
'position' => 3
)
);
$res = array();
foreach ($keywordsUrlsArray as $urlResults) {
//remove params from url
$parsedUrl = parse_url($urlResults['url']);
$parsedUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'];
$keyword = $urlResults['keyword'];
if (array_key_exists($parsedUrl, $res)) {
if (isset($res[$parsedUrl][$keyword])) {
$res[$parsedUrl][$keyword]['urlCount'] += 1;
$res[$parsedUrl][$keyword]['score1'] += $urlResults['score1'];
$res[$parsedUrl][$keyword]['score2'] += $urlResults['score2'];
$res[$parsedUrl][$keyword]['sumPosition'] += $urlResults['position'];
$res[$parsedUrl][$keyword]['position'] = $res[$parsedUrl][$keyword]['sumPosition'] / $res[$parsedUrl][$keyword]['urlCount'];
continue;
}
}
$res[$parsedUrl][$keyword] = $urlResults;
$res[$parsedUrl][$keyword]['url'] = $parsedUrl;
$res[$parsedUrl][$keyword]['urlCount'] = 1;
$res[$parsedUrl][$keyword]['sumPosition'] = $urlResults['position'];
}
var_dump($res);
- Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.4, 8.3.6 - 8.3.26, 8.4.1 - 8.4.13
- array(2) {
["https://www.example.pl/"]=>
array(1) {
["test"]=>
array(7) {
["url"]=>
string(23) "https://www.example.pl/"
["keyword"]=>
string(4) "test"
["score1"]=>
int(300)
["score2"]=>
int(150)
["position"]=>
float(2.6666666666666665)
["urlCount"]=>
int(3)
["sumPosition"]=>
int(8)
}
}
["https://www.example.pl/other-site"]=>
array(1) {
["test"]=>
array(7) {
["url"]=>
string(33) "https://www.example.pl/other-site"
["keyword"]=>
string(4) "test"
["score1"]=>
int(100)
["score2"]=>
int(50)
["position"]=>
int(3)
["urlCount"]=>
int(1)
["sumPosition"]=>
int(3)
}
}
}
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array(2) {
["https://www.example.pl/"]=>
array(1) {
["test"]=>
array(7) {
["url"]=>
string(23) "https://www.example.pl/"
["keyword"]=>
string(4) "test"
["score1"]=>
int(300)
["score2"]=>
int(150)
["position"]=>
float(2.6666666666666665)
["urlCount"]=>
int(3)
["sumPosition"]=>
int(8)
}
}
["https://www.example.pl/other-site"]=>
array(1) {
["test"]=>
array(7) {
["url"]=>
string(33) "https://www.example.pl/other-site"
["keyword"]=>
string(4) "test"
["score1"]=>
int(100)
["score2"]=>
int(50)
["position"]=>
int(3)
["urlCount"]=>
int(1)
["sumPosition"]=>
int(3)
}
}
}
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
- array(2) {
["https://www.example.pl/"]=>
array(1) {
["test"]=>
array(7) {
["url"]=>
string(23) "https://www.example.pl/"
["keyword"]=>
string(4) "test"
["score1"]=>
int(300)
["score2"]=>
int(150)
["position"]=>
float(2.6666666666667)
["urlCount"]=>
int(3)
["sumPosition"]=>
int(8)
}
}
["https://www.example.pl/other-site"]=>
array(1) {
["test"]=>
array(7) {
["url"]=>
string(33) "https://www.example.pl/other-site"
["keyword"]=>
string(4) "test"
["score1"]=>
int(100)
["score2"]=>
int(50)
["position"]=>
int(3)
["urlCount"]=>
int(1)
["sumPosition"]=>
int(3)
}
}
}
preferences:
132.93 ms | 413 KiB | 5 Q