Created
January 28, 2018 10:42
-
-
Save lasida/254197b630b828f48441ded97c35fea6 to your computer and use it in GitHub Desktop.
Sum Value by Id Array Multidimensional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = array( | |
array( '231', 'http://dalamislam.com/akhlaq/amalan-shaleh/cara-agar-tetap-istiqomah-di-jalan-allah', '1'), | |
array( '231', 'http://dalamislam.com/akhlaq/amalan-shaleh/cara-agar-tetap-istiqomah-di-jalan-allah/', '2'), | |
array( '231', 'http://dalamislam.com/akhlaq/amalan-shaleh/cara-agar-tetap-istiqomah-di-jalan-allah/amp', '3'), | |
array( '21221', 'http://dalamislam.com/akhlaq/amalan-shaleh/cara-agar-tetap-istiqomah', '4'), | |
array( '1', 'http://dalamislam.com/akhlaq/amalan-shaleh/cara-agar-tetap-istiqomah-di-jalan-allah', '5'), | |
array( '1', 'http://dalamislam.com/akhlaq/amalan-shaleh/cara-agar-tetap-istiqomah-di-jalan-allah/', '10'), | |
array( '2', 'http://dalamislam.com/akhlaq/amalan-shaleh/cara-agar-tetap-istiqomah-di-jalan-allah/amp', '15'), | |
array( '2', 'http://dalamislam.com/akhlaq/amalan-shaleh/cara-agar-tetap-istiqomah', '5'), | |
); | |
$keluaran = []; | |
foreach ($data as $key => $point) { | |
$id = $point[0]; | |
$permalink = $point[1]; | |
$permalinkin = explode("?",$permalink); | |
if(strrpos($permalinkin[0], '/') == strlen($permalinkin[0]) - 1 ){ | |
$permal = substr($permalinkin[0], 0, -1); | |
} | |
if( strpos($permalinkin[0], 'amp') !== false ){ | |
$permalinkin = explode("/amp",$permalinkin[0]); | |
} | |
array_push($keluaran, array( "id" => $id, "permalink" => $permalink, "pageview" => $point[2])); | |
} | |
$sum = array_reduce($keluaran, function ($a, $b) { | |
isset($a[$b['id']]) ? $a[$b['id']]['pageview'] += $b['pageview'] : $a[$b['id']] = $b; | |
return $a; | |
}); | |
function unique_multidim_array($array, $key) { | |
$temp_array = array(); | |
$i = 0; | |
$key_array = array(); | |
foreach($array as $val) { | |
$buffer = 0; | |
if (!in_array($val[$key], $key_array)) { | |
$key_array[$i] = $val[$key]; | |
// $buffer = $buffer + $val['pageview']; | |
// $val['pageview'] = $buffer; | |
$temp_array[$i] = $val; | |
} | |
$i++; | |
} | |
return $temp_array; | |
} | |
print_r(array_values(unique_multidim_array($sum,'id'))); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment