V
V
Vitaly Smolin2015-09-02 14:55:53
PHP
Vitaly Smolin, 2015-09-02 14:55:53

How to merge together values ​​in an array if their values ​​are the same in one key?

Good afternoon colleagues.
Please, help me solve a couple of questions. There is a site on modx evo, I am uploading a price list, but this is not a store, but a company providing services. On the service page, I created a multiTV field with the name, price and stock price.
In the price list (csv) the format is: Section, Subsection, Service, Price, Promotion price. Since there are no document IDs that need to be merged into, I check by the section and subsection fields, I ran into a couple of difficulties for me:
1. How can I merge the Service values ​​​​in the array, with the same Subsection field, now I get the following array at the output:

Array
(
    [0] => Array
        (
            [tv] => Array
                (
                    [4] => КЛЕТОЧНАЯ КОСМЕТОЛОГИЯ
                    [35] => prp
                    [41] => PRP-терапия (2 пробирки )15 000 ₽
                )

            [] => Array
                (
                    [] => 
                )

            [content] => Array
                (
                    [parent] => 19
                )

        )

    [1] => Array
        (
            [tv] => Array
                (
                    [4] => КЛЕТОЧНАЯ КОСМЕТОЛОГИЯ
                    [35] => prp
                    [41] => PRP-терапия (4пробирки )25 000 ₽
                )

            [] => Array
                (
                    [] => 
                )

            [content] => Array
                (
                    [parent] => 19
                )

        )

)

It turns out that I need to merge the tv->41 fields together if they have the same tv->35 values.
Also, when draining, you need to format tv->41 into a JSON string. Something like:
{

"fieldValue":[
    {
        "name":"PRP-терапия (2 пробирки )",
        "price":"15000",
        "price_action":""
    }
],

"fieldSettings":{"autoincrement":1}

}

M... yes, I forgot to clarify, now, when draining, I am merging the Service, Price, Promotion Price fields into one because it will be in json format.
$name = $output_arr['tv']['1'];
    $price = str_replace('руб.', '₽', $output_arr['tv']['2']);
    $price_acton = $output_arr['tv']['3'];
    $output_arr['tv']['41'] = $name.$price.$price_acton ;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Ad4ptec, 2015-09-02
@Ad4ptec

Final version:

$result = $tempArr ;
  
  foreach (array_keys($result) as $key1) {
    unset($result[$key1][tv][4]);
    unset($result[$key1][tv][41]);
  }
  
  $output = array_intersect_key($tempArr, array_map("unserialize", array_unique(array_map("serialize", $result))));

  foreach (array_keys($output) as $key2) {
    unset($output[$key2][tv][41]);
    foreach (array_keys($tempArr) as $key3) {
      if ($output[$key2][tv][35] == $tempArr[$key3][tv][35]) {
        $output[$key2][tv][41] .= $tempArr[$key3][tv][41] . "<br/>\n";
      }
    }
  }
  
  print_r($output);

V
Vitaly Smolin, 2015-09-03
@studev

Now the array looks like this:

$tempArr = array(
    array(
        "tv" => array(
            "4" => "ЭСТЕТИЧЕСКАЯ КОСМЕТОЛОГИЯ",
            "35" => "ПИЛИНГИ skin tech",
            "41" => "Easy  Peel - срединный пилинг  руки (дерматологический пилинг)2 800 ₽"
        ),

    ),
    array(
        "tv" => array(
            "4" => "ЭСТЕТИЧЕСКАЯ КОСМЕТОЛОГИЯ",
            "35" => "ПИЛИНГИ skin tech",
            "41" => "Easy Peel - срединный пилинг лицо и шея (дерматологический пилинг)7 000 ₽"
        ),

    ),
    array(
        "tv" => array(
            "4" => "ЭСТЕТИЧЕСКАЯ КОСМЕТОЛОГИЯ",
            "35" => "ПИЛИНГИ skin tech",
            "41" => "Easy Peel- срединный пилинг  лицо (дерматологический пилинг)4 800 ₽"
        ),
    ),
    array(
        "tv" => array(
            "4" => "ИНЪЕКЦИОННАЯ КОСМЕТОЛОГИЯ",
            "35" => "БИОРЕВИТАЛИЗАЦИЯ",
            "41" => "IAL-SYSTEM 1.1 мл (мезотерапия с использованием препаратов  гиалуроновой кислоты)от 3х процедур12 125 ₽"
        ),
    ),
    array(
        "tv" => array(
            "4" => "ИНЪЕКЦИОННАЯ КОСМЕТОЛОГИЯ",
            "35" => "БИОРЕВИТАЛИЗАЦИЯ",
            "41" => "IAL-SYSTEM 1.1 мл (мезотерапия с использованием препаратов  гиалуроновой кислоты)от 5х процедур11 500 ₽"
        ),
    ),
);

The output would be something like:
$tempArr = array(
    array(
        "tv" => array(
            "4" => "ЭСТЕТИЧЕСКАЯ КОСМЕТОЛОГИЯ",
            "35" => "ПИЛИНГИ skin tech",
            "41" => "Easy  Peel - срединный пилинг  руки (дерматологический пилинг)2 800, ₽ Easy Peel - срединный пилинг лицо и шея (дерматологический пилинг)7 000 ₽, Easy Peel- срединный пилинг  лицо (дерматологический пилинг)4 800 ₽"
        ),

    ),
    array(
        "tv" => array(
            "4" => "ИНЪЕКЦИОННАЯ КОСМЕТОЛОГИЯ",
            "35" => "БИОРЕВИТАЛИЗАЦИЯ",
            "41" => "IAL-SYSTEM 1.1 мл (мезотерапия с использованием препаратов  гиалуроновой кислоты)от 3х процедур12 125 ₽, IAL-SYSTEM 1.1 мл (мезотерапия с использованием препаратов  гиалуроновой кислоты)от 5х процедур11 500 ₽"
        ),
    ),
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question