簡體   English   中英

阿爾戈利亞:如何使用以下數據創建優化列表

[英]Algolia: How To Create a Refinement List with the following Data

我正在嘗試使用以下產品數據創建優化列表:

  1. 表1:產品
  2. 表2:產品詳細信息。

使用Table Product,可以完美地檢索所有所需數據,從而創建一個與algolia電子商務百思買示例非常相似的json文件。

{
“id”: "3953367"
“name”: “360fly - Panoramic 360° HD Video Camera - Black”,
“popularity”: 9999,
“rating”: 5,
“objectID”: “9131042”
},

另一方面,表2->產品詳細信息具有以下結構:

id - productId - name - value.
1 - 3953367 - Operating System - Android 4.4 KitKat
2 - 3953367 - Voice Activated - Yes
3 - 3953367 - Processor Speed - 1.2 gigahertz

如您所見,一個單一產品可以顯示3個以上的方面選項。

  1. 操作系統
  2. 語音激活
  3. 處理器速度

但是我不知道如何構造數據來創建這樣的優化列表:

在此處輸入圖片說明

例如,如何創建json以使人們能夠使用操作系統進行優化。

我嘗試了類似的東西:

$record[$this->productdetails->map(function($data) {return [$data[‘name’]];})->toArray()]
= 
$this->productdetails->map(function($data) {return [$data[‘value’]]; })->toArray();

但在此示例中,我收到錯誤:

非法偏移類型

任何示例贊賞。 我在Algolia中使用Laravel Scout。


基於用戶@ daniel-h方法,我更新了代碼。

public function toSearchableArray()
{
    $record['name'] = $this->productName;
    $record['description'] = $this->productSpecification;
    $record['brand'] = $this->productBrand;
    $record['color'] = $this->color;
    $new_array = [];

    foreach ($this->productdetails as $record) {
        $new_array[$record['name']] = $record['value'];
    }
    return $record;
}

目前,我正在接收array_merge():參數1不是數組


更新:

解決方案是:

// Add the product details to the array
    foreach ($this->productDetails as $detailsRecord) {
      $record[$detailsRecord->name] = $detailsRecord->value;
    }

我不知道您到底想要什么,但是錯誤是因為您不能將數組作為$ record的索引。 應該看起來像這樣:$ record [1]或$ record ['key']。

您是否正在尋找類似的東西:

$new_array = [];

foreach ($this->productdetails as $data) {
    $new_array[$data['name']] = $data['value'];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM