donglang1976 2019-07-05 05:57
浏览 102

如何在PHP中以Matrix的形式映射JSON对象数组?

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>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题