@nevesomostjke

Как создать массив?

Есть у меня массив такой:

stdClass Object
    [SearchResult] => stdClass Object
            [success] => 1       
            [PartsList] => stdClass Object 
                    [Part] => Array
                        [0] => stdClass Object
                                  [guid] => NSII0005959615
                                  [stocks] => stdClass Object
                                            [stock] => stdClass Object
                                                (
                                                    [id] => ТВЦНСК
                                                    [price] => 3694.78
                                                )
                        [1] => stdClass Object
                                  [guid] => NSII0005959615
                                  [stocks] => stdClass Object
                                            [stock] => Array
                                                     [0] => stdClass Object
                                                     (
                                                            [id] => HST19914228
                                                            [price] => 1427.84
                                                    )

                                                    [1] => stdClass Object
                                                    (
                                                     [id] => HST480
                                                     [price] => 997.28
                                                    )

Вот мой код:

$arr = [];
            if (!empty($result->SearchResult->PartsList->Part)) {
                foreach ($result->SearchResult->PartsList->Part as $key=>$value) {
                    $arr['ROSSKO'][$key]['guid']         =   $value->guid;
                }
            }

Мне нужен массив вида:

[0] => Array
    [guid] = '0000000'
    [stocks] => Array
          [0]=> Array
                  [id]=>0000
                  [price]=>00000

и тд.

Моя проблема в том, что я не могу положить stocks в свой массив, так как в каких то элементах stock - это объект, а в каких-то массив.
  • Вопрос задан
  • 117 просмотров
Решения вопроса 1
@nevesomostjke Автор вопроса
Мож кому пригодиться.
Глупо, но работает. По другому не получилось у меня
// Шаг 1
            $arr = [];
            if (!empty($result->SearchResult->PartsList)) {
                foreach ($result->SearchResult->PartsList as $key=>$value) {
                    if(is_array($result->SearchResult->PartsList->Part)) {
                        foreach ($value as $k1=>$v1) {
                            $arr['ROSSKO'][$k1]['name']         =   $v1->name;
                            $arr['ROSSKO'][$k1]['partnumber']   =   $v1->partnumber;
                            $arr['ROSSKO'][$k1]['brand']        =   $v1->brand; 
                            $arr['ROSSKO'][$k1]['guid']         =   $v1->guid; 

                            if (isset($v1->stocks)) {
                                $arr['ROSSKO'][$k1]['stocks'] = $v1->stocks;
                            }

                            /*
                            if (isset($v1->crosses)) {
                                $arr['ROSSKO'][$k1]['crosses'] = $v1->crosses;
                            }         
                            */                  
                        }                           
                    }
                    else {
                        $arr['ROSSKO'][0]['name']         =    $value->name;
                        $arr['ROSSKO'][0]['partnumber']   =   $value->partnumber;
                        $arr['ROSSKO'][0]['brand']        =   $value->brand;
                        $arr['ROSSKO'][0]['guid']         =   $value->guid;      
                        
                        if (isset($value->stocks)) {
                            $arr['ROSSKO'][0]['stocks'] = $value->stocks;
                        }  
                        /*
                        if (isset($value->crosess)) {
                            $arr['ROSSKO'][0]['crosess'] = $value->crosess;
                        }     
                        */                   
                    }                 
                }
            }

            // Шаг 2
            $arr2 = [];
            foreach ($arr['ROSSKO'] as $key=>$value) {
                $arr2['ROSSKO'][$key]['name'] = $value['name'];
                $arr2['ROSSKO'][$key]['partnumber'] = $value['partnumber'];
                $arr2['ROSSKO'][$key]['brand'] = $value['brand'];
                $arr2['ROSSKO'][$key]['guid'] = $value['guid'];

                if (isset($value['stocks']->stock)) {
                    if (is_array($value['stocks']->stock)) {
                        foreach ($value['stocks']->stock as $k3=>$v3) {
                            $arr2['ROSSKO'][$key]['stocks']['stock'][$k3]['id'] = $v3->id;
                            $arr2['ROSSKO'][$key]['stocks']['stock'][$k3]['price'] = $v3->price;
                            $arr2['ROSSKO'][$key]['stocks']['stock'][$k3]['count'] = $v3->count;
                            $arr2['ROSSKO'][$key]['stocks']['stock'][$k3]['description'] = $v3->description;
                        }
                    }
                    else {
                        $arr2['ROSSKO'][$key]['stocks']['stock'][0]['id'] = $value['stocks']->stock->id;
                        $arr2['ROSSKO'][$key]['stocks']['stock'][0]['price'] = $value['stocks']->stock->price;
                        $arr2['ROSSKO'][$key]['stocks']['stock'][0]['count'] = $value['stocks']->stock->price;
                        $arr2['ROSSKO'][$key]['stocks']['stock'][0]['description'] = $value['stocks']->stock->price;
                    }
                }
            }

            return $arr2;
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы