json to array to mysql not cycling through data - php

hi guys i need to get this json data into my db so i have allready collected the details of another structure from the same site using a script similar to the one below but that was only 8 items long, this behaves as if it worked but nothing happens to the table in question heres the json:
[{"country":"Sri Lanka","countryCode":"LK"},{"country":"Syria","countryCode":"SY"},{"country":"Kenya","countryCode":"KE"},{"country":"Cambodia","countryCode":"KH"},{"country":"Mongolia","countryCode":"MN"},{"country":"Senegal","countryCode":"SN"},{"country":"Jordan","countryCode":"JO"},{"country":"Sweden","countryCode":"SE"},{"country":"United Kingdom","countryCode":"GB"},{"country":"Austria","countryCode":"AT"},{"country":"Israel","countryCode":"IL"},{"country":"Paraguay","countryCode":"PY"},{"country":"Jamaica","countryCode":"JM"},{"country":"Mexico","countryCode":"MX"},{"country":"Chile","countryCode":"CL"},{"country":"Canada","countryCode":"CA"},{"country":"Bulgaria","countryCode":"BG"},{"country":"Monaco","countryCode":"MC"},{"country":"Puerto Rico","countryCode":"PR"},{"country":"Lebanon","countryCode":"LB"},{"country":"Ecuador","countryCode":"EC"},{"country":"Palau","countryCode":"PW"},{"country":"Guam","countryCode":"GU"},{"country":"Macedonia","countryCode":"MK"},{"country":"Reunion","countryCode":"RE"},{"country":"Botswana","countryCode":"BW"},{"country":"Libya","countryCode":"LY"},{"country":"Togo","countryCode":"TG"},{"country":"Thailand","countryCode":"TH"},{"country":"Malaysia","countryCode":"MY"},{"country":"Spain","countryCode":"ES"},{"country":"Denmark","countryCode":"DK"},{"country":"Dominican Republic","countryCode":"DO"},{"country":"Belize","countryCode":"BZ"},{"country":"Nicaragua","countryCode":"NI"},{"country":"Venezuela","countryCode":"VE"},{"country":"Latvia","countryCode":"LV"},{"country":"Azerbaijan","countryCode":"AZ"},{"country":"Moldova","countryCode":"MD"},{"country":"United Arab Emirates","countryCode":"AE"},{"country":"Malta","countryCode":"MT"},{"country":"Swaziland","countryCode":"SZ"},{"country":"Yemen","countryCode":"YE"},{"country":"El Salvador","countryCode":"SV"},{"country":"Malawi","countryCode":"MW"},{"country":"French Guiana","countryCode":"GF"},{"country":"Bangladesh","countryCode":"BD"},{"country":"Liechtenstein","countryCode":"LI"},{"country":"San Marino","countryCode":"SM"},{"country":"Belarus","countryCode":"BY"},{"country":"Mozambique","countryCode":"MZ"},{"country":"Iran","countryCode":"IR"},{"country":"Pakistan","countryCode":"PK"},{"country":"Russia","countryCode":"RU"},{"country":"Mauritius","countryCode":"MU"},{"country":"Taiwan","countryCode":"TW"},{"country":"Portugal","countryCode":"PT"},{"country":"Nigeria","countryCode":"NG"},{"country":"Greece","countryCode":"GR"},{"country":"Switzerland","countryCode":"CH"},{"country":"Kuwait","countryCode":"KW"},{"country":"Luxembourg","countryCode":"LU"},{"country":"Czech Republic","countryCode":"CZ"},{"country":"Croatia","countryCode":"HR"},{"country":"Argentina","countryCode":"AR"},{"country":"Egypt","countryCode":"EG"},{"country":"Costa Rica","countryCode":"CR"},{"country":"United States","countryCode":"US"},{"country":"Lithuania","countryCode":"LT"},{"country":"Kazakhstan","countryCode":"KZ"},{"country":"Serbia","countryCode":"RS"},{"country":"Estonia","countryCode":"EE"},{"country":"Singapore","countryCode":"SG"},{"country":"Micronesia","countryCode":"FM"},{"country":"Albania","countryCode":"AL"},{"country":"Mali","countryCode":"ML"},{"country":"Uganda","countryCode":"UG"},{"country":"South Africa","countryCode":"ZA"},{"country":"Myanmar","countryCode":"MM"},{"country":"Australia","countryCode":"AU"},{"country":"Fiji","countryCode":"FJ"},{"country":"Italy","countryCode":"IT"},{"country":"Andorra","countryCode":"AD"},{"country":"Norway","countryCode":"NO"},{"country":"Brazil","countryCode":"BR"},{"country":"Uruguay","countryCode":"UY"},{"country":"Guatemala","countryCode":"GT"},{"country":"French Polynesia","countryCode":"PF"},{"country":"Seychelles","countryCode":"SC"},{"country":"Barbados","countryCode":"BB"},{"country":"Montenegro","countryCode":"ME"},{"country":"Laos","countryCode":"LA"},{"country":"Cook Islands","countryCode":"CK"},{"country":"Cayman Islands","countryCode":"KY"},{"country":"Gabon","countryCode":"GA"},{"country":"Saudi Arabia","countryCode":"SA"},{"country":"Vietnam","countryCode":"VN"},{"country":"Philippines","countryCode":"PH"},{"country":"South Korea","countryCode":"KR"},{"country":"Japan","countryCode":"JP"},{"country":"Vanuatu","countryCode":"VU"},{"country":"New Zealand","countryCode":"NZ"},{"country":"Tunisia","countryCode":"TN"},{"country":"Ireland","countryCode":"IE"},{"country":"Slovenia","countryCode":"SI"},{"country":"Suriname","countryCode":"SR"},{"country":"Finland","countryCode":"FI"},{"country":"Aruba","countryCode":"AW"},{"country":"Nepal","countryCode":"NP"},{"country":"Faroe Islands","countryCode":"FO"},{"country":"Sudan","countryCode":"SD"},{"country":"Indonesia","countryCode":"ID"},{"country":"China","countryCode":"CN"},{"country":"Maldives","countryCode":"MV"},{"country":"Uzbekistan","countryCode":"UZ"},{"country":"Hong Kong","countryCode":"HK"},{"country":"Algeria","countryCode":"DZ"},{"country":"Germany","countryCode":"DE"},{"country":"Oman","countryCode":"OM"},{"country":"France","countryCode":"FR"},{"country":"Slovakia","countryCode":"SK"},{"country":"Poland","countryCode":"PL"},{"country":"Bahamas","countryCode":"BS"},{"country":"Guadeloupe","countryCode":"GP"},{"country":"Colombia","countryCode":"CO"},{"country":"Bolivia","countryCode":"BO"},{"country":"Ukraine","countryCode":"UA"},{"country":"Netherlands Antilles","countryCode":"AN"},{"country":"Tonga","countryCode":"TO"},{"country":"Georgia","countryCode":"GE"},{"country":"Ethiopia","countryCode":"ET"},{"country":"Bermuda","countryCode":"BM"},{"country":"Cyprus","countryCode":"CY"},{"country":"Gambia","countryCode":"GM"},{"country":"Morocco","countryCode":"MA"},{"country":"Hungary","countryCode":"HU"},{"country":"Honduras","countryCode":"HN"},{"country":"Peru","countryCode":"PE"},{"country":"Romania","countryCode":"RO"},{"country":"Armenia","countryCode":"AM"},{"country":"New Caledonia","countryCode":"NC"},{"country":"Zambia","countryCode":"ZM"},{"country":"India","countryCode":"IN"},{"country":"Tanzania","countryCode":"TZ"},{"country":"Netherlands","countryCode":"NL"},{"country":"Belgium","countryCode":"BE"},{"country":"Turkey","countryCode":"TR"},{"country":"Namibia","countryCode":"NA"},{"country":"Grenada","countryCode":"GD"},{"country":"Qatar","countryCode":"QA"},{"country":"Iceland","countryCode":"IS"},{"country":"Bahrain","countryCode":"BH"},{"country":"Zimbabwe","countryCode":"ZW"},{"country":"Gibraltar","countryCode":"GI"},{"country":"Cameroon","countryCode":"CM"}]
then this is what the array looks like:
array(155)
{ [0]=> array(2) { ["country"]=> string(9) "Sri Lanka" ["countryCode"]=> string(2) "LK" }
[1]=> array(2) { ["country"]=> string(5) "Syria" ["countryCode"]=> string(2) "SY" }
[2]=> array(2) { ["country"]=> string(5) "Kenya" ["countryCode"]=> string(2) "KE" }
[3]=> array(2) { ["country"]=> string(8) "Cambodia" ["countryCode"]=> string(2) "KH" }
[4]=> array(2) { ["country"]=> string(8) "Mongolia" ["countryCode"]=> string(2) "MN" }
[5]=> array(2) { ["country"]=> string(7) "Senegal" ["countryCode"]=> string(2) "SN" }
[6]=> array(2) { ["country"]=> string(6) "Jordan" ["countryCode"]=> string(2) "JO" }
[7]=> array(2) { ["country"]=> string(6) "Sweden" ["countryCode"]=> string(2) "SE" }
[8]=> array(2) { ["country"]=> string(14) "United Kingdom" ["countryCode"]=> string(2) "GB" }
[9]=> array(2) { ["country"]=> string(7) "Austria" ["countryCode"]=> string(2) "AT" } [10]=> array(2) { ["country"]=> string(6) "Israel" ["countryCode"]=> string(2) "IL" } [11]=> array(2) { ["country"]=> string(8) "Paraguay" ["countryCode"]=> string(2) "PY" } [12]=> array(2) { ["country"]=> string(7) "Jamaica" ["countryCode"]=> string(2) "JM" } [13]=> array(2) { ["country"]=> string(6) "Mexico" ["countryCode"]=> string(2) "MX" } [14]=> array(2) { ["country"]=> string(5) "Chile" ["countryCode"]=> string(2) "CL" } [15]=> array(2) { ["country"]=> string(6) "Canada" ["countryCode"]=> string(2) "CA" } [16]=> array(2) { ["country"]=> string(8) "Bulgaria" ["countryCode"]=> string(2) "BG" } [17]=> array(2) { ["country"]=> string(6) "Monaco" ["countryCode"]=> string(2) "MC" } [18]=> array(2) { ["country"]=> string(11) "Puerto Rico" ["countryCode"]=> string(2) "PR" } [19]=> array(2) { ["country"]=> string(7) "Lebanon" ["countryCode"]=> string(2) "LB" } [20]=> array(2) { ["country"]=> string(7) "Ecuador" ["countryCode"]=> string(2) "EC" } [21]=> array(2) { ["country"]=> string(5) "Palau" ["countryCode"]=> string(2) "PW" } [22]=> array(2) { ["country"]=> string(4) "Guam" ["countryCode"]=> string(2) "GU" } [23]=> array(2) { ["country"]=> string(9) "Macedonia" ["countryCode"]=> string(2) "MK" } [24]=> array(2) { ["country"]=> string(7) "Reunion" ["countryCode"]=> string(2) "RE" } [25]=> array(2) { ["country"]=> string(8) "Botswana" ["countryCode"]=> string(2) "BW" } [26]=> array(2) { ["country"]=> string(5) "Libya" ["countryCode"]=> string(2) "LY" } [27]=> array(2) { ["country"]=> string(4) "Togo" ["countryCode"]=> string(2) "TG" } [28]=> array(2) { ["country"]=> string(8) "Thailand" ["countryCode"]=> string(2) "TH" } [29]=> array(2) { ["country"]=> string(8) "Malaysia" ["countryCode"]=> string(2) "MY" } [30]=> array(2) { ["country"]=> string(5) "Spain" ["countryCode"]=> string(2) "ES" } [31]=> array(2) { ["country"]=> string(7) "Denmark" ["countryCode"]=> string(2) "DK" } [32]=> array(2) { ["country"]=> string(18) "Dominican Republic" ["countryCode"]=> string(2) "DO" } [33]=> array(2) { ["country"]=> string(6) "Belize" ["countryCode"]=> string(2) "BZ" } [34]=> array(2) { ["country"]=> string(9) "Nicaragua" ["countryCode"]=> string(2) "NI" } [35]=> array(2) { ["country"]=> string(9) "Venezuela" ["countryCode"]=> string(2) "VE" } [36]=> array(2) { ["country"]=> string(6) "Latvia" ["countryCode"]=> string(2) "LV" } [37]=> array(2) { ["country"]=> string(10) "Azerbaijan" ["countryCode"]=> string(2) "AZ" } [38]=> array(2) { ["country"]=> string(7) "Moldova" ["countryCode"]=> string(2) "MD" } [39]=> array(2) { ["country"]=> string(20) "United Arab Emirates" ["countryCode"]=> string(2) "AE" } [40]=> array(2) { ["country"]=> string(5) "Malta" ["countryCode"]=> string(2) "MT" } [41]=> array(2) { ["country"]=> string(9) "Swaziland" ["countryCode"]=> string(2) "SZ" } [42]=> array(2) { ["country"]=> string(5) "Yemen" ["countryCode"]=> string(2) "YE" } [43]=> array(2) { ["country"]=> string(11) "El Salvador" ["countryCode"]=> string(2) "SV" } [44]=> array(2) { ["country"]=> string(6) "Malawi" ["countryCode"]=> string(2) "MW" } [45]=> array(2) { ["country"]=> string(13) "French Guiana" ["countryCode"]=> string(2) "GF" } [46]=> array(2) { ["country"]=> string(10) "Bangladesh" ["countryCode"]=> string(2) "BD" } [47]=> array(2) { ["country"]=> string(13) "Liechtenstein" ["countryCode"]=> string(2) "LI" } [48]=> array(2) { ["country"]=> string(10) "San Marino" ["countryCode"]=> string(2) "SM" } [49]=> array(2) { ["country"]=> string(7) "Belarus" ["countryCode"]=> string(2) "BY" } [50]=> array(2) { ["country"]=> string(10) "Mozambique" ["countryCode"]=> string(2) "MZ" } [51]=> array(2) { ["country"]=> string(4) "Iran" ["countryCode"]=> string(2) "IR" } [52]=> array(2) { ["country"]=> string(8) "Pakistan" ["countryCode"]=> string(2) "PK" } [53]=> array(2) { ["country"]=> string(6) "Russia" ["countryCode"]=> string(2) "RU" } [54]=> array(2) { ["country"]=> string(9) "Mauritius" ["countryCode"]=> string(2) "MU" } [55]=> array(2) { ["country"]=> string(6) "Taiwan" ["countryCode"]=> string(2) "TW" } [56]=> array(2) { ["country"]=> string(8) "Portugal" ["countryCode"]=> string(2) "PT" } [57]=> array(2) { ["country"]=> string(7) "Nigeria" ["countryCode"]=> string(2) "NG" } [58]=> array(2) { ["country"]=> string(6) "Greece" ["countryCode"]=> string(2) "GR" } [59]=> array(2) { ["country"]=> string(11) "Switzerland" ["countryCode"]=> string(2) "CH" } [60]=> array(2) { ["country"]=> string(6) "Kuwait" ["countryCode"]=> string(2) "KW" } [61]=> array(2) { ["country"]=> string(10) "Luxembourg" ["countryCode"]=> string(2) "LU" } [62]=> array(2) { ["country"]=> string(14) "Czech Republic" ["countryCode"]=> string(2) "CZ" } [63]=> array(2) { ["country"]=> string(7) "Croatia" ["countryCode"]=> string(2) "HR" } [64]=> array(2) { ["country"]=> string(9) "Argentina" ["countryCode"]=> string(2) "AR" } [65]=> array(2) { ["country"]=> string(5) "Egypt" ["countryCode"]=> string(2) "EG" } [66]=> array(2) { ["country"]=> string(10) "Costa Rica" ["countryCode"]=> string(2) "CR" } [67]=> array(2) { ["country"]=> string(13) "United States" ["countryCode"]=> string(2) "US" } [68]=> array(2) { ["country"]=> string(9) "Lithuania" ["countryCode"]=> string(2) "LT" } [69]=> array(2) { ["country"]=> string(10) "Kazakhstan" ["countryCode"]=> string(2) "KZ" } [70]=> array(2) { ["country"]=> string(6) "Serbia" ["countryCode"]=> string(2) "RS" } [71]=> array(2) { ["country"]=> string(7) "Estonia" ["countryCode"]=> string(2) "EE" } [72]=> array(2) { ["country"]=> string(9) "Singapore" ["countryCode"]=> string(2) "SG" } [73]=> array(2) { ["country"]=> string(10) "Micronesia" ["countryCode"]=> string(2) "FM" } [74]=> array(2) { ["country"]=> string(7) "Albania" ["countryCode"]=> string(2) "AL" } [75]=> array(2) { ["country"]=> string(4) "Mali" ["countryCode"]=> string(2) "ML" } [76]=> array(2) { ["country"]=> string(6) "Uganda" ["countryCode"]=> string(2) "UG" } [77]=> array(2) { ["country"]=> string(12) "South Africa" ["countryCode"]=> string(2) "ZA" } [78]=> array(2) { ["country"]=> string(7) "Myanmar" ["countryCode"]=> string(2) "MM" } [79]=> array(2) { ["country"]=> string(9) "Australia" ["countryCode"]=> string(2) "AU" } [80]=> array(2) { ["country"]=> string(4) "Fiji" ["countryCode"]=> string(2) "FJ" } [81]=> array(2) { ["country"]=> string(5) "Italy" ["countryCode"]=> string(2) "IT" } [82]=> array(2) { ["country"]=> string(7) "Andorra" ["countryCode"]=> string(2) "AD" } [83]=> array(2) { ["country"]=> string(6) "Norway" ["countryCode"]=> string(2) "NO" } [84]=> array(2) { ["country"]=> string(6) "Brazil" ["countryCode"]=> string(2) "BR" } [85]=> array(2) { ["country"]=> string(7) "Uruguay" ["countryCode"]=> string(2) "UY" } [86]=> array(2) { ["country"]=> string(9) "Guatemala" ["countryCode"]=> string(2) "GT" } [87]=> array(2) { ["country"]=> string(16) "French Polynesia" ["countryCode"]=> string(2) "PF" } [88]=> array(2) { ["country"]=> string(10) "Seychelles" ["countryCode"]=> string(2) "SC" } [89]=> array(2) { ["country"]=> string(8) "Barbados" ["countryCode"]=> string(2) "BB" } [90]=> array(2) { ["country"]=> string(10) "Montenegro" ["countryCode"]=> string(2) "ME" } [91]=> array(2) { ["country"]=> string(4) "Laos" ["countryCode"]=> string(2) "LA" } [92]=> array(2) { ["country"]=> string(12) "Cook Islands" ["countryCode"]=> string(2) "CK" } [93]=> array(2) { ["country"]=> string(14) "Cayman Islands" ["countryCode"]=> string(2) "KY" } [94]=> array(2) { ["country"]=> string(5) "Gabon" ["countryCode"]=> string(2) "GA" } [95]=> array(2) { ["country"]=> string(12) "Saudi Arabia" ["countryCode"]=> string(2) "SA" } [96]=> array(2) { ["country"]=> string(7) "Vietnam" ["countryCode"]=> string(2) "VN" } [97]=> array(2) { ["country"]=> string(11) "Philippines" ["countryCode"]=> string(2) "PH" } [98]=> array(2) { ["country"]=> string(11) "South Korea" ["countryCode"]=> string(2) "KR" } [99]=> array(2) { ["country"]=> string(5) "Japan" ["countryCode"]=> string(2) "JP" } [100]=> array(2) { ["country"]=> string(7) "Vanuatu" ["countryCode"]=> string(2) "VU" } [101]=> array(2) { ["country"]=> string(11) "New Zealand" ["countryCode"]=> string(2) "NZ" } [102]=> array(2) { ["country"]=> string(7) "Tunisia" ["countryCode"]=> string(2) "TN" } [103]=> array(2) { ["country"]=> string(7) "Ireland" ["countryCode"]=> string(2) "IE" } [104]=> array(2) { ["country"]=> string(8) "Slovenia" ["countryCode"]=> string(2) "SI" } [105]=> array(2) { ["country"]=> string(8) "Suriname" ["countryCode"]=> string(2) "SR" } [106]=> array(2) { ["country"]=> string(7) "Finland" ["countryCode"]=> string(2) "FI" } [107]=> array(2) { ["country"]=> string(5) "Aruba" ["countryCode"]=> string(2) "AW" } [108]=> array(2) { ["country"]=> string(5) "Nepal" ["countryCode"]=> string(2) "NP" } [109]=> array(2) { ["country"]=> string(13) "Faroe Islands" ["countryCode"]=> string(2) "FO" } [110]=> array(2) { ["country"]=> string(5) "Sudan" ["countryCode"]=> string(2) "SD" } [111]=> array(2) { ["country"]=> string(9) "Indonesia" ["countryCode"]=> string(2) "ID" } [112]=> array(2) { ["country"]=> string(5) "China" ["countryCode"]=> string(2) "CN" } [113]=> array(2) { ["country"]=> string(8) "Maldives" ["countryCode"]=> string(2) "MV" } [114]=> array(2) { ["country"]=> string(10) "Uzbekistan" ["countryCode"]=> string(2) "UZ" } [115]=> array(2) { ["country"]=> string(9) "Hong Kong" ["countryCode"]=> string(2) "HK" } [116]=> array(2) { ["country"]=> string(7) "Algeria" ["countryCode"]=> string(2) "DZ" } [117]=> array(2) { ["country"]=> string(7) "Germany" ["countryCode"]=> string(2) "DE" } [118]=> array(2) { ["country"]=> string(4) "Oman" ["countryCode"]=> string(2) "OM" } [119]=> array(2) { ["country"]=> string(6) "France" ["countryCode"]=> string(2) "FR" } [120]=> array(2) { ["country"]=> string(8) "Slovakia" ["countryCode"]=> string(2) "SK" } [121]=> array(2) { ["country"]=> string(6) "Poland" ["countryCode"]=> string(2) "PL" } [122]=> array(2) { ["country"]=> string(7) "Bahamas" ["countryCode"]=> string(2) "BS" } [123]=> array(2) { ["country"]=> string(10) "Guadeloupe" ["countryCode"]=> string(2) "GP" } [124]=> array(2) { ["country"]=> string(8) "Colombia" ["countryCode"]=> string(2) "CO" } [125]=> array(2) { ["country"]=> string(7) "Bolivia" ["countryCode"]=> string(2) "BO" } [126]=> array(2) { ["country"]=> string(7) "Ukraine" ["countryCode"]=> string(2) "UA" } [127]=> array(2) { ["country"]=> string(20) "Netherlands Antilles" ["countryCode"]=> string(2) "AN" } [128]=> array(2) { ["country"]=> string(5) "Tonga" ["countryCode"]=> string(2) "TO" } [129]=> array(2) { ["country"]=> string(7) "Georgia" ["countryCode"]=> string(2) "GE" } [130]=> array(2) { ["country"]=> string(8) "Ethiopia" ["countryCode"]=> string(2) "ET" } [131]=> array(2) { ["country"]=> string(7) "Bermuda" ["countryCode"]=> string(2) "BM" } [132]=> array(2) { ["country"]=> string(6) "Cyprus" ["countryCode"]=> string(2) "CY" } [133]=> array(2) { ["country"]=> string(6) "Gambia" ["countryCode"]=> string(2) "GM" } [134]=> array(2) { ["country"]=> string(7) "Morocco" ["countryCode"]=> string(2) "MA" } [135]=> array(2) { ["country"]=> string(7) "Hungary" ["countryCode"]=> string(2) "HU" } [136]=> array(2) { ["country"]=> string(8) "Honduras" ["countryCode"]=> string(2) "HN" } [137]=> array(2) { ["country"]=> string(4) "Peru" ["countryCode"]=> string(2) "PE" } [138]=> array(2) { ["country"]=> string(7) "Romania" ["countryCode"]=> string(2) "RO" } [139]=> array(2) { ["country"]=> string(7) "Armenia" ["countryCode"]=> string(2) "AM" } [140]=> array(2) { ["country"]=> string(13) "New Caledonia" ["countryCode"]=> string(2) "NC" } [141]=> array(2) { ["country"]=> string(6) "Zambia" ["countryCode"]=> string(2) "ZM" } [142]=> array(2) { ["country"]=> string(5) "India" ["countryCode"]=> string(2) "IN" } [143]=> array(2) { ["country"]=> string(8) "Tanzania" ["countryCode"]=> string(2) "TZ" } [144]=> array(2) { ["country"]=> string(11) "Netherlands" ["countryCode"]=> string(2) "NL" } [145]=> array(2) { ["country"]=> string(7) "Belgium" ["countryCode"]=> string(2) "BE" } [146]=> array(2) { ["country"]=> string(6) "Turkey" ["countryCode"]=> string(2) "TR" } [147]=> array(2) { ["country"]=> string(7) "Namibia" ["countryCode"]=> string(2) "NA" } [148]=> array(2) { ["country"]=> string(7) "Grenada" ["countryCode"]=> string(2) "GD" } [149]=> array(2) { ["country"]=> string(5) "Qatar" ["countryCode"]=> string(2) "QA" } [150]=> array(2) { ["country"]=> string(7) "Iceland" ["countryCode"]=> string(2) "IS" } [151]=> array(2) { ["country"]=> string(7) "Bahrain" ["countryCode"]=> string(2) "BH" } [152]=> array(2) { ["country"]=> string(8) "Zimbabwe" ["countryCode"]=> string(2) "ZW" } [153]=> array(2) { ["country"]=> string(9) "Gibraltar" ["countryCode"]=> string(2) "GI" } [154]=> array(2) { ["country"]=> string(8) "Cameroon" ["countryCode"]=> string(2) "CM" } }
then this is the code used to convert to an array and insert into mysql but it doesn't get the data into mysql:
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdatar = curl_exec($ch);
curl_close($ch);
$object_array = json_decode($rawdatar,true);
foreach($object_array as $obj) {
$country = $obj['country'];
$countryCode = $obj['countryCode'];
if($countryCode){
$sql = "INSERT INTO `pt_country` (country,countrycode) VALUES ('$country','$countryCode')";
database_queryModify($sql,$result);
}
else{
echo "dint work";
}
}
it behaves as if it should be doing somthing but when i check the sql table theres nothing what am i doing wrong with this? i know everything works upto the foreach and im only running this script once to get the data its not a regular request that someone could exploit i will be monitoring the data input

Did you check if the curl call works? No.
$rawdata = curl_exec($ch) or die(curl_error($ch));
^^^^^^^^^^^^^^^^^^^^^^^^
Did you check if you actually get valid JSON data? after CURL completes? No
var_dump($rawdata);
Did you check if the JSON decode succeeds? No
var_dump($object_array);
Did you check if the INSERT query succeeds? No.
if (!database_queryModify($sql,$result)) { ... whatever your DB library's error message fetcher is };
As it stands now, your query is vulnerable to SQL injection, since you're inserting raw text provided by a remote site into your query strings. Nothing says the remote site can't be compromised and make $obj['country'] be little Bobby Tables

The length of the items should not be a problem for the database.
I think there is an error but you can't see it.
Try to put error_reporting on and rerun the script. Add this code at the beginning of your script.
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
http://www.php.net/manual/en/function.error-reporting.php

Related

problem with explode, arrays and for each

I am having a few problems with the following code; I have a select with the option to select multiple options which then get inputted into the DB in a list format, I then convert these into arrays when I need to cross reference if they are selected for help I have included the var_dump for both $countryList and $DBcountries the area I am having issues with is
foreach ($DBcountries as $country) this foreach statement appears to be not working correctly.
Check below for both $countryList and $DBcountries ; Thanks.
The problem I have is that at the moment it doesn't select all the selected options that is stored in the DB.
<?php
$countryList = file('/var/www/vhosts/example.co.uk/httpdocs/user/tools/countries.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$options = '';
$DBcountries = $fetch['locations'];
$DBcountries = explode(', ', $DBcountries);
foreach ($countryList as $country) {
foreach ($DBcountries as $cross){
if($cross == $country){
$selected = 'selected';
}else{
$selected = '';
}
}
if($DBcountries == 'All'){
$all = 'selected';
}
$options .= '<option value="'.$country.'" ' . $selected . '>'.$country.'</option>';
}
$select = '<select class="form-select" oninput="locationOutputUpdate(value)" id="country" name="location" multiple aria-label="multiple select example"><option ' . $all . ' >All</option>'.$options.'</select>';
echo $select;
?>
vardump of $DBcountries ;
array(2) {
[0]=>
string(11) "Afghanistan"
[1]=>
string(7) "Albania"
}
vardump of $countryList ;
array(196) {
[0]=>
string(11) "Afghanistan"
[1]=>
string(7) "Albania"
[2]=>
string(7) "Algeria"
[3]=>
string(7) "Andorra"
[4]=>
string(6) "Angola"
[5]=>
string(14) "Antigua & Deps"
[6]=>
string(9) "Argentina"
[7]=>
string(7) "Armenia"
[8]=>
string(9) "Australia"
[9]=>
string(7) "Austria"
[10]=>
string(10) "Azerbaijan"
[11]=>
string(7) "Bahamas"
[12]=>
string(7) "Bahrain"
[13]=>
string(10) "Bangladesh"
[14]=>
string(8) "Barbados"
[15]=>
string(7) "Belarus"
[16]=>
string(7) "Belgium"
[17]=>
string(6) "Belize"
[18]=>
string(5) "Benin"
[19]=>
string(6) "Bhutan"
[20]=>
string(7) "Bolivia"
[21]=>
string(18) "Bosnia Herzegovina"
[22]=>
string(8) "Botswana"
[23]=>
string(6) "Brazil"
[24]=>
string(6) "Brunei"
[25]=>
string(8) "Bulgaria"
[26]=>
string(7) "Burkina"
[27]=>
string(7) "Burundi"
[28]=>
string(8) "Cambodia"
[29]=>
string(8) "Cameroon"
[30]=>
string(6) "Canada"
[31]=>
string(10) "Cape Verde"
[32]=>
string(19) "Central African Rep"
[33]=>
string(4) "Chad"
[34]=>
string(5) "Chile"
[35]=>
string(5) "China"
[36]=>
string(8) "Colombia"
[37]=>
string(7) "Comoros"
[38]=>
string(5) "Congo"
[39]=>
string(22) "Congo {Democratic Rep}"
[40]=>
string(10) "Costa Rica"
[41]=>
string(7) "Croatia"
[42]=>
string(4) "Cuba"
[43]=>
string(6) "Cyprus"
[44]=>
string(14) "Czech Republic"
[45]=>
string(7) "Denmark"
[46]=>
string(8) "Djibouti"
[47]=>
string(8) "Dominica"
[48]=>
string(18) "Dominican Republic"
[49]=>
string(10) "East Timor"
[50]=>
string(7) "Ecuador"
[51]=>
string(5) "Egypt"
[52]=>
string(11) "El Salvador"
[53]=>
string(17) "Equatorial Guinea"
[54]=>
string(7) "Eritrea"
[55]=>
string(7) "Estonia"
[56]=>
string(8) "Ethiopia"
[57]=>
string(4) "Fiji"
[58]=>
string(7) "Finland"
[59]=>
string(6) "France"
[60]=>
string(5) "Gabon"
[61]=>
string(6) "Gambia"
[62]=>
string(7) "Georgia"
[63]=>
string(7) "Germany"
[64]=>
string(5) "Ghana"
[65]=>
string(6) "Greece"
[66]=>
string(7) "Grenada"
[67]=>
string(9) "Guatemala"
[68]=>
string(6) "Guinea"
[69]=>
string(13) "Guinea-Bissau"
[70]=>
string(6) "Guyana"
[71]=>
string(5) "Haiti"
[72]=>
string(8) "Honduras"
[73]=>
string(7) "Hungary"
[74]=>
string(7) "Iceland"
[75]=>
string(5) "India"
[76]=>
string(9) "Indonesia"
[77]=>
string(4) "Iran"
[78]=>
string(4) "Iraq"
[79]=>
string(18) "Ireland {Republic}"
[80]=>
string(6) "Israel"
[81]=>
string(5) "Italy"
[82]=>
string(11) "Ivory Coast"
[83]=>
string(7) "Jamaica"
[84]=>
string(5) "Japan"
[85]=>
string(6) "Jordan"
[86]=>
string(10) "Kazakhstan"
[87]=>
string(5) "Kenya"
[88]=>
string(8) "Kiribati"
[89]=>
string(11) "Korea North"
[90]=>
string(11) "Korea South"
[91]=>
string(6) "Kosovo"
[92]=>
string(6) "Kuwait"
[93]=>
string(10) "Kyrgyzstan"
[94]=>
string(4) "Laos"
[95]=>
string(6) "Latvia"
[96]=>
string(7) "Lebanon"
[97]=>
string(7) "Lesotho"
[98]=>
string(7) "Liberia"
[99]=>
string(5) "Libya"
[100]=>
string(13) "Liechtenstein"
[101]=>
string(9) "Lithuania"
[102]=>
string(10) "Luxembourg"
[103]=>
string(9) "Macedonia"
[104]=>
string(10) "Madagascar"
[105]=>
string(6) "Malawi"
[106]=>
string(8) "Malaysia"
[107]=>
string(8) "Maldives"
[108]=>
string(4) "Mali"
[109]=>
string(5) "Malta"
[110]=>
string(16) "Marshall Islands"
[111]=>
string(10) "Mauritania"
[112]=>
string(9) "Mauritius"
[113]=>
string(6) "Mexico"
[114]=>
string(10) "Micronesia"
[115]=>
string(7) "Moldova"
[116]=>
string(6) "Monaco"
[117]=>
string(8) "Mongolia"
[118]=>
string(10) "Montenegro"
[119]=>
string(7) "Morocco"
[120]=>
string(10) "Mozambique"
[121]=>
string(16) "Myanmar, {Burma}"
[122]=>
string(7) "Namibia"
[123]=>
string(5) "Nauru"
[124]=>
string(5) "Nepal"
[125]=>
string(11) "Netherlands"
[126]=>
string(11) "New Zealand"
[127]=>
string(9) "Nicaragua"
[128]=>
string(5) "Niger"
[129]=>
string(7) "Nigeria"
[130]=>
string(6) "Norway"
[131]=>
string(4) "Oman"
[132]=>
string(8) "Pakistan"
[133]=>
string(5) "Palau"
[134]=>
string(6) "Panama"
[135]=>
string(16) "Papua New Guinea"
[136]=>
string(8) "Paraguay"
[137]=>
string(4) "Peru"
[138]=>
string(11) "Philippines"
[139]=>
string(6) "Poland"
[140]=>
string(8) "Portugal"
[141]=>
string(5) "Qatar"
[142]=>
string(7) "Romania"
[143]=>
string(18) "Russian Federation"
[144]=>
string(6) "Rwanda"
[145]=>
string(16) "St Kitts & Nevis"
[146]=>
string(8) "St Lucia"
[147]=>
string(30) "Saint Vincent & the Grenadines"
[148]=>
string(5) "Samoa"
[149]=>
string(10) "San Marino"
[150]=>
string(19) "Sao Tome & Principe"
[151]=>
string(12) "Saudi Arabia"
[152]=>
string(7) "Senegal"
[153]=>
string(6) "Serbia"
[154]=>
string(10) "Seychelles"
[155]=>
string(12) "Sierra Leone"
[156]=>
string(9) "Singapore"
[157]=>
string(8) "Slovakia"
[158]=>
string(8) "Slovenia"
[159]=>
string(15) "Solomon Islands"
[160]=>
string(7) "Somalia"
[161]=>
string(12) "South Africa"
[162]=>
string(11) "South Sudan"
[163]=>
string(5) "Spain"
[164]=>
string(9) "Sri Lanka"
[165]=>
string(5) "Sudan"
[166]=>
string(8) "Suriname"
[167]=>
string(9) "Swaziland"
[168]=>
string(6) "Sweden"
[169]=>
string(11) "Switzerland"
[170]=>
string(5) "Syria"
[171]=>
string(6) "Taiwan"
[172]=>
string(10) "Tajikistan"
[173]=>
string(8) "Tanzania"
[174]=>
string(8) "Thailand"
[175]=>
string(4) "Togo"
[176]=>
string(5) "Tonga"
[177]=>
string(17) "Trinidad & Tobago"
[178]=>
string(7) "Tunisia"
[179]=>
string(6) "Turkey"
[180]=>
string(12) "Turkmenistan"
[181]=>
string(6) "Tuvalu"
[182]=>
string(6) "Uganda"
[183]=>
string(7) "Ukraine"
[184]=>
string(20) "United Arab Emirates"
[185]=>
string(14) "United Kingdom"
[186]=>
string(13) "United States"
[187]=>
string(7) "Uruguay"
[188]=>
string(10) "Uzbekistan"
[189]=>
string(7) "Vanuatu"
[190]=>
string(12) "Vatican City"
[191]=>
string(9) "Venezuela"
[192]=>
string(7) "Vietnam"
[193]=>
string(5) "Yemen"
[194]=>
string(6) "Zambia"
[195]=>
string(8) "Zimbabwe"
}
Try to replace this bit:
foreach ($DBcountries as $cross){
if($cross == $country){
$selected = 'selected';
}else{
$selected = '';
}
}
By this:
foreach ($DBcountries as $cross){
$selected = '';
if($cross == $country) {
$selected = 'selected';
}
}
This way all the countries should be selected. The reason is that even if you set a country to selected you overwrite that again in the next iteration of the loop.
Somewhat more efficient would be this:
foreach ($DBcountries as $cross){
$selected = '';
if($cross == $country) {
$selected = 'selected';
break;
}
}
Break out of the loop when the country is found. But shorter would be:
$selected = in_array($country, $DBcountries) ? 'selected' : '';
Yes, that's all that's needed to replace that loop.

Array with 91 items, trying to reduce redundancy

I have an array with 91 items, and I don't want to constantly use $data[1], $data[2], etc. and it seems redundant to make 91 separate variables just to make it easier to remember what each data item does.
Does anyone know how I can give each of these items a name easier? Or am I just stuck with manually naming each one...
array(91) { [0]=> string(6) "537146" [1]=> string(4) "1212" [2]=> string(7) "7902400" [3]=> string(6) "670563" [4]=> string(2) "69" [5]=> string(6) "674789" [6]=> string(6) "576650" [7]=> string(2) "68" [8]=> string(6) "628147" [9]=> string(6) "690316" [10]=> string(2) "71" [11]=> string(6) "831597" [12]=> string(6) "746547" [13]=> string(2) "70" [14]=> string(6) "776478" [15]=> string(6) "832324" [16]=> string(2) "65" [17]=> string(6) "455705" [18]=> string(6) "710640" [19]=> string(2) "47" [20]=> string(5) "76682" [21]=> string(6) "722402" [22]=> string(2) "66" [23]=> string(6) "512396" [24]=> string(6) "863938" [25]=> string(2) "51" [26]=> string(6) "123086" [27]=> string(6) "143396" [28]=> string(2) "80" [29]=> string(7) "2163323" [30]=> string(6) "435239" [31]=> string(2) "67" [32]=> string(6) "560102" [33]=> string(7) "1078055" [34]=> string(2) "47" [35]=> string(5) "80051" [36]=> string(6) "601043" [37]=> string(2) "51" [38]=> string(6) "114870" [39]=> string(6) "715865" [40]=> string(2) "51" [41]=> string(6) "112269" [42]=> string(6) "403800" [43]=> string(2) "57" [44]=> string(6) "219098" [45]=> string(6) "697401" [46]=> string(2) "52" [47]=> string(6) "124179" [48]=> string(6) "599311" [49]=> string(2) "38" [50]=> string(5) "32070" [51]=> string(6) "769359" [52]=> string(2) "44" [53]=> string(5) "59335" [54]=> string(6) "472791" [55]=> string(2) "53" [56]=> string(6) "138904" [57]=> string(6) "591165" [58]=> string(2) "55" [59]=> string(6) "172795" [60]=> string(6) "500981" [61]=> string(2) "33" [62]=> string(5) "19433" [63]=> string(6) "650383" [64]=> string(2) "23" [65]=> string(4) "6979" [66]=> string(6) "552008" [67]=> string(2) "29" [68]=> string(5) "12185" [69]=> string(6) "692439" [70]=> string(2) "25" [71]=> string(4) "7927" [72]=> string(2) "-1" [73]=> string(2) "-1" [74]=> string(2) "-1" [75]=> string(2) "-1" [76]=> string(6) "483773" [77]=> string(1) "6" [78]=> string(6) "362146" [79]=> string(1) "3" [80]=> string(6) "347664" [81]=> string(1) "3" [82]=> string(2) "-1" [83]=> string(2) "-1" [84]=> string(2) "-1" [85]=> string(2) "-1" [86]=> string(2) "-1" [87]=> string(2) "-1" [88]=> string(2) "-1" [89]=> string(2) "-1" [90]=> string(0) "" }
You can make a try like this way to create multiple variables for your each and every element of array using foreach().
foreach($array as $key=>$value){
${"var_$key"} = $value;
}
echo "variable no 1 is = $var_1, variable no 50 is = $var_50, and variable no 90 is = $var_90";
DEMO: https://3v4l.org/DXvVp

Paypal API Refund Payment (PHP API)

this is my first time posting:
I wish to integrate PayPal API into my site, I can get the payment successfully done but not the refund part. I see an refund api in the lib folder; however, I am not sure which ID to use, I don't see a lot of examples regarding refund.
This is what I have now:
$refund = new Refund();
$refund->setId($payment_id);
$refund->setAmount(1000);
$refund->get($payment_id, $this->apiContext);
try {
//$refund = Refund::get();
} catch (Exception $ex) {
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
ResultPrinter::printError("Get Payment", "Payment", null, null, $ex);
exit(1);
}
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
return json_decode($payment, 128);
Where Payment Detail is:
array(10) {
["id"]=>
string(28) "PAY-1WL153218P032830GKZK2BLY"
["intent"]=>
string(4) "sale"
["state"]=>
string(7) "created"
["cart"]=>
string(17) "08X596883D298843B"
["payer"]=>
array(3) {
["payment_method"]=>
string(6) "paypal"
["status"]=>
string(8) "VERIFIED"
["payer_info"]=>
array(8) {
["email"]=>
string(32) "ted.chou12-facilitator#gmail.com"
["first_name"]=>
string(4) "test"
["last_name"]=>
string(11) "facilitator"
["payer_id"]=>
string(13) "GGYZW23EGRAY4"
["shipping_address"]=>
array(7) {
["recipient_name"]=>
string(16) "test facilitator"
["line1"]=>
string(22) "88 Des Voeux Road West"
["line2"]=>
string(6) "3F 520"
["city"]=>
string(9) "Hong Kong"
["state"]=>
string(9) "Hong Kong"
["postal_code"]=>
string(6) "000000"
["country_code"]=>
string(2) "HK"
}
["phone"]=>
string(10) "4088069385"
["country_code"]=>
string(2) "US"
["billing_address"]=>
array(6) {
["line1"]=>
string(22) "88 Des Voeux Road West"
["line2"]=>
string(6) "3F 520"
["city"]=>
string(9) "Hong Kong"
["state"]=>
string(9) "Hong Kong"
["postal_code"]=>
string(6) "000000"
["country_code"]=>
string(2) "HK"
}
}
}
["transactions"]=>
array(1) {
[0]=>
array(6) {
["amount"]=>
array(3) {
["total"]=>
string(7) "2340.00"
["currency"]=>
string(3) "TWD"
["details"]=>
array(3) {
["subtotal"]=>
string(7) "2310.00"
["tax"]=>
string(4) "0.00"
["shipping"]=>
string(5) "30.00"
}
}
["payee"]=>
array(1) {
["email"]=>
string(35) "jaypatel512-facilitator#hotmail.com"
}
["description"]=>
string(19) "Payment description"
["invoice_number"]=>
string(13) "5655a0adceedf"
["item_list"]=>
array(2) {
["items"]=>
array(3) {
[0]=>
array(5) {
["name"]=>
string(2) "25"
["sku"]=>
string(2) "11"
["price"]=>
string(6) "529.00"
["currency"]=>
string(3) "TWD"
["quantity"]=>
int(3)
}
[1]=>
array(5) {
["name"]=>
string(13) "Warrior Model"
["sku"]=>
string(2) "15"
["price"]=>
string(6) "180.00"
["currency"]=>
string(3) "TWD"
["quantity"]=>
int(1)
}
[2]=>
array(5) {
["name"]=>
string(7) "dim_sum"
["sku"]=>
string(2) "22"
["price"]=>
string(6) "543.00"
["currency"]=>
string(3) "TWD"
["quantity"]=>
int(1)
}
}
["shipping_address"]=>
array(7) {
["recipient_name"]=>
string(16) "test facilitator"
["line1"]=>
string(22) "88 Des Voeux Road West"
["line2"]=>
string(6) "3F 520"
["city"]=>
string(9) "Hong Kong"
["state"]=>
string(9) "Hong Kong"
["postal_code"]=>
string(6) "000000"
["country_code"]=>
string(2) "HK"
}
}
["related_resources"]=>
array(0) {
}
}
}
["redirect_urls"]=>
array(2) {
["return_url"]=>
string(97) "..."
["cancel_url"]=>
string(59) "..."
}
["create_time"]=>
string(20) "2015-11-25T11:51:10Z"
["update_time"]=>
string(20) "2015-11-25T11:51:10Z"
["links"]=>
array(3) {
[0]=>
array(3) {
["href"]=>
string(79) "..."
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
[1]=>
array(3) {
["href"]=>
string(87) ".../execute"
["rel"]=>
string(7) "execute"
["method"]=>
string(4) "POST"
}
[2]=>
array(3) {
["href"]=>
string(94) "..."
["rel"]=>
string(12) "approval_url"
["method"]=>
string(8) "REDIRECT"
}
}
}
Once you execute the payment, you will get back a sale=>id. That's the id you would use for a refund.
It will also include a full URI for you under transactions=>related_resources=>links with the 'rel' value of 'refund'.
Source: https://developer.paypal.com/docs/api/#refunds
Each of the payment will produce an sale_id as an example 4CFG677234DFG234F. This ID will be use to make a refund.
Don't get yourself confuse with the payment id and the sale_id.

Search Multidimensional Array in PHP

How can I search the following multidimensional array for the string 'uk'?
array(43)
{
[0]=> array(1) { ["country"]=> string(9) "Australia" }
[1]=> array(1) { ["country"]=> string(7) "Bahrain" }
[2]=> array(1) { ["country"]=> string(7) "Belgium" }
[3]=> array(1) { ["country"]=> string(8) "Bulgaria" }
[4]=> array(1) { ["country"]=> string(6) "Canada" }
[5]=> array(1) { ["country"]=> string(7) "Croatia" }
[6]=> array(1) { ["country"]=> string(6) "Cyprus" }
[7]=> array(1) { ["country"]=> string(14) "Czech Republic" }
[8]=> array(1) { ["country"]=> string(7) "Denmark" }
[9]=> array(1) { ["country"]=> string(7) "Finland" }
[10]=> array(1) { ["country"]=> string(7) "Germany" }
[11]=> array(1) { ["country"]=> string(6) "Greece" }
[12]=> array(1) { ["country"]=> string(9) "Hong Kong" }
[13]=> array(1) { ["country"]=> string(7) "Hungary" }
[14]=> array(1) { ["country"]=> string(7) "Iceland" }
[15]=> array(1) { ["country"]=> string(5) "India" }
[16]=> array(1) { ["country"]=> string(4) "Iran" }
[17]=> array(1) { ["country"]=> string(6) "Israel" }
[18]=> array(1) { ["country"]=> string(5) "Italy" }
[19]=> array(1) { ["country"]=> string(5) "Japan" }
[20]=> array(1) { ["country"]=> string(5) "Korea" }
[21]=> array(1) { ["country"]=> string(6) "Kuwait" }
[22]=> array(1) { ["country"]=> string(5) "Malta" }
[23]=> array(1) { ["country"]=> string(8) "Mongolia" }
[24]=> array(1) { ["country"]=> string(11) "Netherlands" }
[25]=> array(1) { ["country"]=> string(6) "Norway" }
[26]=> array(1) { ["country"]=> string(8) "Pakistan" }
[27]=> array(1) { ["country"]=> string(6) "Poland" }
[28]=> array(1) { ["country"]=> string(7) "Romania" }
[29]=> array(1) { ["country"]=> string(6) "Russia" }
[30]=> array(1) { ["country"]=> string(9) "Singapore" }
[31]=> array(1) { ["country"]=> string(8) "Slovakia" }
[32]=> array(1) { ["country"]=> string(12) "South Africa" }
[33]=> array(1) { ["country"]=> string(13) "South America" }
[34]=> array(1) { ["country"]=> string(5) "Spain" }
[35]=> array(1) { ["country"]=> string(6) "Sweden" }
[36]=> array(1) { ["country"]=> string(11) "Switzerland" }
[37]=> array(1) { ["country"]=> string(6) "Taiwan" }
[38]=> array(1) { ["country"]=> string(10) "Tajikistan" }
[39]=> array(1) { ["country"]=> string(2) "UK" }
[40]=> array(1) { ["country"]=> string(20) "United Arab Emirates" }
[41]=> array(1) { ["country"]=> string(24) "United States of America" }
[42]=> array(1) { ["country"]=> string(7) "Vietnam" }
}
1) Why are you encapsulating all your simple strings in a new array?
1a) If there actually is a reason:
function my_search($needle, $haystack)
{
foreach($haystack as $k => $v)
{
if ($v["country"] == $needle)
return $k;
}
return FALSE;
}
$key = my_search("uk", $yourArray);
if($key !== FALSE)
{
...
} else {
echo "Not Found.";
}
This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE. Please read the section on
Booleans for more information. Use the === operator for testing the
return value of this function.
1b) if there isn't:
2) Fix your array using simple strings as values, not array(1)'s of strings
3) If it's fixed, you can simply use array_search()
Edit: Changed arguments to resemble array_search more.
If you are using (PHP 5 >= 5.5.0) there is a better and simple way:
if(array_search('UK', array_column($array, 'country')) !== false ){
echo 'found';
}else{
echo 'Not found';
}

