G
G
Grigory Vasilkov2017-01-14 21:35:43
PHP
Grigory Vasilkov, 2017-01-14 21:35:43

What "Hindu code" converts an array into a relational structure of arrays?

There is such an array - in other words - a product from the price list

$arr = array(
    "param1" => "value",
    "param2" => "value",
    "param3" => array("value", "value2"),
    "param4" => array("value", "value2"),
    "param5" => "value",
    "param6" => array("value", "value2"),
    "param7" => "value",
    "param8" => array("value", "value2"),
  );

The following code can convert it into an array for the param_enum relational table
$param_enum = array();
foreach ($arr as $k => $v):
  if (is_array($v)):
    foreach ($v as $v2):
      $param_enum[] = array(
        "param" => $k,
        "enum" => $v2
      );
    endforeach;
  else:
    $param_enum[] = array(
      "param" => $k,
      "enum" => $v
    );
  endif;
endforeach;

The answer will be like this:
array(
  0 => array(
    "param" => "param1",
    "enum" => "value"
  ),
  1 => array(
    "param" => "param2",
    "enum" => "value"
  ),
  2 => array(
    "param" => "param3",
    "enum" => "value"
  ),
  3 => array(
    "param" => "param3",
    "enum" => "value2"
  ),
  4 => array(
    "param" => "param4",
    "enum" => "value"
  ),
  5 => array(
    "param" => "param4",
    "enum" => "value2"
  ),
  6 => array(
    "param" => "param5",
    "enum" => "value"
  ),
  7 => array(
    "param" => "param6",
    "enum" => "value"
  ),
  8 => array(
    "param" => "param6",
    "enum" => "value2"
  ),
  9 => array(
    "param" => "param7",
    "enum" => "value"
  ),
  10 => array(
    "param" => "param8",
    "enum" => "value"
  ),
  11 => array(
    "param" => "param8",
    "enum" => "value2"
  )
)

After that, I can do pluck("param") and pluck("enum") and even then not really, I will need to associatively parse enum so that they are unique within the param table, and not generally unique in order to get unique values ​​for filling to the `param` and `enum` tables.
In response to such a fill, I will receive the ID of the records. The

question is - how to further put down in the arrays the correspondence of the fields by ID using a few lines of code?

Is there really no easier way than foreach () { array_search() }?
It's just that this task is so common that the coders must have come up with a good solution for a long time, who will tell you

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Vasilkov, 2017-01-17
@gzhegow

While the current version - there is no question of any universality yet, use at your own peril and risk
Download the source : --temporarily_unavailable--
The code turned out to be greasy, so prepare cigars and popcorn
Picture with the results: https://www.screencast.com/ t/nF8qygECj

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question