There are two arrays: The names of the characteristics are stored in the first array, their values ​​are stored in the second array. It is necessary to merge them, so that later it would be possible to output the Names of the characteristic and the list of their values, well, I haven’t thought up how to do it correctly yet. Give advice for simplicity or it may not be entirely convenient to manipulate the output of data in the form of a list or checkbox.

1. Массив характеристик: array(9) { [0]=> array(5) { ["id"]=> int(1) ["name_ru"]=> string(8) "Цвет" ["alias"]=> string(6) "colour" ["type"]=> string(6) "select" } [1]=> ... } 2. Массив значений array(27) { [0]=> array(4) { ["id"]=> int(1) ["property_id"]=> int(1) ["value_ru"]=> string(14) "Красный" } [1]=> .... } 3.Предполагаемый массив array(9) { [0]=> array(5) { ["id"]=> int(1) ["name_ru"]=> string(8) "Цвет" ["alias"]=> string(6) "colour" ["type"]=> string(6) "select" ["values"] => [0] => array(4) { ["id"]=> int(1) ["property_id"]=> int(1) ["value_ru"]=> string(14) "Красный" } [1] => ..... } [1]=> ... } 
  • On what grounds should they be combined? - etki
  • In SO, the answer is taken as an answer - even when you give it yourself. React question can correct errors or clarify the conditions. - artoodetoo
  • Until you issue your answer, the question will remain without a decision. This is bad. - artoodetoo

3 answers 3

I understand that arrays must be combined by id in the array of characteristics? In that case, I think that array_filter() what you need.

Scroll through an array of characteristics by applying a filter to an array of values.

Something like this:
$properties - an array of characteristics
$values - an array of values

 for($i=0; $i<Count($properties); $i++) { $property_id = $properties[$i]["id"]; $properties[$i]["values"] = array_filter($values, function($el) { if ($el["property_id"]==$property_id) ? 1 : 0; } ); } 

    This code should add values ​​to the source attribute array:

     array_walk( $attributes, function(&$item, $key) use($values) { $item += [ 'values' => $values[$key] ]; } ); 

    Here I proceed from the fact that gluing occurs by a simple array index.

    Update: As it turned out suddenly, the records are linked by id - property_id :

     array_walk( $attributes, function(&$item) use($values) { foreach ($values as $key => $value) { if ($value['property_id'] == $item['id']) break; } $item += ['values' => $values[$key]]; } ); 

      The issue is resolved, done like this:

        private function mmarge_array($array, $array_value){ $newArray = array(); for ($i = 0; $i < count($array); $i++) { $newArray[$i] = array( 'id' => $array[$i]['id'], 'name' => $array[$i]['name'], 'alias' => $array[$i]['alias'], 'type' => $array[$i]['type'], ); for ($j = 0; $j < count($array_value); $j++) { if ($array[$i]['id'] == $array_value[$j]['property_id']) { $newArray[$i]['values'][] = array( 'id' => $array_value[$j]['id'], 'value' => $array_value[$j]['value'], ); } } } return $newArray; }