I got many duplicates with this function

I know something wrong with this function. It is supposed to return an array with 12 keys and values but it returns 60. The problem should be the foreach loop. Everytime the if loop is false, it passes a value to $result. Can anyone tell me how to make it right?
function getFinaluserElearningLesson($array1, $array2){
$result = array();
for($i = 0; $i<count($array1); $i++){
foreach($array2 as $lesson2){
if($array1[$i]['lesson'] == $lesson2['lesson_id'] && $array1[$i]['user_id'] == $lesson2['user_id']){
$result[] = array(
'first_name' => $array1[$i]['first_name'],
'last_name' => $array1[$i]['last_name'],
'date_of_birth' => $array1[$i]['date_of_birth'],
'user_id' => $array1[$i]['user_id'],
'client_id' => $array1[$i]['client_id'],
'lesson' => $array1[$i]['lesson'],
'lessonName' => $array1[$i]['lessonName'],
'completion' => 'Completed'
);
}else{
$result[] = array(
'first_name' => $array1[$i]['first_name'],
'last_name' => $array1[$i]['last_name'],
'date_of_birth' => $array1[$i]['date_of_birth'],
'user_id' => $array1[$i]['user_id'],
'client_id' => $array1[$i]['client_id'],
'lesson' => $array1[$i]['lesson'],
'lessonName' => $array1[$i]['lessonName'],
'completion' => 'Incomplete'
);
}
}
}
return $result;
}
The output of array1 and array2 is:
array(12) {
[0]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "222"
["lessonName"]=>
string(6) "Asthma"
}
[1]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(2) "69"
["lessonName"]=>
string(23) "Cardiac Catheterization"
}
[2]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "308"
["lessonName"]=>
string(38) "CyberKnife Radiosurgery For Brain AVMs"
}
[3]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(2) "50"
["lessonName"]=>
string(47) "Durable Power of Attorney for Health Care (DPA)"
}
[4]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(2) "91"
["lessonName"]=>
string(12) "Heart Attack"
}
[5]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(10) "Wilczewski"
["date_of_birth"]=>
string(10) "1989-05-27"
["user_id"]=>
string(7) "2602204"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "500"
["lessonName"]=>
string(33) "Abdominal Aortic Aneurysm Surgery"
}
[6]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(10) "Wilczewski"
["date_of_birth"]=>
string(10) "1989-05-27"
["user_id"]=>
string(7) "2602204"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(1) "1"
["lessonName"]=>
string(19) "Cholesterol Control"
}
[7]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(10) "Wilczewski"
["date_of_birth"]=>
string(10) "1989-05-27"
["user_id"]=>
string(7) "2602204"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(1) "5"
["lessonName"]=>
string(16) "Types of Doctors"
}
[8]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "500"
["lessonName"]=>
string(33) "Abdominal Aortic Aneurysm Surgery"
}
[9]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "116"
["lessonName"]=>
string(4) "Acne"
}
[10]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "122"
["lessonName"]=>
string(5) "Burns"
}
[11]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "546"
["lessonName"]=>
string(60) "Peripherally Inserted Central Catheter (PICC) for Outpatient"
}
}
array(5) {
[0]=>
array(2) {
["user_id"]=>
string(6) "951301"
["lesson_id"]=>
string(2) "35"
}
[1]=>
array(2) {
["user_id"]=>
string(7) "1700981"
["lesson_id"]=>
string(2) "35"
}
[2]=>
array(2) {
["user_id"]=>
string(7) "2585455"
["lesson_id"]=>
string(2) "25"
}
[3]=>
array(2) {
["user_id"]=>
string(7) "2602204"
["lesson_id"]=>
string(3) "500"
}
[4]=>
array(2) {
["user_id"]=>
string(7) "2642677"
["lesson_id"]=>
string(3) "180"
}
}
You could simplify the inner if() DRASTICALLY by doing this:
if($array1[$i]['lesson'] == $lesson2['lesson_id'] && $array1[$i]['user_id'] == $lesson2['user_id']){
$completion = 'Complete';
} else {
$completion = 'Incomplete';
}
$result[] = array(
'first_name' => $array1[$i]['first_name'],
'last_name' => $array1[$i]['last_name'],
'date_of_birth' => $array1[$i]['date_of_birth'],
'user_id' => $array1[$i]['user_id'],
'client_id' => $array1[$i]['client_id'],
'lesson' => $array1[$i]['lesson'],
'lessonName' => $array1[$i]['lessonName'],
'completion' => $completion // <---- the **ONLY** difference between the two versions
);
As far as your "duplication" problem... I'm not sure what you're trying to accomplish with this. You're running a nested loop, with 5 outer and 12 inner members, which gives your 60 array entries. Since you're assigning something to $result inside the inner loop, no matter WHAT the if() condition returns, that'll produce 60 assignments.
If you don't want the 'false' ones to happen, then don't do anything in the else clause of your code.

Categories