app\helpers\featuresHelper::oldFeatures PHP Method

oldFeatures() public method

@param [array] $productFeatures old product features or empty array
public oldFeatures ( [array] $productFeatures ) : array
$productFeatures [array]
return array
    public function oldFeatures($productFeatures)
    {
        $return = [];
        foreach ($this->features->toArray() as $row) {
            if (isset($productFeatures[$row['indexByName']])) {
                if ($row['max_num_values'] * 1 == 1) {
                    if (is_array($productFeatures[$row['indexByName']][0])) {
                        $return['feature_' . $row['indexByName']] = $productFeatures[$row['indexByName']][0][0];
                    } else {
                        $return['feature_' . $row['indexByName']] = $productFeatures[$row['indexByName']][0];
                    }
                } else {
                    for ($i = 0; $i <= $row['max_num_values']; $i++) {
                        if (isset($productFeatures[$row['indexByName']][$i])) {
                            if (is_array($productFeatures[$row['indexByName']][$i])) {
                                $return['feature_' . $row['indexByName'] . '_' . ($i + 1)] = $productFeatures[$row['indexByName']][$i][0];
                            } else {
                                $return['feature_' . $row['indexByName'] . '_' . ($i + 1)] = $productFeatures[$row['indexByName']][$i];
                            }
                        } else {
                            $return['feature_' . $row['indexByName'] . '_' . ($i + 1)] = '';
                        }
                    }
                }
            } else {
                if ($row['max_num_values'] * 1 == 1) {
                    $return['feature_' . $row['indexByName']] = '';
                } else {
                    for ($i = 1; $i <= $row['max_num_values']; $i++) {
                        $return['feature_' . $row['indexByName'] . '_' . $i] = '';
                    }
                }
            }
        }
        return $return;
    }

Usage Example

 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($id)
 {
     $feature = ProductDetail::find($id);
     if ($feature) {
         $panel = $this->panel;
         $arrayExample = [];
         if ($feature->max_num_values > 1) {
             for ($i = 0; $i < $feature->max_num_values; $i++) {
                 if (isset($feature->help_message['general'])) {
                     $arrayExample[$i] = ['data example', $feature->help_message['general']];
                 } elseif (isset($feature->help_message['specific'])) {
                     $arrayExample[$i] = ['data example', $feature->help_message['specific'][$feature->indexByName . '_' . $i]];
                 } elseif (isset($feature->help_message['general_selection'])) {
                     $arrayExample[$i] = ['data example', 'help message selected'];
                 } elseif (isset($feature->help_message['specific_selection'])) {
                     $arrayExample[$i] = ['data example', 'help message selected to value' . $i];
                 } else {
                     $arrayExample[$i] = 'data example';
                 }
             }
         } else {
             if (isset($feature->help_message['general'])) {
                 $arrayExample = ['data example', $feature->help_message['general']];
             } elseif (isset($feature->help_message['general_selection'])) {
                 $arrayExample = ['data example', 'help message selected'];
             } else {
                 $arrayExample = 'data example';
             }
         }
         $features = [str_replace(" ", "", $feature['name']) => $arrayExample];
         $productsDetails = new featuresHelper();
         $oldFeatures = $productsDetails->oldFeatures([]);
         $edit = false;
         return view('features.show', compact('panel', 'feature', 'features', 'oldFeatures', 'edit'));
     } else {
     }
 }