problem with explode, arrays and for each - php
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.
Related
'ImagickException', 'NoDecodeDelegateForThisImageFormat `TMP' # error/constitute.c/ReadImage/501'
I try to install magick, and works, but when i need to work with PDF file i have error. I use xampp php 5.6 Compiler MSVC11 (Visual C++ 2012) Architecture x86 Thread Safety enabled 64bit system windows Server 2012 R2 Bin/Dlls: php_imagick-3.1.2-5.6-ts-vc11-x86 ImageMagick-6.9.3-7-vc11-x86 I try to install ghostpcl-9.27-win32. version: ImageMagick 7.0.8-48 Q16 x86 2019-06-01 $im = new Imagick(); $im->setResolution(300,300); $im->readimage($pdf->tmp_name); $im->setImageFormat('jpeg'); $im->cropImage(500, 500, 50, 1420); $im->writeImage($pdf->tmp_name . '.jpg'); $im->clear(); $im->destroy(); Fatal error: Uncaught exception 'ImagickException' with message 'NoDecodeDelegateForThisImageFormat `TMP' # error/constitute.c/ReadImage/501' in C:\xampp\htdocs\adm_engemed_ts5\eol\scr\savePDF.php:60 Stack trace: #0 C:\xampp\htdocs\adm_engemed_ts5\eol\scr\savePDF.php(60): Imagick->readimage('C:\xampp\tmp\ph...') #1 {main} thrown in C:\xampp\htdocs\adm_engemed_ts5\eol\scr\savePDF.php on line 60 $im->queryFormats(); When i use: array(234) { [0]=> string(3) "3FR" [1]=> string(3) "AAI" [2]=> string(2) "AI" [3]=> string(3) "ART" [4]=> string(3) "ARW" [5]=> string(3) "AVI" [6]=> string(3) "AVS" [7]=> string(3) "BGR" [8]=> string(4) "BGRA" [9]=> string(4) "BGRO" [10]=> string(3) "BIE" [11]=> string(3) "BMP" [12]=> string(4) "BMP2" [13]=> string(4) "BMP3" [14]=> string(3) "BRF" [15]=> string(3) "CAL" [16]=> string(4) "CALS" [17]=> string(6) "CANVAS" [18]=> string(7) "CAPTION" [19]=> string(3) "CIN" [20]=> string(3) "CIP" [21]=> string(4) "CLIP" [22]=> string(9) "CLIPBOARD" [23]=> string(4) "CMYK" [24]=> string(5) "CMYKA" [25]=> string(3) "CR2" [26]=> string(3) "CRW" [27]=> string(3) "CUR" [28]=> string(3) "CUT" [29]=> string(4) "DATA" [30]=> string(3) "DCM" [31]=> string(3) "DCR" [32]=> string(3) "DCX" [33]=> string(3) "DDS" [34]=> string(5) "DFONT" [35]=> string(4) "DJVU" [36]=> string(3) "DNG" [37]=> string(3) "DOT" [38]=> string(3) "DPS" [39]=> string(3) "DPX" [40]=> string(4) "DXT1" [41]=> string(4) "DXT5" [42]=> string(3) "EMF" [43]=> string(4) "EPDF" [44]=> string(3) "EPI" [45]=> string(3) "EPS" [46]=> string(4) "EPS2" [47]=> string(4) "EPS3" [48]=> string(4) "EPSF" [49]=> string(4) "EPSI" [50]=> string(3) "EPT" [51]=> string(4) "EPT2" [52]=> string(4) "EPT3" [53]=> string(3) "ERF" [54]=> string(3) "EXR" [55]=> string(3) "FAX" [56]=> string(4) "FITS" [57]=> string(3) "FPX" [58]=> string(7) "FRACTAL" [59]=> string(3) "FTS" [60]=> string(2) "G3" [61]=> string(3) "GIF" [62]=> string(5) "GIF87" [63]=> string(8) "GRADIENT" [64]=> string(4) "GRAY" [65]=> string(6) "GROUP4" [66]=> string(2) "GV" [67]=> string(1) "H" [68]=> string(4) "HALD" [69]=> string(3) "HDR" [70]=> string(9) "HISTOGRAM" [71]=> string(3) "HRZ" [72]=> string(3) "HTM" [73]=> string(4) "HTML" [74]=> string(3) "ICB" [75]=> string(3) "ICO" [76]=> string(4) "ICON" [77]=> string(3) "IIQ" [78]=> string(4) "INFO" [79]=> string(6) "INLINE" [80]=> string(3) "IPL" [81]=> string(6) "ISOBRL" [82]=> string(7) "ISOBRL6" [83]=> string(3) "J2C" [84]=> string(3) "J2K" [85]=> string(3) "JBG" [86]=> string(4) "JBIG" [87]=> string(3) "JNG" [88]=> string(3) "JNX" [89]=> string(3) "JP2" [90]=> string(3) "JPC" [91]=> string(3) "JPE" [92]=> string(4) "JPEG" [93]=> string(3) "JPG" [94]=> string(3) "JPM" [95]=> string(3) "JPS" [96]=> string(3) "JPT" [97]=> string(4) "JSON" [98]=> string(3) "K25" [99]=> string(3) "KDC" [100]=> string(5) "LABEL" [101]=> string(3) "M2V" [102]=> string(3) "M4V" [103]=> string(3) "MAC" [104]=> string(6) "MAGICK" [105]=> string(3) "MAP" [106]=> string(4) "MASK" [107]=> string(3) "MAT" [108]=> string(5) "MATTE" [109]=> string(3) "MEF" [110]=> string(4) "MIFF" [111]=> string(3) "MKV" [112]=> string(3) "MNG" [113]=> string(4) "MONO" [114]=> string(3) "MOV" [115]=> string(3) "MP4" [116]=> string(3) "MPC" [117]=> string(4) "MPEG" [118]=> string(3) "MPG" [119]=> string(3) "MRW" [120]=> string(3) "MSL" [121]=> string(4) "MSVG" [122]=> string(3) "MTV" [123]=> string(3) "MVG" [124]=> string(3) "NEF" [125]=> string(3) "NRW" [126]=> string(4) "NULL" [127]=> string(3) "ORF" [128]=> string(3) "OTB" [129]=> string(3) "OTF" [130]=> string(3) "PAL" [131]=> string(4) "PALM" [132]=> string(3) "PAM" [133]=> string(5) "PANGO" [134]=> string(7) "PATTERN" [135]=> string(3) "PBM" [136]=> string(3) "PCD" [137]=> string(4) "PCDS" [138]=> string(3) "PCL" [139]=> string(3) "PCT" [140]=> string(3) "PCX" [141]=> string(3) "PDB" [142]=> string(3) "PDF" [143]=> string(4) "PDFA" [144]=> string(3) "PEF" [145]=> string(3) "PES" [146]=> string(3) "PFA" [147]=> string(3) "PFB" [148]=> string(3) "PFM" [149]=> string(3) "PGM" [150]=> string(5) "PICON" [151]=> string(4) "PICT" [152]=> string(3) "PIX" [153]=> string(5) "PJPEG" [154]=> string(6) "PLASMA" [155]=> string(3) "PNG" [156]=> string(5) "PNG00" [157]=> string(5) "PNG24" [158]=> string(5) "PNG32" [159]=> string(5) "PNG48" [160]=> string(5) "PNG64" [161]=> string(4) "PNG8" [162]=> string(3) "PNM" [163]=> string(3) "PPM" [164]=> string(7) "PREVIEW" [165]=> string(2) "PS" [166]=> string(3) "PS2" [167]=> string(3) "PS3" [168]=> string(3) "PSB" [169]=> string(3) "PSD" [170]=> string(4) "PTIF" [171]=> string(3) "PWP" [172]=> string(15) "RADIAL-GRADIENT" [173]=> string(3) "RAF" [174]=> string(3) "RAS" [175]=> string(3) "RAW" [176]=> string(3) "RGB" [177]=> string(4) "RGBA" [178]=> string(4) "RGBO" [179]=> string(3) "RGF" [180]=> string(3) "RLA" [181]=> string(3) "RLE" [182]=> string(3) "RMF" [183]=> string(3) "RW2" [184]=> string(3) "SCR" [185]=> string(10) "SCREENSHOT" [186]=> string(3) "SCT" [187]=> string(3) "SFW" [188]=> string(3) "SGI" [189]=> string(5) "SHTML" [190]=> string(3) "SIX" [191]=> string(5) "SIXEL" [192]=> string(12) "SPARSE-COLOR" [193]=> string(3) "SR2" [194]=> string(3) "SRF" [195]=> string(7) "STEGANO" [196]=> string(3) "SUN" [197]=> string(3) "SVG" [198]=> string(4) "SVGZ" [199]=> string(4) "TEXT" [200]=> string(3) "TGA" [201]=> string(9) "THUMBNAIL" [202]=> string(4) "TIFF" [203]=> string(6) "TIFF64" [204]=> string(4) "TILE" [205]=> string(3) "TIM" [206]=> string(3) "TTC" [207]=> string(3) "TTF" [208]=> string(3) "TXT" [209]=> string(4) "UBRL" [210]=> string(5) "UBRL6" [211]=> string(3) "UIL" [212]=> string(4) "UYVY" [213]=> string(3) "VDA" [214]=> string(5) "VICAR" [215]=> string(3) "VID" [216]=> string(4) "VIFF" [217]=> string(4) "VIPS" [218]=> string(3) "VST" [219]=> string(4) "WBMP" [220]=> string(4) "WEBP" [221]=> string(3) "WMF" [222]=> string(3) "WMV" [223]=> string(3) "WPG" [224]=> string(3) "X3F" [225]=> string(3) "XBM" [226]=> string(2) "XC" [227]=> string(3) "XCF" [228]=> string(3) "XPM" [229]=> string(3) "XPS" [230]=> string(2) "XV" [231]=> string(5) "YCbCr" [232]=> string(6) "YCbCrA" [233]=> string(3) "YUV" }
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
Search Array and find Key Value
I have a array array(153) { [0]=> string(47) "colg - colgate palmolive (pakistan) ltd. " [1]=> string(47) "gatm - gul ahmed textile mills ltd. " [2]=> string(47) "atlh - atlas honda limited " [3]=> string(33) "ffbl - fauji fertilizer bin qasim" [4]=> string(16) " - general news" [5]=> string(49) "searl - the searl company ltd. " [6]=> string(47) "jdws - j. d. w. sugar mills ltd. " [7]=> string(47) "aabs - al- abbas sugar mills limited. " [8]=> string(37) "hmb - habib metropolitan bank limited" [9]=> string(37) "hmb - habib metropolitan bank limited" [10]=> string(23) "ogdc - oil & gas dev.co" [11]=> string(39) "ffc - fauji fertilizer company limited." [12]=> string(46) "bop - bank of punjab limited. " [13]=> string(46) "bop - bank of punjab limited. " [14]=> string(30) "luck - lucky cement limited " [15]=> string(47) "natf - national foods limited. " [16]=> string(46) "pol - pakistan oilfields ltd. " [17]=> string(47) "psmc - pak suzuki motor co. ltd. " [18]=> string(35) "nbp - national bank of pakistan ltd" [19]=> string(27) "ncpl - nishat chunian power" [20]=> string(25) "tgl - tariq glass limited" [21]=> string(47) "bahl - bank al-habib limited " [22]=> string(33) "ffbl - fauji fertilizer bin qasim" [23]=> string(136) " kse 100 kse 30 kmi 30 " [24]=> string(9) "26,730.82" [25]=> string(5) "60.22" [26]=> string(18) "volume: 96,370,040" [27]=> string(9) "19,289.00" [28]=> string(6) "-18.68" [29]=> string(18) "volume: 43,582,400" [30]=> string(9) "43,968.61" [31]=> string(6) "-61.00" [32]=> string(18) "volume: 27,252,400" [33]=> string(3) "anl" [34]=> string(4) "9.73" [35]=> string(4) "0.58" [36]=> string(10) "23,192,000" [37]=> string(4) "jscl" [38]=> string(5) "12.53" [39]=> string(4) "1.00" [40]=> string(10) "20,936,000" [41]=> string(4) "jsil" [42]=> string(5) "13.75" [43]=> string(4) "0.35" [44]=> string(10) "10,966,500" [45]=> string(5) "power" [46]=> string(4) "8.00" [47]=> string(4) "0.71" [48]=> string(9) "7,161,000" [49]=> string(4) "piaa" [50]=> string(4) "9.80" [51]=> string(4) "0.16" [52]=> string(9) "6,978,500" [53]=> string(4) "lpcl" [54]=> string(5) "10.40" [55]=> string(5) "-0.04" [56]=> string(9) "5,988,500" [57]=> string(3) "bop" [58]=> string(5) "11.34" [59]=> string(4) "0.11" [60]=> string(9) "5,785,000" [61]=> string(4) "byco" [62]=> string(4) "9.39" [63]=> string(4) "0.47" [64]=> string(9) "4,972,000" [65]=> string(5) "kasbb" [66]=> string(4) "2.39" [67]=> string(4) "0.34" [68]=> string(9) "4,910,500" [69]=> string(3) "spl" [70]=> string(5) "19.78" [71]=> string(4) "0.65" [72]=> string(9) "4,600,000" [73]=> string(6) "nestle" [74]=> string(9) "11,989.16" [75]=> string(6) "570.91" [76]=> string(5) "2,100" [77]=> string(4) "iltm" [78]=> string(8) "1,564.92" [79]=> string(5) "74.52" [80]=> string(3) "500" [81]=> string(5) "mureb" [82]=> string(6) "913.18" [83]=> string(5) "43.48" [84]=> string(6) "57,500" [85]=> string(5) "wyeth" [86]=> string(8) "4,600.00" [87]=> string(5) "33.50" [88]=> string(3) "600" [89]=> string(4) "colg" [90]=> string(8) "1,700.00" [91]=> string(5) "15.50" [92]=> string(3) "140" [93]=> string(4) "hino" [94]=> string(6) "292.87" [95]=> string(5) "13.94" [96]=> string(3) "400" [97]=> string(3) "ici" [98]=> string(6) "323.00" [99]=> string(5) "11.55" [100]=> string(7) "103,500" [101]=> string(4) "pict" [102]=> string(6) "270.98" [103]=> string(4) "9.88" [104]=> string(6) "29,200" [105]=> string(4) "pkgs" [106]=> string(6) "309.00" [107]=> string(4) "9.04" [108]=> string(6) "37,800" [109]=> string(4) "jdmt" [110]=> string(6) "168.49" [111]=> string(4) "8.02" [112]=> string(5) "3,500" [113]=> string(4) "upfl" [114]=> string(8) "9,100.00" [115]=> string(6) "-90.00" [116]=> string(3) "120" [117]=> string(4) "mffl" [118]=> string(6) "855.00" [119]=> string(6) "-43.33" [120]=> string(3) "300" [121]=> string(4) "siem" [122]=> string(8) "1,331.00" [123]=> string(6) "-38.00" [124]=> string(3) "600" [125]=> string(4) "pakt" [126]=> string(6) "620.00" [127]=> string(6) "-27.90" [128]=> string(5) "1,000" [129]=> string(3) "sfl" [130]=> string(6) "409.00" [131]=> string(6) "-15.90" [132]=> string(5) "9,400" [133]=> string(4) "gadt" [134]=> string(6) "286.00" [135]=> string(6) "-13.37" [136]=> string(6) "48,800" [137]=> string(4) "atba" [138]=> string(6) "394.79" [139]=> string(6) "-12.96" [140]=> string(3) "900" [141]=> string(3) "btl" [142]=> string(6) "265.51" [143]=> string(5) "-8.49" [144]=> string(5) "1,200" [145]=> string(4) "natf" [146]=> string(6) "616.00" [147]=> string(5) "-8.26" [148]=> string(5) "1,200" [149]=> string(4) "admm" [150]=> string(6) "121.51" [151]=> string(5) "-6.39" [152]=> string(6) "98,500" } And i want to find "kse 100 kse 30 kmi 30" which is located at key number 23 I have tried in_array and other php array functions including preg_match but it dont find string in array. Is there a way to find string in my array. If yes then tell me
Well your "kse 100 kse 30 kmi 30" has a space " " before and after the string in your array. string(136) " kse 100 kse 30 kmi 30 " Is this the reason why you cannot find it? Because array_serach, in_array and other functions are looking for exact values. Otherwise use strstr to compare if your string is inside your haystack but be careful. If you have similar content like "Hello" and "Hello World" in your array you might find the wrong entry to this is not really a good unique solution. Anyway I am leaving a quick example: $found = false; foreach($myArray as $key => $value) { if(strstr($value, "kse 100 kse 30 kmi 30") == true) { $found = true; break; } } if($found == true) { //Do something awesome! } else { //Do something not so awesome but still cool? }
You should make use of array_search Searches the array for a given value and returns the corresponding key if successful So it must be like .. echo $key = array_search('kse 100 kse 30 kmi 30', $yourarray); //"prints" 23 EDIT: <?php $array = [ 0 => "colg - colgate palmolive (pakistan) ltd. ", 1 => "gatm - gul ahmed textile mills ltd. ", 2 => "atlh - atlas honda limited ", 3 => "ffbl - fauji fertilizer bin qasim", 4 => " - general news", 5 => "searl - the searl company ltd. ", 23 => " kse 100 kse 30 kmi 30 ", 24 => "26,730.82", ]; $array = array_map('trim',$array); $searchText = 'kse 100 kse 30 kmi 30'; $searchText = trim($searchText); echo $key = array_search($searchText, $array); //"prints" 23 Demo
Thanks For you help but i sort it out myself for ($index = 10; $index < 35; $index++) { if (substr_count(array[$index], 'kse 100') > 0) { $IndexKey = $index; } } Got The value of key where that variable is.
For the first one that matches: $key = key(preg_grep('/kse 100/', $array));
Magento Geting Data from Serializd array
i am trying to get custom option values from sales order items. i wrote a code as below foreach ($order->getAllItems() as $item){ $productOptions = $item->getProductOptions(); foreach($productOptions as $opt){ var_dump($opt); } } but i don't know ho to access single options through this array. ex- $opt->Title not getting anything for me here is var dump for one item array(7) { ["uenc"]=> string(80) "aHR0cDovL3ZhbmhhZ2UuY2FtZm9saW8uY28udWsvc2hvcC1vbmxpbmUvbG95YWx0eS1jYXJkLmh0bWw," ["product"]=> string(4) "1841" ["related_product"]=> string(0) "" ["super_attribute"]=> array(1) { [1000]=> string(5) "11160" } ["options"]=> array(13) { [1574]=> string(2) "Mr" [1575]=> string(7) "Cameron" [1576]=> string(4) "Owen" [1577]=> string(1) "6" [1578]=> string(14) "Address Line 1" [1579]=> string(14) "Address Line 2" [1580]=> string(4) "Town" [1581]=> string(6) "County" [1582]=> string(7) "B17 6AG" [1583]=> string(12) "0121 424 578" [1584]=> string(11) "07811877865" [1585]=> string(10) "19/04/1987" [1573]=> string(20) "hello#camfolio.co.uk" } ["cpid"]=> string(4) "1840" ["qty"]=> int(1) } array(13) { [0]=> array(7) { ["label"]=> string(5) "Title" ["value"]=> string(2) "Mr" ["print_value"]=> string(2) "Mr" ["option_id"]=> string(4) "1574" ["option_type"]=> string(5) "field" ["option_value"]=> string(2) "Mr" ["custom_view"]=> bool(false) } [1]=> array(7) { ["label"]=> string(10) "First Name" ["value"]=> string(7) "Cameron" ["print_value"]=> string(7) "Cameron" ["option_id"]=> string(4) "1575" ["option_type"]=> string(5) "field" ["option_value"]=> string(7) "Cameron" ["custom_view"]=> bool(false) } [2]=> array(7) { ["label"]=> string(9) "Last Name" ["value"]=> string(4) "Owen" ["print_value"]=> string(4) "Owen" ["option_id"]=> string(4) "1576" ["option_type"]=> string(5) "field" ["option_value"]=> string(4) "Owen" ["custom_view"]=> bool(false) } [3]=> array(7) { ["label"]=> string(12) "House Number" ["value"]=> string(1) "6" ["print_value"]=> string(1) "6" ["option_id"]=> string(4) "1577" ["option_type"]=> string(5) "field" ["option_value"]=> string(1) "6" ["custom_view"]=> bool(false) } [4]=> array(7) { ["label"]=> string(14) "Address Line 1" ["value"]=> string(14) "Address Line 1" ["print_value"]=> string(14) "Address Line 1" ["option_id"]=> string(4) "1578" ["option_type"]=> string(5) "field" ["option_value"]=> string(14) "Address Line 1" ["custom_view"]=> bool(false) } [5]=> array(7) { ["label"]=> string(14) "Address Line 2" ["value"]=> string(14) "Address Line 2" ["print_value"]=> string(14) "Address Line 2" ["option_id"]=> string(4) "1579" ["option_type"]=> string(5) "field" ["option_value"]=> string(14) "Address Line 2" ["custom_view"]=> bool(false) } [6]=> array(7) { ["label"]=> string(4) "Town" ["value"]=> string(4) "Town" ["print_value"]=> string(4) "Town" ["option_id"]=> string(4) "1580" ["option_type"]=> string(5) "field" ["option_value"]=> string(4) "Town" ["custom_view"]=> bool(false) } [7]=> array(7) { ["label"]=> string(6) "County" ["value"]=> string(6) "County" ["print_value"]=> string(6) "County" ["option_id"]=> string(4) "1581" ["option_type"]=> string(5) "field" ["option_value"]=> string(6) "County" ["custom_view"]=> bool(false) } [8]=> array(7) { ["label"]=> string(8) "Postcode" ["value"]=> string(7) "B17 6AG" ["print_value"]=> string(7) "B17 6AG" ["option_id"]=> string(4) "1582" ["option_type"]=> string(5) "field" ["option_value"]=> string(7) "B17 6AG" ["custom_view"]=> bool(false) } [9]=> array(7) { ["label"]=> string(16) "Telephone Number" ["value"]=> string(12) "0121 424 578" ["print_value"]=> string(12) "0121 424 578" ["option_id"]=> string(4) "1583" ["option_type"]=> string(5) "field" ["option_value"]=> string(12) "0121 424 578" ["custom_view"]=> bool(false) } [10]=> array(7) { ["label"]=> string(13) "Mobile Number" ["value"]=> string(11) "07811877865" ["print_value"]=> string(11) "07811877865" ["option_id"]=> string(4) "1584" ["option_type"]=> string(5) "field" ["option_value"]=> string(11) "07811877865" ["custom_view"]=> bool(false) } [11]=> array(7) { ["label"]=> string(13) "Date of Birth" ["value"]=> string(10) "19/04/1987" ["print_value"]=> string(10) "19/04/1987" ["option_id"]=> string(4) "1585" ["option_type"]=> string(5) "field" ["option_value"]=> string(10) "19/04/1987" ["custom_view"]=> bool(false) } [12]=> array(7) { ["label"]=> string(13) "Email Address" ["value"]=> string(20) "hello#camfolio.co.uk" ["print_value"]=> string(20) "hello#camfolio.co.uk" ["option_id"]=> string(4) "1573" ["option_type"]=> string(5) "field" ["option_value"]=> string(20) "hello#camfolio.co.uk" ["custom_view"]=> bool(false) } } can anyone help me for get the datas from this ex Email Address Date of Birth etc.... thank you
$opt is a multidimensional array. So if you want to access the first name, you would use: $options=$opt['options']; $name=$options[1575]; But this can be hard work. I really suggest you create a class (if there isn't one) and deserialize an object out of $opt.
json to array to mysql not cycling through data
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