S
S
smoln2018-09-11 17:07:01
PHP
smoln, 2018-09-11 17:07:01

How to make such a structure from a cycle?

There is an output loop from the mysql database

while ($tmyrow = mysqli_fetch_array($tresult)){ 
Номер 1
номер товара 45
------------
Номер 2
номер товара 23
}

How can I make and output a structure like this:
"Товары"=> array( 
 [
        "Номер"=>"1",
        "номер товара"=>"45"
],
 [
        "Номер"=>"2",
        "номер товара"=>"23"
]
)

maybe I'm dumb...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-09-11
@webinar

$arr = [];
while ($tmyrow = mysqli_fetch_array($tresult)){ 
  $arr['товары'][] = [
                 'номер' => $tmyrow['some_key_1'], 
                 'номер товара' => $tmyrow['some_key_2']
                  ];
}

S
smoln, 2018-09-12
@smoln

There are two cycles

$return_arr = [];
while ($myrow = mysqli_fetch_array($result)){
  $return_arr[]=[
        "Партнер_Key"=>"$myrow[4]",
    "Контрагент_Key"=>"$myrow[2]",
    "Организация_Key"=>"8dc9cf36-424b-11e8-bde7-99eede49b4b6", 
    "Склад_Key"=>"03460e63-af82-11e8-ba81-7c8bca035cde",
    "Валюта_Key"=>"80f5e10f-d438-11e7-988b-ece0db457431",
    "Соглашение_Key"=>"$myrow[5]",
    "ЦенаВключаетНДС"=>true,
    "СуммаДокумента"=>"$total_price",
    "РегистрироватьЦеныПоставщика"=>true,
    "Согласован"=>true,
    ];

 $k=1;
  while ($tmyrow = mysqli_fetch_array($tresult)){ 
 $return_arr['Tовары'][] = [
          "LineNumber"=>"$k",
          "НоменклатураПоставщика_Key"=>"$items_id_1c",
           "Номенклатура_Key"=>"$item_id_1c",
           "Количество"=>"$tmyrow[5]",
           "ВидЦеныПоставщика_Key"=>"00000000-0000-0000-0000-000000000000",
           "Цена"=>"$tmyrow[7]",
           "Сумма"=>"$summ_orderline",
           "СтавкаНДС"=>"НДС18",
           "КодСтроки"=>"$tmyrow[6]",
           "Отменено"=>false,
           "Склад_Key"=>"03460e63-af82-11e8-ba81-7c8bca035cde"
        ];
$k++;
}

}

print "<pre>";
print_r($return_arr);
print "</pre>";

In this code, all products are displayed after the first pass, but they should be in every cycle.
Here's what it outputs
Array
(
    [0] => Array
        (
            [Партнер_Key] => ba302cf9-b0db-11e8-ba81-7c8bca035cde
            [Контрагент_Key] => ba302cfa-b0db-11e8-ba81-7c8bca035cde
            [Организация_Key] => 8dc9cf36-424b-11e8-bde7-99eede49b4b6
            [Склад_Key] => 03460e63-af82-11e8-ba81-7c8bca035cde
            [Валюта_Key] => 80f5e10f-d438-11e7-988b-ece0db457431
            [Соглашение_Key] => ba302cfc-b0db-11e8-ba81-7c8bca035cde
            [ЦенаВключаетНДС] => 1
            [СуммаДокумента] => 2062.9000244140625
            [РегистрироватьЦеныПоставщика] => 1
            [Согласован] => 1
        )

    [Tовары] => Array
        (
            [0] => Array
                (
                    [LineNumber] => 1
                    [НоменклатураПоставщика_Key] => 
                    [Номенклатура_Key] => 
                    [Количество] => 2
                    [ВидЦеныПоставщика_Key] => 00000000-0000-0000-0000-000000000000
                    [Цена] => 615.76
                    [Сумма] => 1231.52
                    [СтавкаНДС] => НДС18
                    [КодСтроки] => 5778
                    [Отменено] => 
                    [Склад_Key] => 03460e63-af82-11e8-ba81-7c8bca035cde
                )

            [1] => Array
                (
                    [LineNumber] => 2
                    [НоменклатураПоставщика_Key] => 
                    [Номенклатура_Key] => 
                    [Количество] => 1
                    [ВидЦеныПоставщика_Key] => 00000000-0000-0000-0000-000000000000
                    [Цена] => 831.38
                    [Сумма] => 831.38
                    [СтавкаНДС] => НДС18
                    [КодСтроки] => 5777
                    [Отменено] => 
                    [Склад_Key] => 03460e63-af82-11e8-ba81-7c8bca035cde
                )

            [2] => Array
                (
                    [LineNumber] => 1
                    [НоменклатураПоставщика_Key] => 
                    [Номенклатура_Key] => 
                    [Количество] => 2
                    [ВидЦеныПоставщика_Key] => 00000000-0000-0000-0000-000000000000
                    [Цена] => 896
                    [Сумма] => 1792
                    [СтавкаНДС] => НДС18
                    [КодСтроки] => 5772
                    [Отменено] => 
                    [Склад_Key] => 03460e63-af82-11e8-ba81-7c8bca035cde
                )

            [3] => Array
                (
                    [LineNumber] => 1
                    [НоменклатураПоставщика_Key] => 
                    [Номенклатура_Key] => 
                    [Количество] => 2
                    [ВидЦеныПоставщика_Key] => 00000000-0000-0000-0000-000000000000
                    [Цена] => 58.5
                    [Сумма] => 117
                    [СтавкаНДС] => НДС18
                    [КодСтроки] => 5769
                    [Отменено] => 
                    [Склад_Key] => 03460e63-af82-11e8-ba81-7c8bca035cde
                )

        )

    [1] => Array
        (
            [Партнер_Key] => 148fa494-6011-11e8-bbb0-cc0593f2be79
            [Контрагент_Key] => a5c28eea-9eef-11e8-ba81-7c8bca035cde
            [Организация_Key] => 8dc9cf36-424b-11e8-bde7-99eede49b4b6
            [Склад_Key] => 03460e63-af82-11e8-ba81-7c8bca035cde
            [Валюта_Key] => 80f5e10f-d438-11e7-988b-ece0db457431
            [Соглашение_Key] => 81aadc13-af44-11e8-ba81-7c8bca035cde
            [ЦенаВключаетНДС] => 1
            [СуммаДокумента] => 1792
            [РегистрироватьЦеныПоставщика] => 1
            [Согласован] => 1
        )

    [2] => Array
        (
            [Партнер_Key] => 65776b2c-b4cb-11e8-ba81-7c8bca035cde
            [Контрагент_Key] => 65776b2d-b4cb-11e8-ba81-7c8bca035cde
            [Организация_Key] => 8dc9cf36-424b-11e8-bde7-99eede49b4b6
            [Склад_Key] => 03460e63-af82-11e8-ba81-7c8bca035cde
            [Валюта_Key] => 80f5e10f-d438-11e7-988b-ece0db457431
            [Соглашение_Key] => 65776b2f-b4cb-11e8-ba81-7c8bca035cde
            [ЦенаВключаетНДС] => 1
            [СуммаДокумента] => 117
            [РегистрироватьЦеныПоставщика] => 1
            [Согласован] => 1
        )

)

How can I get it right?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question