Here is my Data set format in JSON in PHP:
$product_arr =
{
"Samiya J.": [
{
"SparePartID": "45",
"Name": "Lights",
"Rating": "5",
"CustomerID": "5",
"FirstName": "Samiya J."
},
{
"SparePartID": "49",
"Name": "Tool Kit",
"Rating": "4",
"CustomerID": "5",
"FirstName": "Samiya J."
},
{
"SparePartID": "52",
"Name": "Side Mirrors",
"Rating": "5",
"CustomerID": "5",
"FirstName": "Samiya J."
},
{
"SparePartID": "52",
"Name": "Side Mirrors",
"Rating": "4",
"CustomerID": "5",
"FirstName": "Samiya J."
},
{
"SparePartID": "45",
"Name": "Lights",
"Rating": "4",
"CustomerID": "5",
"FirstName": "Samiya J."
}
],
"Ammarah Niazi": [
{
"SparePartID": "45",
"Name": "Lights",
"Rating": "4",
"CustomerID": "13",
"FirstName": "Ammarah Niazi"
},
{
"SparePartID": "47",
"Name": "Steering Wheel",
"Rating": "4",
"CustomerID": "13",
"FirstName": "Ammarah Niazi"
}
]
}
This data is grouped on basis of person names. Now I m trying to map this data matrix by sending a person name and calculating his distance with all other persons in above matrix data.
I used the following approach but it gets me with error:
public function getRecommendations($matrix, $person)
{
foreach ( $matrix as $otherPerson => $value)
{
if($otherPerson!= $person)
{
$sim = similarity_distance($matrix, $person, $otherPerson);
var_dump($sim);
return $sim;
}
}
}
public function similarity_distance($matrix, $person1, $person2)
{
$similar = array();
$sum = 0;
foreach( $matrix[$person1] as $key => $value)
{
if (array_key_exists($key, $matrix[$person2]))
{
$similar[$key] = 1;
}
}
if($similar == 0)
{
return 0;
}
foreach( $matrix[$person1] as $key => $value)
{
if (array_key_exists($key, $matrix[$person2]))
{
$sum = $sum + pow($value - $matrix[$person2][$key],2);
}
}
return 1/(1 + sqrt($sum));
}
and calling this above function as
getRecommendations($products_arr, 'Ammarah Niazi');
ERROR I get is: Uncaught Error: Call to undefined function similarity_distance()
Can anyone tell me If Im sending correct format data for the above functions or where Im doing wrong. Im really confused.
</div>