Люди спасайте)) ...есть скрипт...который выбирает инфу с таблицы полученной из 1с, и проверяет с таблицей что уже загружена на сайт, так вот..как можно этот код укоротить, я в php новичок ..поэтому не сильно ругайтесь)) PHP: <?php traitSync1C { protected function API_setstoreactualityfull() { set_time_limit(0); $fh = fopen('//API_setstoreactualityfull.txt', 'w'); fwrite($fh, file_get_contents('php://input')); fclose($fh); $fh = fopen('//API_setstoreactualityfull.txt', 'r'); $incData = fread($fh, filesize('//API_setstoreactualityfull.txt')); fclose($fh); $incData = json_decode($incData); if (!$incData || $incData == '') $this->_terminateByError('setstoreactuality: Данные не получены!'); $t = '#type'; $v = '#value'; $this->SQL->query("DROP TABLE IF EXISTS _tmp_products_1c"); $this->SQL->query("DROP TABLE IF EXISTS _tmp_products_stores_quantity"); $this->SQL->query("CREATE TABLE _tmp_products_1c LIKE products_1c"); $this->SQL->query("CREATE TABLE _tmp_products_stores_quantity LIKE products_stores_quantity"); $this->SQL->query("ALTER TABLE `_tmp_products_1c` AUTO_INCREMENT=1"); $incData = $incData->$v; $posStruct = $this->SQL->struct('products_1c'); unset($posStruct['record_id']); unset($posStruct['record_datetime']); $transform = ['id' => 'product_1c_id', 'art' => 'product_article', 'brand' => 'product_brand', 'name' => 'product_name', 'store_quantity' => 'store_quantity', 'unit' => 'product_unit', 'store_id' => 'store_id', 'buy_price' => 'buy_price', 'sell_price' => 'sell_price', ]; $failArticle = []; $storeList = []; $posQuantity = $storesExists = $storesAdded = $rowsWithoutId1c = $posAddes = 0; $incDataStores = $incData->row[0][0]->$v; $incData = $incData->row[0][1]->$v; $result = ['positions' => [], 'quantity' => []]; foreach($incDataStores as $key => $store) { $store = $store->$v; $code = $this->SQL->protect(trim($store[0]->Value->$v)); $name = $this->SQL->protect(trim($store[1]->Value->$v)); $storeList[$code] = ['name' => $name, '1c_id' => $code, 'id' => '', ]; $storeList[$code]['id'] = $this->SQL->getRow("SELECT * FROM products_stores WHERE store_1c_id = '$code'"); if (!empty($storeList[$code]['id'])) { $storeList[$code]['id'] = $storeList[$code]['id']['store_id']; $storesExists++; } else { $storesAdded++; $storeList[$code]['id'] = $this->SQL->insert('products_stores', ['store_1c_id' => $code, 'store_name' => $name, ]); } } foreach($incData as $index => $data) { unset($incData[$index]); $pos = $posStruct; foreach($data->$v as $col) { $cname = strtolower($col->name->$v); $cval = $col->Value->$v; $cval = $this->SQL->protect(trim($cval)); if (isset($transform[$cname])) $pos[$transform[$cname]] = $cval; } if ($pos['product_1c_id'] != '') { if ($pos['product_article'] == '') { $failArticle[] = $pos['product_1c_id']; } else { $idQuery = "SELECT DISTINCT product_id FROM products_articles WHERE ( sku = '1c:$pos[product_article]' OR sku = '$pos[product_article]' ) AND available = 1"; $pos['product_id'] = $this->SQL->getRow($idQuery); if (!isset($pos['product_id']['product_id'])) { $failArticle[] = $pos['product_1c_id']; } else { $pos['product_id'] = $pos['product_id']['product_id']; } } $pos['store_id'] = $storeList[$pos['store_id']]['id']; $result['quantity'][$pos['product_1c_id']][$pos['store_id']] = $pos['store_quantity']; unset($pos['store_id']); unset($pos['store_quantity']); $result['positions'][$pos['product_1c_id']] = $pos; unset($pos); $posQuantity++; } else { $rowsWithoutId1c++; } } foreach($result['positions'] as $key1c => $pos) { $recordId = $this->SQL->insert('_tmp_products_1c', $pos); if (isset($result['quantity'][$key1c]) && !empty($result['quantity'][$key1c])) { foreach($result['quantity'][$key1c] as $storeId => $quant) { $this->SQL->insert('_tmp_products_stores_quantity', ['products_1c_rec_id' => $recordId, 'store_id' => (int)$storeId, 'store_quantity' => (float)$quant, ]); } } unset($result['quantity'][$key1c]); unset($result['positions'][$key1c]); unset($pos); } $this->SQL->query("DELETE FROM products_1c"); $this->SQL->query("INSERT INTO products_1c SELECT * FROM _tmp_products_1c"); $this->SQL->query("DELETE FROM products_stores_quantity"); $this->SQL->query("INSERT INTO products_stores_quantity SELECT * FROM _tmp_products_stores_quantity"); $this->SQL->query("DROP TABLE _tmp_products_1c"); $this->SQL->query("DROP TABLE _tmp_products_stores_quantity"); $this->_terminateBySuccess(['stores_exists' => $storesExists, 'stores_addes' => $storesAdded, 'total_products' => $posQuantity + $rowsWithoutId1c, 'available_products' => $posQuantity, 'filed_empty_id' => $rowsWithoutId1c, 'failed_articles' => count($failArticle) , 'failed_articles_list' => $failArticle, ]); } } ?>
Извините Код (Text): <?php trait Sync1C { protected function API_setstoreactualityfull() { set_time_limit(0); $fh = fopen( '//API_setstoreactualityfull.txt', 'w' ); fwrite( $fh, file_get_contents('php://input') ); fclose($fh); $fh = fopen( '//API_setstoreactualityfull.txt', 'r' ); $incData = fread( $fh, filesize('//logs/API_setstoreactualityfull.txt') ); fclose($fh); $incData = json_decode( $incData ); if ( !$incData || $incData == '' ) $this->_terminateByError('setstoreactuality: Данные не получены!'); $t = '#type'; $v = '#value'; $incData = $incData->$v; $posStruct = $this->SQL->struct('orders_states'); unset( $posStruct['stateId'] ); unset( $posStruct['stateCode'] ); unset( $posStruct['stateCode1c'] ); unset( $posStruct['stateName'] ); $transform = [ 'id' => 'stateId', 'art' => 'stateCode', 'brand' => 'stateCode1c', 'name' => 'stateName', 'store_quantity' => 'store_quantity', 'unit' => 'product_unit', 'store_id' => 'store_id', 'buy_price' => 'buy_price', 'sell_price' => 'sell_price', ]; $failArticle = []; $storeList = []; $posQuantity = $storesExists = $storesAdded = $rowsWithoutId1c = $posAddes = 0; $incDataStores = $incData->row[0][0]->$v; $incData = $incData->row[0][1]->$v; $result = [ 'positions' => [], 'quantity' => [] ]; foreach ($incDataStores as $key => $store) { $store = $store->$v; $code = $this->SQL->protect( trim( $store[0]->Value->$v ) ); $name = $this->SQL->protect( trim( $store[1]->Value->$v ) ); $storeList[ $code ] = [ 'name' => $name, '1c_id' => $code, 'id' => '', ]; $storeList[ $code ][ 'id' ] = $this->SQL->getRow( "SELECT * FROM products_stores WHERE store_1c_id = '$code'" ); if ( !empty( $storeList[ $code ][ 'id' ] ) ) { $storeList[ $code ][ 'id' ] = $storeList[ $code ][ 'id' ][ 'store_id' ]; $storesExists++; } else { $storesAdded++; $storeList[ $code ][ 'id' ] = $this->SQL->insert( 'products_stores', [ 'store_1c_id' => $code, 'store_name' => $name, ] ); } } foreach ($incData as $index => $data) { unset( $incData[ $index ] ); $pos = $posStruct; foreach ($data->$v as $col) { $cname = strtolower( $col->name->$v ); $cval = $col->Value->$v; $cval = $this->SQL->protect( trim( $cval ) ); if ( isset( $transform[ $cname ] ) ) $pos[ $transform[ $cname ] ] = $cval; } if ( $pos['product_1c_id'] != '' ) { if ( $pos['product_article'] == '' ) { $failArticle[] = $pos['product_1c_id']; } else { $idQuery = "SELECT DISTINCT product_id FROM products_articles WHERE ( sku = '1c:$pos[product_article]' OR sku = '$pos[product_article]' ) AND available = 1"; $pos['product_id'] = $this->SQL->getRow( $idQuery ); if ( !isset($pos['product_id']['product_id']) ) { $failArticle[] = $pos['product_1c_id']; } else { $pos['product_id'] = $pos['product_id']['product_id']; } } $pos[ 'store_id' ] = $storeList[ $pos[ 'store_id' ] ]['id']; $result['quantity'][ $pos['product_1c_id'] ][ $pos['store_id'] ] = $pos['store_quantity']; unset( $pos['store_id'] ); unset( $pos['store_quantity'] ); $result['positions'][ $pos['product_1c_id'] ] = $pos; unset( $pos ); $posQuantity++; } else { $rowsWithoutId1c++; } } foreach ($result['positions'] as $key1c => $pos) { $recordId = $this->SQL->insert( '_tmp_products_1c', $pos ); if ( isset( $result['quantity'][ $key1c ] ) && !empty( $result['quantity'][ $key1c ] ) ) { foreach ($result['quantity'][ $key1c ] as $storeId => $quant) { $this->SQL->insert( '_tmp_products_stores_quantity', [ 'products_1c_rec_id' => $recordId, 'store_id' => (int)$storeId, 'store_quantity' => (float)$quant, ] ); } } unset( $result['quantity'][ $key1c ] ); unset( $result['positions'][ $key1c ] ); unset( $pos ); } $this->SQL->query( "DELETE FROM products_1c" ); $this->SQL->query( "INSERT INTO products_1c SELECT * FROM _tmp_products_1c" ); $this->SQL->query( "DELETE FROM products_stores_quantity" ); $this->SQL->query( "INSERT INTO products_stores_quantity SELECT * FROM _tmp_products_stores_quantity" ); $this->SQL->query( "DROP TABLE _tmp_products_1c" ); $this->SQL->query( "DROP TABLE _tmp_products_stores_quantity" ); $this->_terminateBySuccess([ 'stores_exists'=>$storesExists, 'stores_addes'=>$storesAdded, 'total_products'=>$posQuantity+$rowsWithoutId1c, 'available_products'=>$posQuantity, 'filed_empty_id'=>$rowsWithoutId1c, 'failed_articles'=>count($failArticle), 'failed_articles_list'=>$failArticle, ]); } } ?>
Хочешь ускориться записывай, из 1с в базу MySQL. А потом уже у себя в коде и проверяй. А то у тебя там куча не понятного кода, ты бы хоть прокомментировал пошагово что ты делаешь... А то гадание на 7 камнях, не айс. --- Добавлено --- А то исходя из того что ты написал чувак. То тут ты можешь получить массив из базы. 1С и Массив из таблицы PHP: $result = array_diff($Выборка1, $Выборка2); if (!empty($result)) { // То твои массивы различны, а значит делай дальше уже что тебе надо. }