我有一个关于关联数组的问题我由于缺乏编程知识而无法自行解决。
我有一个从数字“1”到“12”的foreach循环。 对于每个号码,我得到一个从另一个服务器提取数据的链接。
这个数据具有json数组的形式,所以我为每个键值类型创建单个数组,我从json数据中提取(例如雇主名称;雇主ID;雇主薪水; ......)和CURL。
我需要将所有这些数据插入到数据库中,因此我创建了一个包含所有这些数据的数组,而不是将所有这些数据插入到表中。 但是......如果我必须从第一个foreach创建最终的单个数组我没有问题,但是我无法继续从第二个foreach到最后一个已经存在的最终数组...... 我需要像这样工作的原因可能是从Json数组中提取的数据包含相同的ex。雇主两次,所以我想创建最终阵列而不重复。
以下是代码:
createUrl();
function createUrl() {
$serverName = 'test';
$iscountryID = 'test';
$countryCurrency = 'test';
$countryName = 'test';
// TAX url (here I get tax data)*******************************
$urlTaxcountry = strtolower("https://www.test.org/".$serverName."/tax/".$iscountryID.".json");
// CURRENCY EXCHANGE url **********************
$urlChangeCurrencySell = strtolower("https://www.test.org/".$serverName."/exchange/gold-".$countryCurrency.".json"); // to change currency value in gold
$Skill = array(1,2,3,4,5,6,7,8,9,10,11,12);
foreach ($Skill as $workSkill) {
// JOBS url *******************************
$urlJobcountry = strtolower("https://www.test.org/".$serverName."/job/".$iscountryID."-".$workSkill.".json");
getJsonData($serverName,$iscountryID,$workSkill,$urlJobcountry,$urlChangeCurrencySell,$urlTaxcountry,$countryCurrency,$countryName);
}};
function getJsonData($serverName,$iscountryID,$workSkill,$urlJobcountry,$urlChangeCurrencySell,$urlTaxcountry,$countryCurrency,$countryName)
{
$arrEmpName = array();
$arrEmpID = array();
$arrProduct = array();
$arrQuality = array();
$arrSkill = array();
$arrSalary = array();
$arrKeys = array();
$i = 0;
if ($iscountryID != NULL) {
// FOR JOBS
$chJob = curl_init($urlJobcountry);
curl_setopt($chJob, CURLOPT_RETURNTRANSFER, true);
$resultJob = curl_exec($chJob);
$outLoopJob = json_decode($resultJob, true);
$marketCountryID = $outLoopJob['country']['id'];
$marketCountryName = $outLoopJob['country']['name'];
$f = 0;
$dataJob = $outLoopJob['offers'];
foreach($dataJob as $job)
{
$empCompanyID = ($job['company']['id']);
$empCompanyName = ($job['company']['name']);
$empProduct = ($job['product']);
$empQuality = ($job['quality']);
$empSkill = ($job['skill']);
$empSalary = ($job['salary']);
// CHECK
echo "<br>empCompanyID: ".$empCompanyID;
echo "<br>empCompanyName: ".$empCompanyName;
echo "<br>empProduct: ".$empProduct;
echo "<br>empQuality: ".$empProduct;
echo "<br>empSkill: ".$empSkill;
echo "<br>empSalary: ".$empSalary;
$arrEmpName[] = $empCompanyName;
$arrEmpID[] = $empCompanyID;
$arrProduct[] = $empProduct;
$arrQuality[] = $empQuality;
$arrSkill[] = $empSkill;
$arrSalary[] = $empSalary;
$arrKeys[] = $i++;
$r++;
}}
//$result = array_merge($arrKeys, $arrEmpName, $arrEmpID, $arrProduct, $arrQuality, $arrSkill, $arrSalary);
$allJobs = array();
foreach ($arrKeys as $id => $key) {
if (empty($allJobs)) {
$allJobs[$key] = array(
'EmployerName' => $arrEmpName[$id],
'EmployerID' => $arrEmpID[$id],
'Product' => $arrProduct[$id],
'Quality' => $arrQuality[$id],
'Skill' => $arrSkill[$id],
'Salary' => $arrSalary[$id],);
} elseif ((!in_array($empCompanyID, $allJobs)) && (!in_array($empCompanyName, $allJobs)) && (!in_array($empProduct, $allJobs)) && (!in_array($empQuality, $allJobs)) && (!in_array($empSkill, $allJobs)) && (!in_array($empSalary, $allJobs))) {
比我尝试过:
$allJobs[$key] = array(
'EmployerName' => $arrEmpName[$id],
'EmployerID' => $arrEmpID[$id],
'Product' => $arrProduct[$id],
'Quality' => $arrQuality[$id],
'Skill' => $arrSkill[$id],
'Salary' => $arrSalary[$id]);
而不是
array_push($allJobs, ("'EmployerName' => ".$arrEmpName[$id]), ("'EmployerID' => ".$arrEmpID[$id]), "'Product' => ".$arrProduct[$id], "'Quality' => ".$arrQuality[$id], "'Skill' => ".$arrSkill[$id], "'Salary' => ".$arrSalary[$id]);
$f++;
}};
两者都不起作用...... 而代码的结尾如下:
// se no entry, write no record
if ($empCompanyID == NULL)
{
$marketCountryName = $countryName; // NBBBBBBBBBB
$empCompanyName = 'No Record';
$empProduct = 'No Record';
$empQuality = 'No Record';
$empSkill = 'No Record';
$empSalary = 'No Record';
};
}
echo "<br>this is allJobs:<br>";
print_r($allJobs);
echo "<br>";
print_r($result);
};
感谢您的时间和帮助。
更新
我们走了: 这是我从Json文件中获取的数据
{"country":
{"id":24
,"name":"Argentina"
}
,"offers":[
{"company":
{"id":12767
,"name":"Armas Q5 Argentina "
}
,"product":"Weapon"
,"quality":5
,"skill":8
,"salary":25
}
,{"company":
{"id":12767
,"name":"Armas Q5 Argentina "
}
,"product":"Weapon"
,"quality":5
,"skill":1
,"salary":25
}
,{"company":
{"id":16587
,"name":"International bank of Railman"
}
,"product":"Iron"
,"quality":1
,"skill":1
,"salary":1
}
]
,"load":1.4469039440155
}
此特定数据是从对应于文件阿根廷的文件中获得的,具有工作技能8。 您必须检查所有技能级别以获得从1到12的完整作业市场数据。每个页面仅返回15个结果,并且可能发生“公司xx”也出现在Skill Work ex的页面中。 “3”和“2”。
我要做的是从所有工作技能页面(从1到12)中提取数据并创建一个不重复的数组,如:
[0] => ['EmployerName'] => test ['EmployerID'] => 18768 ['Product'] => test ['Quality'] => 5 ['Skill'] => 12 ['Salary'] => 35.5 )
[1] => ['EmployerName'] => test ['EmployerID'] => 10844 ['Product'] => test ['Quality'] => 5 ['Skill'] => 12 ['Salary'] => 35.01)
[2] => ['EmployerName'] => ...
更新2
但如果我只使用:
} elseif ((!in_array($empCompanyID, $allJobs)) && (!in_array($empCompanyName, $allJobs)) && (!in_array($empProduct, $allJobs)) && (!in_array($empQuality, $allJobs)) && (!in_array($empSkill, $allJobs)) && (!in_array($empSalary, $allJobs))) {
$allJobs[$key] = array(
'EmployerName' => $arrEmpName[$id],
'EmployerID' => $arrEmpID[$id],
'Product' => $arrProduct[$id],
'Quality' => $arrQuality[$id],
'Skill' => $arrSkill[$id],
'Salary' => $arrSalary[$id]);
$f++;
}};
我得到了这个奇怪的结果......:
this is allJobs:
Array ( [0] => Array ( [EmployerName] => SOCIVAL-01-ARM - ITA [EmployerID] => 5288 [Product] => Weapon [Quality] => 1 [Skill] => 1 [Salary] => 11.24 ) [1] => Array ( [EmployerName] => Egyptian Oil Company [EmployerID] => 18558 [Product] => Oil [Quality] => 2 [Skill] => 1 [Salary] => 11.23 ) [2] => Array ( [EmployerName] => GD & Co. [EmployerID] => 18770 [Product] => Weapon [Quality] => 1 [Skill] => 1 [Salary] => 11.2 ) [3] => Array ( [EmployerName] => Israel Weap Q3 [EmployerID] => 2533 [Product] => Weapon [Quality] => 3 [Skill] => 1 [Salary] => 10.61 ) [4] => Array ( [EmployerName] => 1weapQ5 [EmployerID] => 10431 [Product] => Weapon [Quality] => 5 [Skill] => 1 [Salary] => 7.2 ) [5] => Array ( [EmployerName] => Silent murder [EmployerID] => 16005 [Product] => Weapon [Quality] => 1 [Skill] => 1 [Salary] => 6.5 ) [6] => Array ( [EmployerName] => NK Iron [EmployerID] => 16745 [Product] => Iron [Quality] => 3 [Skill] => 1 [Salary] => 5 ) [7] => Array ( [EmployerName] => Air Veneto Q1 -1 [EmployerID] => 3481 [Product] => Iron [Quality] => 2 [Skill] => 1 [Salary] => 5 ) [8] => Array ( [EmployerName] => Helvetia Eisen [EmployerID] => 10673 [Product] => Iron [Quality] => 4 [Skill] => 1 [Salary] => 3 ) [9] => Array ( [EmployerName] => Rebels' Guns [EmployerID] => 18810 [Product] => Weapon [Quality] => 1 [Skill] => 1 [Salary] => 3 ) [10] => Array ( [EmployerName] => NK weap [EmployerID] => 17382 [Product] => Weapon [Quality] => 1 [Skill] => 1 [Salary] => 1 ) [11] => Array ( [EmployerName] => GUNS N' ROSES [EmployerID] => 7378 [Product] => Weapon [Quality] => 5 [Skill] => 1 [Salary] => 1 ) [12] => Array ( [EmployerName] => Traforis [EmployerID] => 15848 [Product] => Oil [Quality] => 4 [Skill] => 1 [Salary] => 1 ) [13] => Array ( [EmployerName] => Grano Q1 [EmployerID] => 18897 [Product] => Grain [Quality] => 1 [Skill] => 1 [Salary] => 1 ) [14] => Array ( [EmployerName] => Babys Q1 Wep [EmployerID] => 1350 [Product] => Weapon [Quality] => 1 [Skill] => 1 [Salary] => 1 ) )
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => SOCIVAL-01-ARM - ITA [16] => Egyptian Oil Company [17] => GD & Co. [18] => Israel Weap Q3 [19] => 1weapQ5 [20] => Silent murder [21] => NK Iron [22] => Air Veneto Q1 -1 [23] => Helvetia Eisen [24] => Rebels' Guns [25] => NK weap [26] => GUNS N' ROSES [27] => Traforis [28] => Grano Q1 [29] => Babys Q1 Wep [30] => 5288 [31] => 18558 [32] => 18770 [33] => 2533 [34] => 10431 [35] => 16005 [36] => 16745 [37] => 3481 [38] => 10673 [39] => 18810 [40] => 17382 [41] => 7378 [42] => 15848 [43] => 18897 [44] => 1350 [45] => Weapon [46] => Oil [47] => Weapon [48] => Weapon [49] => Weapon [50] => Weapon [51] => Iron [52] => Iron [53] => Iron [54] => Weapon [55] => Weapon [56] => Weapon [57] => Oil [58] => Grain [59] => Weapon [60] => 1 [61] => 2 [62] => 1 [63] => 3 [64] => 5 [65] => 1 [66] => 3 [67] => 2 [68] => 4 [69] => 1 [70] => 1 [71] => 5 [72] => 4 [73] => 1 [74] => 1 [75] => 1 [76] => 1 [77] => 1 [78] => 1 [79] => 1 [80] => 1 [81] => 1 [82] => 1 [83] => 1 [84] => 1 [85] => 1 [86] => 1 [87] => 1 [88] => 1 [89] => 1 [90] => 11.24 [91] => 11.23 [92] => 11.2 [93] => 10.61 [94] => 7.2 [95] => 6.5 [96] => 5 [97] => 5 [98] => 3 [99] => 3 [100] => 1 [101] => 1 [102] => 1 [103] => 1 [104] => 1 ) 2
抱歉,但不能理解为什么......
更新3
以下是我的示例中的“allJobs数组”的2个示例: 第一个是从SKILL PAGE 2获得的(循环的第二个值),第二个来自SKILL PAGE 3
这是allJobs(工作技能2):
Array ( [0] => Array ( [EmployerName] => Air Veneto Q1 -1 [EmployerID] => 3481 [Product] => Iron [Quality] => 2 [Skill] => 2 [Salary] => 12.01 ) [1] => Array ( [EmployerName] => Israel Weap Q3 [EmployerID] => 2533 [Product] => Weapon [Quality] => 3 [Skill] => 2 [Salary] => 11 ) [2] => Array ( [EmployerName] => Israel Weap Q3 [EmployerID] => 2533 [Product] => Weapon [Quality] => 3 [Skill] => 1 [Salary] => 10.61 ) [3] => Array ( [EmployerName] => Traforis [EmployerID] => 15848 [Product] => Oil [Quality] => 4 [Skill] => 2 [Salary] => 9.05 ) [4] => Array ( [EmployerName] => 1weapQ5 [EmployerID] => 10431 [Product] => Weapon [Quality] => 5 [Skill] => 1 [Salary] => 7.2 ) [5] => Array ( [EmployerName] => WAR 1111 [EmployerID] => 17753 [Product] => Iron [Quality] => 5 [Skill] => 2 [Salary] => 7.1 ) [6] => Array ( [EmployerName] => Silent murder [EmployerID] => 16005 [Product] => Weapon [Quality] => 1 [Skill] => 1 [Salary] => 6.5 ) [7] => Array ( [EmployerName] => NK Iron [EmployerID] => 16745 [Product] => Iron [Quality] => 3 [Skill] => 2 [Salary] => 6.5 ) [8] => Array ( [EmployerName] => NK Iron [EmployerID] => 16745 [Product] => Iron [Quality] => 3 [Skill] => 1 [Salary] => 5 ) [9] => Array ( [EmployerName] => Air Veneto Q1 -1 [EmployerID] => 3481 [Product] => Iron [Quality] => 2 [Skill] => 1 [Salary] => 5 ) [10] => Array ( [EmployerName] => Helvetia Eisen [EmployerID] => 10673 [Product] => Iron [Quality] => 4 [Skill] => 1 [Salary] => 3 ) [11] => Array ( [EmployerName] => Olimpiaki A.E. [EmployerID] => 7156 [Product] => Ticket [Quality] => 1 [Skill] => 1 [Salary] => 1.01 ) [12] => Array ( [EmployerName] => Africa Oil Corp. [EmployerID] => 18180 [Product] => Oil [Quality] => 2 [Skill] => 1 [Salary] => 1 ) [13] => Array ( [EmployerName] => Hardika Olaj #1 [EmployerID] => 7861 [Product] => Oil [Quality] => 3 [Skill] => 1 [Salary] => 1 ) [14] => Array ( [EmployerName] => GD & Co. [EmployerID] => 18770 [Product] => Weapon [Quality] => 1 [Skill] => 1 [Salary] => 1 ) )
这是allJobs(工作技能3):
Array ( [0] => Array ( [EmployerName] => Mauzer [EmployerID] => 18756 [Product] => Weapon [Quality] => 1 [Skill] => 3 [Salary] => 12.55 ) [1] => Array ( [EmployerName] => Air Veneto Q1 -1 [EmployerID] => 3481 [Product] => Iron [Quality] => 2 [Skill] => 3 [Salary] => 12.54 ) [2] => Array ( [EmployerName] => Rebels' Guns [EmployerID] => 18810 [Product] => Weapon [Quality] => 1 [Skill] => 3 [Salary] => 12.54 ) [3] => Array ( [EmployerName] => Italy Org - Q3 Food 1 [EmployerID] => 15464 [Product] => Food [Quality] => 3 [Skill] => 3 [Salary] => 12.53 ) [4] => Array ( [EmployerName] => Petrol-ITA [EmployerID] => 16904 [Product] => Oil [Quality] => 2 [Skill] => 3 [Salary] => 12.53 ) [5] => Array ( [EmployerName] => Be or not to be Gold IronQ5 [EmployerID] => 15100 [Product] => Iron [Quality] => 5 [Skill] => 3 [Salary] => 12.52 ) [6] => Array ( [EmployerName] => Traforis [EmployerID] => 15848 [Product] => Oil [Quality] => 4 [Skill] => 3 [Salary] => 12.51 ) [7] => Array ( [EmployerName] => granopastafariano [EmployerID] => 17855 [Product] => Grain [Quality] => 1 [Skill] => 3 [Salary] => 12.5 ) [8] => Array ( [EmployerName] => GL Tickets Q1 - Italy [EmployerID] => 11990 [Product] => Ticket [Quality] => 1 [Skill] => 3 [Salary] => 12.05 ) [9] => Array ( [EmployerName] => Air Veneto Q1 -1 [EmployerID] => 3481 [Product] => Iron [Quality] => 2 [Skill] => 2 [Salary] => 12.01 ) [10] => Array ( [EmployerName] => Israel Weap Q3 [EmployerID] => 2533 [Product] => Weapon [Quality] => 3 [Skill] => 3 [Salary] => 12.01 ) [11] => Array ( [EmployerName] => Helvetia Eisen [EmployerID] => 10673 [Product] => Iron [Quality] => 4 [Skill] => 3 [Salary] => 12 ) [12] => Array ( [EmployerName] => Israel Weap Q3 [EmployerID] => 2533 [Product] => Weapon [Quality] => 3 [Skill] => 2 [Salary] => 11 ) [13] => Array ( [EmployerName] => Israel Weap Q3 [EmployerID] => 2533 [Product] => Weapon [Quality] => 3 [Skill] => 1 [Salary] => 10.61 ) [14] => Array ( [EmployerName] => Babys Q1 Wep [EmployerID] => 1350 [Product] => Weapon [Quality] => 1 [Skill] => 3 [Salary] => 10.57 ) )
如你所见,
Array ( [EmployerName] => Air Veneto Q1 -1 [EmployerID] => 3481 [Product] => Iron [Quality] => 2 [Skill] => 2 [Salary] => 12.01
重复两次,两个条目完全相同。
更新4(解决方案)
好吧,我无法用关联数组来解决我的问题,所以(这可能对其他人有用,我稍后在代码中进行检查,就在将数据插入数据库之前。
因此,当我发现所有VARS已作为存储在DB中的值存在时,代码跳转到下一部分,避免再次添加相同的值。
这是代码:
function addJobs($iscountryID, $countryName, $countryCurrency, $empCompanyID, $empCompanyName, $empProduct, $empQuality, $empSkill, $empSalary, $Changerate, $goldJob, $taxIncome, $netgoldJob, $serverName, $linktopageJob, $linktoEmployer, $dayTimeJob, $timeJob, $Currencysell, $marketCountryID, $marketCountryName)
{
// db connection
require "db-connection/db-connection2.php";
$sqlfind = "SELECT
ID
FROM cJM".$serverName."
WHERE (
iscountryID = ?
AND countryName = ? AND countryCurrency = ?
AND empCompanyID = ? AND empCompanyName = ?
AND empProduct = ? AND empQuality = ?
AND empSkill = ? AND empSalary = ?
AND goldExchange = ? AND salaryinGold = ?
AND jobTax = ? AND netSalary = ? AND Server = ?
AND time_of_record = ? AND DateSave = CURDATE()
)";
echo "<b><br><br>*********************POST***********************</b>";
echo "<br>empCompanyID: ".$empCompanyID;
echo "<br>empCompanyName: ".$empCompanyName;
echo "<br>empProduct: ".$empProduct;
echo "<br>empQuality: ".$empProduct;
echo "<br>empSkill: ".$empSkill;
echo "<br>empSalary: ".$empSalary;
echo "<br>is country id: ".$iscountryID;
echo "<br>country name: ".$countryName;
echo "<br>country currency: ".$countryCurrency;
echo "<br>gold exchange rate: ".$Changerate;
echo "<br>job tax: ".$taxIncome;
echo "<br>server name: ".$serverName;
echo "<br>link to market: ".$linktopageJob;
echo "<br>market country ID: ".$marketCountryID;
echo "<br>market country Name: ".$countryName;
echo "<br>time Job: ".$timeJob;
echo "<br>day time Job: ".$dayTimeJob;
echo "<br>gold Job: ".$goldJob;
echo "<br>net gold job: ".$netgoldJob;
echo "<br>link to employer: ".$linktoEmployer;
echo "<br>currency Sell: ".$Currencysell;
echo "<br>**********************END POST**********************<br><br>";
echo_flush();
echo "check $serverName";
if ($stmt = $conn->prepare($sqlfind)) {
$stmt->bind_param('sississiiiiiiis', $iscountryID, $countryName, $countryCurrency, $empCompanyID, $empCompanyName, $empProduct, $empQuality, $empSkill, $empSalary, $Changerate, $goldJob, $taxIncome, $netgoldJob, $serverName, $dayTimeJob);
/* execute query */
$stmt->execute();
/* Store the result (to get properties) */
$stmt->store_result();
/* Get the number of rows */
$num_of_rows = $stmt->num_rows;
echo "<br>there are $num_of_rows results corresponding to requested search<br>";
if ($num_of_rows == 0) {
echo "THERE AREN'T RESULTS!";
// SQLI INSERT
$sqlJobIns = "INSERT INTO cJM".$serverName." (iscountryID, countryName, countryCurrency, empCompanyID, empCompanyName, empProduct, empQuality, empSkill, empSalary, goldExchange, salaryinGold, jobTax, netSalary, Server, link_to_page, link_to_employer, time_of_record, DateSave) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURDATE())"; // ADD NEW RECORDS
if($stmt = $conn->prepare($sqlJobIns)) {
$stmt->bind_param('ississiiiiiiiisss', $iscountryID, $countryName, $countryCurrency, $empCompanyID, $empCompanyName, $empProduct, $empQuality, $empSkill, $empSalary, $Changerate, $goldJob, $taxIncome, $netgoldJob, $serverName, $linktopageJob, $linktoEmployer, $dayTimeJob);
/* execute query */
$stmt->execute();
$stmt->store_result();
echo "<br>check $serverName";
//var_dump($stmt);
$num_of_rows_INS = $stmt->num_rows;
echo "<br>Check if data have been inserted: ";
if ($stmt) {
// RETURN AMOUNT OF DATA INSERT
printf("added %d\n records to the DB...<br>", $stmt->affected_rows);
} else {
echo "<br>there was a problem with data insert...<br>";
}}
} else echo ("<br>WE HAVE RESOULTS!");
/* Bind the result to variables */
$stmt->bind_result($idDB);
while ($stmt->fetch()) {
echo "<br>id from DATABASE is: ".$idDB;
};
/* free results */
$stmt->free_result();
/* close statement */
$stmt->close();
}};
感谢您的时间。 希望这段代码可以帮助其他人。
阿尔贝托
答案 0 :(得分:1)
如果产品,技能和薪水都与雇主提供的工作相关,那么您可以填充您的数组:(请注意它使用您的变量名称的简写)
在循环遍历json数组时使用它。
if (isset ($employers ['employer id']))
$employers ['employer id'] = array ( 'name' => 'employer name', 'jobs' => array ('skill' => 'the skill', 'quality' => 'the quality', 'product' => 'the product', 'salary' => 'the salary'));
else
$employers ['employer id']['jobs'][] = array('skill' => 'the skill', 'quality' => 'the quality', 'product' => 'the product', 'salary' => 'the salary'));
使用嵌套的foreach来取回数据。 像:
$allJobs = array ();
foreach ($employers as $employer) {
for ($employer ['jobs'] as $job) {
$allJobs[] = array(
'EmployerName' => $employer['name'],
'EmployerID' => $employer['id'],
'Product' => $job['product'],
'Quality' => $job['quality'],
'Skill' => $job['skill'],
'Salary' => $job['salary']);
}
}