1

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

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Your question is a bit confusing, but let's give it a try. Can you show the results for `print_r($allJobs);` and `print_r($result); `? And try to draw what it "should be" instead. – al'ein Sep 18 '15 at 23:53
  • oh yes thx. I'll update question – Alberto Giorgis Sep 18 '15 at 23:53
  • Can you use the employer id as the array key? Than when you assign a duplicate, it would just reassign the key. Use `array_vales` to get back the array but with numerical indexes –  Sep 19 '15 at 00:11
  • can't do this, because duplicates are for all the values I need to insert, not only for Company ID or Name. It's possible to have for ex. same name, same ID, same product and skill but different SALARY, because maybe there are two identical offers different only for SALARY offered to worker... – Alberto Giorgis Sep 19 '15 at 00:15
  • What relates to what? If you products, skills, and salaries all relate to employees, just have an array that keeps track of employees and adds products and skills to that employee. You may want to look into [normalizing your database](https://en.wikipedia.org/wiki/Database_normalization) –  Sep 19 '15 at 02:26
  • @Alberto actually, you can probably keep doing what you've been doing and then use [this answer](http://stackoverflow.com/a/946300) or potentially handle it with your sql query with something like [this answer](http://stackoverflow.com/a/2645436)(your where clause would check each column and you'd only need to use holdlock and rowlock) –  Sep 20 '15 at 02:59
  • ok I'll try and update on the solution I found – Alberto Giorgis Sep 21 '15 at 15:01
  • 2
    Please remove "Solved" from your title and question. You can post the answer on its own, and even accept it. That is the proper indication for others with a similar question. – Jongware Nov 09 '15 at 23:04

1 Answers1

1

If products, skills, and salaries are all related to jobs offered by employers you could populate your array something like this: (be aware it uses shorthand for your variable names)

Use this while looping through json array.

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'));

Use nested foreach to get data back out. Like:

$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']);
    } 
}
  • but wouldn't be easier to have foreach that create the "final array" with different name everytime (like $allJobs1,$allJobs2...) and than merge them together? – Alberto Giorgis Sep 19 '15 at 14:15
  • @alberto could you explain a little more what you mean by that? Why would you need more than 1 allJobs array? –  Sep 19 '15 at 19:35
  • No, really sorry for my terrible explanation. What i meant was: i need to obtain data to be inserted into db without repetitions. So i thought to use different arrays to store single data part (ex. Employer name). Than compose a single array checking what was already inside the "final array". – Alberto Giorgis Sep 19 '15 at 20:17
  • The problem was that i needed to repeat this operation 12 times, than have a big array, without rep and overwriting other loops data, and use it to get All the informations to be stored in database. It's little more claerer this way? – Alberto Giorgis Sep 19 '15 at 20:19
  • Like this: 1) 1st loop -> all data in sigle arrays -> all these arrays merged in one without rep checking in_array presence of same data (salary, product, skill lvl...); than 2) restart with skill lvl 2... than 3... up to 12; than merge everything in one last big array and loop it to insert data into db – Alberto Giorgis Sep 19 '15 at 20:26
  • @Alberto I think I understand better now. But still not fully; what fields are duplicating? Is the example JSON array you posted from a single file? It looks like it has different skill levels despite being from a file where the skill level is specified? –  Sep 19 '15 at 22:05
  • that's an example got from one Json interrogation. The Json data I receive, return the first 15 result of the server, so if it doesn't find 15 results for (ex.) skill level 10, it will returns the all results for lvl 10 (ex. 8 jobs offers), than the results from skill lvl 9...8...7 and go on, up to 15 results, so it may happens that I have Company FORD for work skill lvl 10 both in data array returned from json interrogation for page "skill level 11, if there aren't 15 result from that paga, and skill level 10...". So there would be a complete duplication of data in same cases... – Alberto Giorgis Sep 19 '15 at 23:57
  • @Alberto it seems like the .json file shouldn't give you skill levels that you don't ask for (since the skill level appears to be in the filename). –  Sep 20 '15 at 00:13
  • I know @Terminus that's the big deal :D But the API I work with can't be modified by me... Thx a lot for your time! – Alberto Giorgis Sep 20 '15 at 00:14
  • SOLVED using another way. I'll post it in principal question. – Alberto Giorgis Sep 21 '15 at 20:54
  • @Alberto feel free to post it as an answer to this question and accept it if it answers the question! :) –  Sep 21 '15 at 21:14