I have a question about associative array I'm not able to solve by myself with my lack of programming knowledge.
I have a foreach loop from number "1" to "12". For each number I get a link that extract data from another server.
This data has the form of a json array, so I create single arrays for each key-value type I extract from json data (ex. employer name; employer id; employer salary; ...) with CURL.
Than I need to insert all this data into a database so I create one array with all this data and than I insert all this data into table. But... if I have to create the final single array from the first foreach I have no problems, but I'm not able to continue the already existing final array from the second foreach to the last... The reason I need to work like this it that may happens that data extracted from Json array contains the same ex. Employer twice, so I would like to create the final array without repetitions.
Here follows the code:
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))) {
than I've tried:
$allJobs[$key] = array(
'EmployerName' => $arrEmpName[$id],
'EmployerID' => $arrEmpID[$id],
'Product' => $arrProduct[$id],
'Quality' => $arrQuality[$id],
'Skill' => $arrSkill[$id],
'Salary' => $arrSalary[$id]);
and than
array_push($allJobs, ("'EmployerName' => ".$arrEmpName[$id]), ("'EmployerID' => ".$arrEmpID[$id]), "'Product' => ".$arrProduct[$id], "'Quality' => ".$arrQuality[$id], "'Skill' => ".$arrSkill[$id], "'Salary' => ".$arrSalary[$id]);
$f++;
}};
both didn't work... than the code ends like this:
// 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);
};
Thx for your time and help.
UPDATE
Here we go: this is data I obtain from Json file
{"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
}
This particular data is obtained from file corresponding to file Argentina, with work skill 8. You have to check all skill level to have full Job Market data from 1 to 12. Each page returns only 15 result, and it might happens that "company xx" appears also in page for Skill Work ex. "3" and "2".
What I would try to do is to extract data from all Work Skill page (from 1 to 12) and create a single Array without repetitions like:
[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'] => ...
UPDATE 2
but if I use only:
} 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++;
}};
I obtain this strange result...:
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
sorry but can't really understand why...
UPDATE 3
Here follows 2 examples of "allJobs array" I have in my examples: first one is obtained from SKILL PAGE 2 (so second value of loop), second from SKILL PAGE 3
this is allJobs (work skill 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 ) )
this is allJobs (work skill 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 ) )
as you can see,
Array ( [EmployerName] => Air Veneto Q1 -1 [EmployerID] => 3481 [Product] => Iron [Quality] => 2 [Skill] => 2 [Salary] => 12.01
recurs twice, and both entries are exactly the same.
UPDATE 4 (SOLUTION)
Well I was unable to solve my problem with associative array, so (this might be useful for someone else, I've made a check later in code, just before inserting data into database.
So when I find all the VARS already exists as values stored in DB, the code jumps to next part, avoiding adding again the same values.
Here is code:
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();
}};
Thanks for your time. Hope this code helps the others.
Alberto