Good day. In order not to load you with unnecessary information, in brief and with examples, I will describe what the problem is. The fact is that I get some data from the MySQL database. The problem is that they need to be brought to a certain type of array, and I can’t even imagine how.

We have a table in the database and all the cells that correspond to a particular group. For example, a group with ID number 1 should be placed in one associative array.

An example of this label:

sample label

Here is an example of an array of this:

$data = array( array( 'modname' => 'Lorem', 'pricing' => array( '0' => 0.03, '1' => 0.15, '2' => 0.50 ), 'names' => array( '0' => 'Lorems +0.03', '1' => 'Ipsum +0.15', '2' => 'Dolor +0.50' ) ), array( 'modname' => 'Ipsum', 'pricing' => array( '0' => 0.08, '1' => 0.20, '2' => 0.60 ), 'names' => array( '0' => 'Sie +0.08', '1' => 'A +0.20', '2' => 'Mat +0.60' ) ) ); 

So, there is not even a presentation yet on how this can be implemented in the code. How so cells on certain ID Group to place in an array? You can use if-else, but how to do it all in an array?

I hope for your help.

UPD: I will add a small example for a better understanding:

Imagine the store, it has a product - beds.

There is a price for a bed, but there are "modifiers" that affect the price. For example: bed size, bed color, the placement of additional shelves. The point is to somehow pull values ​​from the database and group them by ID. Those. colors to colors, shelves to shelves, sizes to sizes. And it is necessary to group all this in an associative array, as in an example.

  • one
    Nipanimayu, and from the word "absolutely." Why are these entries ( 'Lorem' , 'Ipsum' ) selected for the elements of the modname array? Especially since in the table the meaning of 'Lorem' not in principle ... - Akina
  • @Akina I think this is just a person who has found a free vrIlans and does not know what to ask The question is radically different from the example. - Naumov
  • @Akina, randomly scored names. Imagine for a moment the shop, it has the goods of the bed. There is a price for a bed, but there are "modifiers" that affect the price. For example: bed size, bed color, the placement of additional shelves. The point is to somehow pull values ​​from the database and group them by ID. Those. colors to colors, shelves to shelves, sizes to sizes. - Felix
  • Well, in the query, construct the sorting in the order in which the elements should be placed in the array, and, having received the data, drive into the array in one pass. But generally you could work on the quality of the question on YOUR problem ... I suppose you don’t like the answer of the same quality? - Akina
  • @Akina, on the other I do not know yet how to implement. Here I thought somehow everything can be in one cell, but it's the same parse. In general, the point is that there are these "modifiers" and you need to implement them. I would be happy if you tell me which way to think. - Felix

1 answer 1

Suppose the source data is already extracted and are in the input array:

 $input = [ ['id_group' => 1, 'name' => "Lorem", 'price' => 0.03], ['id_group' => 2, 'name' => "Sie", 'price' => 0.08], ['id_group' => 1, 'name' => "Ipsum", 'price' => 0.15], ['id_group' => 1, 'name' => "Dolor", 'price' => 0.50], ['id_group' => 2, 'name' => "A", 'price' => 0.20], ['id_group' => 2, 'name' => "Mat", 'price' => 0.60], ]; 

it is obvious that we will process them in a loop. Every time we encounter a new id_group we must add an element to the resulting array. The name of the current element will determine the name group. After that, by the group number, we need to add the pricing and names elements.

 $result = []; foreach($input as $d){ $gId = $d['id_group']; //новая группа if(!isset($result[$gId])){ $result[$gId] = [ 'modname' => $d['name'], 'pricing' => [], 'names' => [], ]; } $result[$gId]['pricing'][] = $d['price']; $result[$gId]['names'][] = sprintf("%s %+.2f", $d['name'], $d['price']); } 

Search result:

 Array ( [1] => Array ( [modname] => Lorem [pricing] => Array ( [0] => 0.03 [1] => 0.15 [2] => 0.5 ) [names] => Array ( [0] => Lorem +0.03 [1] => Ipsum +0.15 [2] => Dolor +0.50 ) ) [2] => Array ( [modname] => Sie [pricing] => Array ( [0] => 0.08 [1] => 0.2 [2] => 0.6 ) [names] => Array ( [0] => Sie +0.08 [1] => A +0.20 [2] => Mat +0.60 ) ) 

)

  • Hello! Thanks for the answer, what you need, but I have a problem. The fact is that everything works fine with your array, but something that doesn’t work for mine comes out. Here is a screenshot of my array: i.imgur.com/daK7R0H.jpg . So, instead of the desired, you get this array: i.imgur.com/po7MAOl.jpg . PS Yes, I know that I did not enter some values ​​(id_group, id, etc.), but this is for now unnecessarily, and does not seem to affect the formation of an array. - Felix
  • Oh, so they are the same id_group, something I already have, I need to sleep, it seems. Thank. - Felix