'ImagickException', 'NoDecodeDelegateForThisImageFormat `TMP' # error/constitute.c/ReadImage/501' - php

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"
}

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

Parse JSON object with PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I've read every single question on StackOverflow on how to parse JSON with PHP but none of them are working with the JSON I have.
array(2) { ["status"]=> bool(true) ["currency"]=> array(30) { [0]=>
array(10) { ["currency"]=> string(7) "EUR/AUD" ["value"]=> string(6)
"1.4925" ["change"]=> string(7) "-0.0076" ["change_percent"]=>
string(8) "-0.50663" ["ask"]=> string(6) "1.4926" ["bid"]=> string(6)
"1.4923" ["daily_lowest"]=> string(6) "1.4873" ["daily_highest"]=>
string(6) "1.5016" ["date"]=> string(19) "2017-08-16 09:46:11"
["type"]=> string(8) "original" } [1]=> array(10) { ["currency"]=>
string(7) "EUR/CAD" ["value"]=> string(5) "1.494" ["change"]=>
string(7) "-0.0032" ["change_percent"]=> string(8) "-0.21373"
["ask"]=> string(5) "1.494" ["bid"]=> string(6) "1.4939"
["daily_lowest"]=> string(5) "1.489" ["daily_highest"]=> string(6)
"1.4986" ["date"]=> string(19) "2017-08-16 09:46:11" ["type"]=>
string(8) "original" } [2]=> array(10) { ["currency"]=> string(7)
"EUR/CHF" ["value"]=> string(6) "1.1434" ["change"]=> string(6)
"0.0024" ["change_percent"]=> string(7) "0.21034" ["ask"]=> string(6)
"1.1435" ["bid"]=> string(6) "1.1434" ["daily_lowest"]=> string(6)
"1.1402" ["daily_highest"]=> string(6) "1.1444" ["date"]=> string(19)
"2017-08-16 09:46:11" ["type"]=> string(8) "original" } [3]=>
array(10) { ["currency"]=> string(7) "EUR/GBP" ["value"]=> string(6)
"0.9099" ["change"]=> string(7) "-0.0019" ["change_percent"]=>
string(8) "-0.20838" ["ask"]=> string(4) "0.91" ["bid"]=> string(6)
"0.9099" ["daily_lowest"]=> string(6) "0.9083" ["daily_highest"]=>
string(6) "0.9144" ["date"]=> string(19) "2017-08-16 09:46:11"
["type"]=> string(8) "original" } [4]=> array(10) { ["currency"]=>
string(7) "EUR/ILS" ["type"]=> string(9) "converted" ["date"]=>
string(19) "2017-08-16 09:46:11" ["value"]=> string(7) "4.24323"
["change"]=> string(3) "0.0" ["change_percent"]=> string(3) "0.0"
["ask"]=> string(3) "0.0" ["bid"]=> string(3) "0.0" ["daily_lowest"]=>
string(3) "0.0" ["daily_highest"]=> string(3) "0.0" } [5]=> array(10)
{ ["currency"]=> string(7) "EUR/JPY" ["value"]=> string(8) "130.1145"
["change"]=> string(6) "0.2425" ["change_percent"]=> string(7)
"0.18672" ["ask"]=> string(7) "130.122" ["bid"]=> string(7) "130.107"
["daily_lowest"]=> string(7) "129.613" ["daily_highest"]=> string(7)
"130.405" ["date"]=> string(19) "2017-08-16 09:46:11" ["type"]=>
string(8) "original" } [6]=> array(10) { ["currency"]=> string(7)
"EUR/NZD" ["value"]=> string(6) "1.6182" ["change"]=> string(7)
"-0.0036" ["change_percent"]=> string(8) "-0.22198" ["ask"]=>
string(6) "1.6186" ["bid"]=> string(6) "1.6178" ["daily_lowest"]=>
string(6) "1.6138" ["daily_highest"]=> string(6) "1.6247" ["date"]=>
string(19) "2017-08-16 09:46:11" ["type"]=> string(8) "original" }
[7]=> array(10).............
I'm trying to output the value of each ["currency"] ["bid"] with this code:
foreach($result['currency'][0]['bid'] as $item) {
print $item['currency'][0]['bid'];
}
And tried 100 other variations, still getting errors like:
Warning: Illegal string offset 'currency' in getData.php on line 27
Warning: Illegal string offset 'value' in getData.php on line 27
Warning: Invalid argument supplied for foreach() in getData.php on line 27
Maybe there are online parser websites that can help with that? How do I solve this?
It seems to me like it should be.
foreach($result['currency']as $item) {
print $item['value'];
}
Because each currency is 0,1,2 and so on.
And in the item 0,1,2 there is the "value".

csv header row as array key

I'm able to get the file into an array, but I'd like to get the key's to be the first row. Here's is the curl command:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url.com/download");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "X-App-Key: xxxxx";
$headers[] = "X-User-Key: yyyy";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $result));
return $data;
Here is the csv that is returned:
"Date,Campaign,Campaign Id,Impressions,Clicks,CTR,Platform Cost,Search Cost,Demographic Cost,Contextual Cost,Behavioral Cost,Geographic Cost,Segment Cost,Media Cost,CPM,Total Spend,ECPC,Click Sales,Click Leads,Click Signups,Click Other,View Sales,View Leads,View Signups,View Other,CPA,Weighted Actions 2017-05-16,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-18,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-19,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-20,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-28,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,1,,1.00 2017-06-02,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,1,,1.00 2017-05-10,Company,326606,16877,41,0.002429,0.770373,0.000000,0.0,0.000000,0.0,0.770373,0.000000,1.540785,3.081682,52.009548,1.268526,,,,,,,,,, 2017-05-11,Company,326606,17001,50,0.002941,0.764775,0.000000,0.0,0.000000,0.0,0.764775,0.000000,1.529592,3.059288,52.010956,1.040219,,,,,,,,,, 2017-05-12,Company,326606,16814,44,0.002617,0.773364,0.000000,0.0,0.000000,0.0,0.773364,0.000000,1.546763,3.093651,52.016652,1.182197,,,,,,,,,, 2017-05-13,Company,326606,16880,32,0.001896,0.770245,0.000000,0.0,0.000000,0.0,0.770245,0.000000,1.540520,3.081195,52.010570,1.625330,,,,,,,,,, 2017-05-14,Company,326606,17045,32,0.001877,0.762742,0.000000,0.0,0.000000,0.0,0.762742,0.000000,1.525543,3.051212,52.007907,1.625247,0,0,0,0,0,0,0,1,52.0079,1.00 2017-05-15,Company,326606,7294,10,0.001371,0.781126,0.000000,0.0,0.000000,0.0,0.781126,0.000000,1.562252,3.124683,22.791441,2.279144,,,,,,,,,, 2017-06-03,Company,559435,30186,42,0.001391,0.690422,0.690422,0.0,0.000000,0.0,0.000000,0.000000,1.380910,2.761773,83.366884,1.984926,0,0,0,1,0,0,0,1,41.6834,2.00 2017-06-04,Company,559435,29952,25,0.000835,0.695962,0.695962,0.0,0.000000,0.0,0.000000,0.000000,1.391983,2.783917,83.383894,3.335356,0,0,0,0,0,0,0,12,6.9487,12.00 2017-06-05,Company,559435,29863,39,0.001306,0.697854,0.697854,0.0,0.000000,0.0,0.000000,0.000000,1.395772,2.791494,83.362371,2.137497,0,0,0,4,2,1,0,15,3.7892,22.00 2017-06-06,Company,559435,29859,40,0.001340,0.698055,0.698055,0.0,0.000000,0.0,0.000000,0.000000,1.396160,2.792283,83.374777,2.084369,0,1,0,4,2,1,0,21,2.8750,29.00 2017-06-07,Company,559435,29968,22,0.000734,0.695653,0.695653,0.0,0.000000,0.0,0.000000,0.000000,1.391359,2.782683,83.391458,3.790521,0,0,0,1,0,0,0,25,3.2074,26.00 2017-06-08,Company,559435,29967,41,0.001368,0.695284,0.695284,0.0,0.000000,0.0,0.000000,0.000000,1.390634,2.781195,83.344075,2.032782,0,0,0,2,1,0,0,23,3.2055,26.00 2017-06-09,Company,559435,14689,8,0.000545,0.686755,0.686755,0.0,0.000000,0.0,0.000000,0.000000,1.373565,2.747041,40.351291,5.043911,0,0,0,0,0,2,0,21,1.7544,23.00 ,,,286395,426,0.001487,0.718754,0.471867,0.0,0.000000,0.0,0.246886,0.000000,1.437560,2.875126,823.421824,1.932915,0,1,0,12,5,4,0,129,5.4531,151.00
And the returned array is but I'd like "date" "clicks" to be used as the keys so I can simply call "$data[Click Lead]":
array(22) {
[0]=>
array(27) {
[0]=>
string(4) "Date"
[1]=>
string(8) "Campaign"
[2]=>
string(11) "Campaign Id"
[3]=>
string(11) "Impressions"
[4]=>
string(6) "Clicks"
[5]=>
string(3) "CTR"
[6]=>
string(13) "Platform Cost"
[7]=>
string(11) "Search Cost"
[8]=>
string(16) "Demographic Cost"
[9]=>
string(15) "Contextual Cost"
[10]=>
string(15) "Behavioral Cost"
[11]=>
string(15) "Geographic Cost"
[12]=>
string(12) "Segment Cost"
[13]=>
string(10) "Media Cost"
[14]=>
string(3) "CPM"
[15]=>
string(11) "Total Spend"
[16]=>
string(4) "ECPC"
[17]=>
string(11) "Click Sales"
[18]=>
string(11) "Click Leads"
[19]=>
string(13) "Click Signups"
[20]=>
string(11) "Click Other"
[21]=>
string(10) "View Sales"
[22]=>
string(10) "View Leads"
[23]=>
string(12) "View Signups"
[24]=>
string(10) "View Other"
[25]=>
string(3) "CPA"
[26]=>
string(16) "Weighted Actions"
}
[1]=>
array(27) {
[0]=>
string(10) "2017-05-16"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[2]=>
array(27) {
[0]=>
string(10) "2017-05-18"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[3]=>
array(27) {
[0]=>
string(10) "2017-05-19"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[4]=>
array(27) {
[0]=>
string(10) "2017-05-20"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[5]=>
array(27) {
[0]=>
string(10) "2017-05-28"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(0) ""
[26]=>
string(4) "1.00"
}
[6]=>
array(27) {
[0]=>
string(10) "2017-06-02"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(0) ""
[26]=>
string(4) "1.00"
}
[7]=>
array(27) {
[0]=>
string(10) "2017-05-10"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "16877"
[4]=>
string(2) "41"
[5]=>
string(8) "0.002429"
[6]=>
string(8) "0.770373"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.770373"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.540785"
[14]=>
string(8) "3.081682"
[15]=>
string(9) "52.009548"
[16]=>
string(8) "1.268526"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[8]=>
array(27) {
[0]=>
string(10) "2017-05-11"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "17001"
[4]=>
string(2) "50"
[5]=>
string(8) "0.002941"
[6]=>
string(8) "0.764775"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.764775"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.529592"
[14]=>
string(8) "3.059288"
[15]=>
string(9) "52.010956"
[16]=>
string(8) "1.040219"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[9]=>
array(27) {
[0]=>
string(10) "2017-05-12"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "16814"
[4]=>
string(2) "44"
[5]=>
string(8) "0.002617"
[6]=>
string(8) "0.773364"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.773364"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.546763"
[14]=>
string(8) "3.093651"
[15]=>
string(9) "52.016652"
[16]=>
string(8) "1.182197"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[10]=>
array(27) {
[0]=>
string(10) "2017-05-13"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "16880"
[4]=>
string(2) "32"
[5]=>
string(8) "0.001896"
[6]=>
string(8) "0.770245"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.770245"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.540520"
[14]=>
string(8) "3.081195"
[15]=>
string(9) "52.010570"
[16]=>
string(8) "1.625330"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[11]=>
array(27) {
[0]=>
string(10) "2017-05-14"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "17045"
[4]=>
string(2) "32"
[5]=>
string(8) "0.001877"
[6]=>
string(8) "0.762742"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.762742"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.525543"
[14]=>
string(8) "3.051212"
[15]=>
string(9) "52.007907"
[16]=>
string(8) "1.625247"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(7) "52.0079"
[26]=>
string(4) "1.00"
}
[12]=>
array(27) {
[0]=>
string(10) "2017-05-15"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(4) "7294"
[4]=>
string(2) "10"
[5]=>
string(8) "0.001371"
[6]=>
string(8) "0.781126"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.781126"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.562252"
[14]=>
string(8) "3.124683"
[15]=>
string(9) "22.791441"
[16]=>
string(8) "2.279144"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[13]=>
array(27) {
[0]=>
string(10) "2017-06-03"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "30186"
[4]=>
string(2) "42"
[5]=>
string(8) "0.001391"
[6]=>
string(8) "0.690422"
[7]=>
string(8) "0.690422"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.380910"
[14]=>
string(8) "2.761773"
[15]=>
string(9) "83.366884"
[16]=>
string(8) "1.984926"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "1"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(7) "41.6834"
[26]=>
string(4) "2.00"
}
[14]=>
array(27) {
[0]=>
string(10) "2017-06-04"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29952"
[4]=>
string(2) "25"
[5]=>
string(8) "0.000835"
[6]=>
string(8) "0.695962"
[7]=>
string(8) "0.695962"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.391983"
[14]=>
string(8) "2.783917"
[15]=>
string(9) "83.383894"
[16]=>
string(8) "3.335356"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(2) "12"
[25]=>
string(6) "6.9487"
[26]=>
string(5) "12.00"
}
[15]=>
array(27) {
[0]=>
string(10) "2017-06-05"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29863"
[4]=>
string(2) "39"
[5]=>
string(8) "0.001306"
[6]=>
string(8) "0.697854"
[7]=>
string(8) "0.697854"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.395772"
[14]=>
string(8) "2.791494"
[15]=>
string(9) "83.362371"
[16]=>
string(8) "2.137497"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "4"
[21]=>
string(1) "2"
[22]=>
string(1) "1"
[23]=>
string(1) "0"
[24]=>
string(2) "15"
[25]=>
string(6) "3.7892"
[26]=>
string(5) "22.00"
}
[16]=>
array(27) {
[0]=>
string(10) "2017-06-06"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29859"
[4]=>
string(2) "40"
[5]=>
string(8) "0.001340"
[6]=>
string(8) "0.698055"
[7]=>
string(8) "0.698055"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.396160"
[14]=>
string(8) "2.792283"
[15]=>
string(9) "83.374777"
[16]=>
string(8) "2.084369"
[17]=>
string(1) "0"
[18]=>
string(1) "1"
[19]=>
string(1) "0"
[20]=>
string(1) "4"
[21]=>
string(1) "2"
[22]=>
string(1) "1"
[23]=>
string(1) "0"
[24]=>
string(2) "21"
[25]=>
string(6) "2.8750"
[26]=>
string(5) "29.00"
}
[17]=>
array(27) {
[0]=>
string(10) "2017-06-07"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29968"
[4]=>
string(2) "22"
[5]=>
string(8) "0.000734"
[6]=>
string(8) "0.695653"
[7]=>
string(8) "0.695653"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.391359"
[14]=>
string(8) "2.782683"
[15]=>
string(9) "83.391458"
[16]=>
string(8) "3.790521"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "1"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(2) "25"
[25]=>
string(6) "3.2074"
[26]=>
string(5) "26.00"
}
[18]=>
array(27) {
[0]=>
string(10) "2017-06-08"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29967"
[4]=>
string(2) "41"
[5]=>
string(8) "0.001368"
[6]=>
string(8) "0.695284"
[7]=>
string(8) "0.695284"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.390634"
[14]=>
string(8) "2.781195"
[15]=>
string(9) "83.344075"
[16]=>
string(8) "2.032782"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "2"
[21]=>
string(1) "1"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(2) "23"
[25]=>
string(6) "3.2055"
[26]=>
string(5) "26.00"
}
[19]=>
array(27) {
[0]=>
string(10) "2017-06-09"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "14689"
[4]=>
string(1) "8"
[5]=>
string(8) "0.000545"
[6]=>
string(8) "0.686755"
[7]=>
string(8) "0.686755"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.373565"
[14]=>
string(8) "2.747041"
[15]=>
string(9) "40.351291"
[16]=>
string(8) "5.043911"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "2"
[23]=>
string(1) "0"
[24]=>
string(2) "21"
[25]=>
string(6) "1.7544"
[26]=>
string(5) "23.00"
}
[20]=>
array(27) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(6) "286395"
[4]=>
string(3) "426"
[5]=>
string(8) "0.001487"
[6]=>
string(8) "0.718754"
[7]=>
string(8) "0.471867"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.246886"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.437560"
[14]=>
string(8) "2.875126"
[15]=>
string(10) "823.421824"
[16]=>
string(8) "1.932915"
[17]=>
string(1) "0"
[18]=>
string(1) "1"
[19]=>
string(1) "0"
[20]=>
string(2) "12"
[21]=>
string(1) "5"
[22]=>
string(1) "4"
[23]=>
string(1) "0"
[24]=>
string(3) "129"
[25]=>
string(6) "5.4531"
[26]=>
string(6) "151.00"
}
[21]=>
array(1) {
[0]=>
NULL
}
}
Use array_combine and extend your code to:
$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $result));
$new_data = [];
// shift first element of $data, we will use it as keys.
$keys = array_shift($data);
foreach ($data as $item) {
// create key => value array
$new_data[] = array_combine($keys, $item);
}
Thanks to #Kazz - in case of warnings like
array_combine(): Both parameters should have an equal number of elements
you need to check if both elements are arrays and theirs sizes equals (with count(), for example).

Do action when for loop is at it's last index - 1

I have an function which retrieves specific data from some other functions.
I loop through the retrieved data and explode every result by the # character, the data looks like this:
Not exploded
string(14) "1#28#92#28#103"
string(17) "551#36#112#36#126"
string(17) "651#45#132#45#150"
string(17) "751#59#159#59#179"
string(17) "851#77#194#91#208"
string(18) "951#99#229#129#245"
string(20) "1051#122#264#166#282"
string(20) "1151#145#300#203#318"
string(20) "1251#168#335#240#355"
string(20) "1351#190#371#278#392"
string(20) "1451#213#406#315#428"
string(20) "1551#236#441#352#465"
string(20) "1651#258#477#389#501"
string(20) "1751#281#512#427#538"
string(20) "1851#304#548#464#575"
string(20) "1951#327#583#501#611"
string(20) "2051#349#618#539#648"
string(20) "2151#372#654#576#685"
string(20) "2251#395#689#613#721"
string(20) "2351#417#725#650#758"
string(20) "2451#440#760#688#795"
string(20) "2551#463#795#725#831"
string(20) "2651#486#831#762#868"
string(20) "2751#508#866#799#905"
string(20) "2851#531#902#837#941"
string(20) "2951#554#937#874#978"
string(21) "3051#576#972#911#1015"
string(22) "3151#599#1008#949#1051"
string(22) "3251#618#1040#982#1084"
string(23) "3351#637#1071#1015#1117"
string(23) "3451#655#1102#1049#1149"
string(23) "3551#674#1133#1082#1182"
string(23) "3651#693#1165#1115#1214"
string(23) "3751#711#1196#1148#1247"
string(23) "3851#730#1227#1181#1279"
string(23) "3951#748#1258#1214#1312"
string(23) "4051#767#1290#1247#1344"
string(23) "4151#785#1321#1280#1377"
string(23) "4251#804#1352#1314#1410"
string(23) "4351#823#1383#1347#1442"
string(23) "4451#841#1415#1380#1475"
string(23) "4551#860#1446#1413#1507"
string(23) "4651#878#1477#1446#1540"
string(23) "4751#897#1508#1479#1572"
string(23) "4851#915#1540#1512#1605"
string(23) "4951#934#1571#1546#1637"
Exploded
array(5) {
[0]=>
string(1) "1"
[1]=>
string(2) "28"
[2]=>
string(2) "92"
[3]=>
string(2) "28"
[4]=>
string(3) "103"
}
array(5) {
[0]=>
string(3) "551"
[1]=>
string(2) "36"
[2]=>
string(3) "112"
[3]=>
string(2) "36"
[4]=>
string(3) "126"
}
array(5) {
[0]=>
string(3) "651"
[1]=>
string(2) "45"
[2]=>
string(3) "132"
[3]=>
string(2) "45"
[4]=>
string(3) "150"
}
array(5) {
[0]=>
string(3) "751"
[1]=>
string(2) "59"
[2]=>
string(3) "159"
[3]=>
string(2) "59"
[4]=>
string(3) "179"
}
array(5) {
[0]=>
string(3) "851"
[1]=>
string(2) "77"
[2]=>
string(3) "194"
[3]=>
string(2) "91"
[4]=>
string(3) "208"
}
array(5) {
[0]=>
string(3) "951"
[1]=>
string(2) "99"
[2]=>
string(3) "229"
[3]=>
string(3) "129"
[4]=>
string(3) "245"
}
array(5) {
[0]=>
string(4) "1051"
[1]=>
string(3) "122"
[2]=>
string(3) "264"
[3]=>
string(3) "166"
[4]=>
string(3) "282"
}
array(5) {
[0]=>
string(4) "1151"
[1]=>
string(3) "145"
[2]=>
string(3) "300"
[3]=>
string(3) "203"
[4]=>
string(3) "318"
}
array(5) {
[0]=>
string(4) "1251"
[1]=>
string(3) "168"
[2]=>
string(3) "335"
[3]=>
string(3) "240"
[4]=>
string(3) "355"
}
array(5) {
[0]=>
string(4) "1351"
[1]=>
string(3) "190"
[2]=>
string(3) "371"
[3]=>
string(3) "278"
[4]=>
string(3) "392"
}
array(5) {
[0]=>
string(4) "1451"
[1]=>
string(3) "213"
[2]=>
string(3) "406"
[3]=>
string(3) "315"
[4]=>
string(3) "428"
}
array(5) {
[0]=>
string(4) "1551"
[1]=>
string(3) "236"
[2]=>
string(3) "441"
[3]=>
string(3) "352"
[4]=>
string(3) "465"
}
array(5) {
[0]=>
string(4) "1651"
[1]=>
string(3) "258"
[2]=>
string(3) "477"
[3]=>
string(3) "389"
[4]=>
string(3) "501"
}
array(5) {
[0]=>
string(4) "1751"
[1]=>
string(3) "281"
[2]=>
string(3) "512"
[3]=>
string(3) "427"
[4]=>
string(3) "538"
}
array(5) {
[0]=>
string(4) "1851"
[1]=>
string(3) "304"
[2]=>
string(3) "548"
[3]=>
string(3) "464"
[4]=>
string(3) "575"
}
array(5) {
[0]=>
string(4) "1951"
[1]=>
string(3) "327"
[2]=>
string(3) "583"
[3]=>
string(3) "501"
[4]=>
string(3) "611"
}
array(5) {
[0]=>
string(4) "2051"
[1]=>
string(3) "349"
[2]=>
string(3) "618"
[3]=>
string(3) "539"
[4]=>
string(3) "648"
}
array(5) {
[0]=>
string(4) "2151"
[1]=>
string(3) "372"
[2]=>
string(3) "654"
[3]=>
string(3) "576"
[4]=>
string(3) "685"
}
array(5) {
[0]=>
string(4) "2251"
[1]=>
string(3) "395"
[2]=>
string(3) "689"
[3]=>
string(3) "613"
[4]=>
string(3) "721"
}
array(5) {
[0]=>
string(4) "2351"
[1]=>
string(3) "417"
[2]=>
string(3) "725"
[3]=>
string(3) "650"
[4]=>
string(3) "758"
}
array(5) {
[0]=>
string(4) "2451"
[1]=>
string(3) "440"
[2]=>
string(3) "760"
[3]=>
string(3) "688"
[4]=>
string(3) "795"
}
array(5) {
[0]=>
string(4) "2551"
[1]=>
string(3) "463"
[2]=>
string(3) "795"
[3]=>
string(3) "725"
[4]=>
string(3) "831"
}
array(5) {
[0]=>
string(4) "2651"
[1]=>
string(3) "486"
[2]=>
string(3) "831"
[3]=>
string(3) "762"
[4]=>
string(3) "868"
}
array(5) {
[0]=>
string(4) "2751"
[1]=>
string(3) "508"
[2]=>
string(3) "866"
[3]=>
string(3) "799"
[4]=>
string(3) "905"
}
array(5) {
[0]=>
string(4) "2851"
[1]=>
string(3) "531"
[2]=>
string(3) "902"
[3]=>
string(3) "837"
[4]=>
string(3) "941"
}
array(5) {
[0]=>
string(4) "2951"
[1]=>
string(3) "554"
[2]=>
string(3) "937"
[3]=>
string(3) "874"
[4]=>
string(3) "978"
}
array(5) {
[0]=>
string(4) "3051"
[1]=>
string(3) "576"
[2]=>
string(3) "972"
[3]=>
string(3) "911"
[4]=>
string(4) "1015"
}
array(5) {
[0]=>
string(4) "3151"
[1]=>
string(3) "599"
[2]=>
string(4) "1008"
[3]=>
string(3) "949"
[4]=>
string(4) "1051"
}
array(5) {
[0]=>
string(4) "3251"
[1]=>
string(3) "618"
[2]=>
string(4) "1040"
[3]=>
string(3) "982"
[4]=>
string(4) "1084"
}
array(5) {
[0]=>
string(4) "3351"
[1]=>
string(3) "637"
[2]=>
string(4) "1071"
[3]=>
string(4) "1015"
[4]=>
string(4) "1117"
}
array(5) {
[0]=>
string(4) "3451"
[1]=>
string(3) "655"
[2]=>
string(4) "1102"
[3]=>
string(4) "1049"
[4]=>
string(4) "1149"
}
array(5) {
[0]=>
string(4) "3551"
[1]=>
string(3) "674"
[2]=>
string(4) "1133"
[3]=>
string(4) "1082"
[4]=>
string(4) "1182"
}
array(5) {
[0]=>
string(4) "3651"
[1]=>
string(3) "693"
[2]=>
string(4) "1165"
[3]=>
string(4) "1115"
[4]=>
string(4) "1214"
}
array(5) {
[0]=>
string(4) "3751"
[1]=>
string(3) "711"
[2]=>
string(4) "1196"
[3]=>
string(4) "1148"
[4]=>
string(4) "1247"
}
array(5) {
[0]=>
string(4) "3851"
[1]=>
string(3) "730"
[2]=>
string(4) "1227"
[3]=>
string(4) "1181"
[4]=>
string(4) "1279"
}
array(5) {
[0]=>
string(4) "3951"
[1]=>
string(3) "748"
[2]=>
string(4) "1258"
[3]=>
string(4) "1214"
[4]=>
string(4) "1312"
}
array(5) {
[0]=>
string(4) "4051"
[1]=>
string(3) "767"
[2]=>
string(4) "1290"
[3]=>
string(4) "1247"
[4]=>
string(4) "1344"
}
array(5) {
[0]=>
string(4) "4151"
[1]=>
string(3) "785"
[2]=>
string(4) "1321"
[3]=>
string(4) "1280"
[4]=>
string(4) "1377"
}
array(5) {
[0]=>
string(4) "4251"
[1]=>
string(3) "804"
[2]=>
string(4) "1352"
[3]=>
string(4) "1314"
[4]=>
string(4) "1410"
}
array(5) {
[0]=>
string(4) "4351"
[1]=>
string(3) "823"
[2]=>
string(4) "1383"
[3]=>
string(4) "1347"
[4]=>
string(4) "1442"
}
array(5) {
[0]=>
string(4) "4451"
[1]=>
string(3) "841"
[2]=>
string(4) "1415"
[3]=>
string(4) "1380"
[4]=>
string(4) "1475"
}
array(5) {
[0]=>
string(4) "4551"
[1]=>
string(3) "860"
[2]=>
string(4) "1446"
[3]=>
string(4) "1413"
[4]=>
string(4) "1507"
}
array(5) {
[0]=>
string(4) "4651"
[1]=>
string(3) "878"
[2]=>
string(4) "1477"
[3]=>
string(4) "1446"
[4]=>
string(4) "1540"
}
array(5) {
[0]=>
string(4) "4751"
[1]=>
string(3) "897"
[2]=>
string(4) "1508"
[3]=>
string(4) "1479"
[4]=>
string(4) "1572"
}
array(5) {
[0]=>
string(4) "4851"
[1]=>
string(3) "915"
[2]=>
string(4) "1540"
[3]=>
string(4) "1512"
[4]=>
string(4) "1605"
}
array(5) {
[0]=>
string(4) "4951"
[1]=>
string(3) "934"
[2]=>
string(4) "1571"
[3]=>
string(4) "1546"
[4]=>
string(4) "1637"
}
I want it so whenever the last index - 1 is being handled by the for loop, the code should do a specific action, this is my own try (function) for it:
public function getDataByVehicle(VehicleType $vehicleType, VehicleOwner $vehicleOwner) {
$data = $vehicleType->resolveVehicleTypeWithData($vehicleType);
$dataKey = $vehicleType->resolveDataKey($data, $vehicleOwner);
$dataLocation = $this->data->$dataKey;
if (is_a($vehicleType, "PassengerCar")) {
for ($dataLocationIndex = 0; $dataLocationIndex < count($dataLocation); $dataLocationIndex++) {
$formattedData = explode("#", $dataLocation[$dataLocationIndex]);
var_dump($formattedData);
//My own try!
if (!$dataLocationIndex == count($dataLocation)) {
// do action
}
}
}
return null;
}
Question
How do I make my code do an specific action when the for loop is at its last index - 1?
Note
I want to use an for loop because i want to be able to check what the next index of the array would be, if it's not exists, take the last one it remembered.
The for-loop is only running while: $dataLocationIndex < count($dataLocation), which means you should check the count($dataLocation) minus 1, because the loop never reaches the count of $dataLocation.
//My own try!
if ($dataLocationIndex === count($dataLocation)-1) {
// do action
}
Hope it worked.

Categories