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));
Related
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.
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"
}
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
I have an array of data that contains the alphabetically sorted data, but in view I have different sections. Each alphabet represents each section.
I need to list the data in such order that section with A must contain all the names starting with A, section B must contain the names sarting with B and so on.
My work out
I have made an array that gives me the data separates the data with there starting alphabets
foreach ($data['stores_cat_data'] as $store ) {
$store_title = $store['title']."<br />";
$curr = current(str_split($store_title));
if(!preg_match("/^[a-zA-Z]$/", $curr))
{
$storeArray['0-9'][$store_title] = $store;
}
else{
$storeArray[$curr][$store_title] = $store;
}
}
EDITED
my output is
array(46) {
["0-9"]=>
array(21) {
["1 800 Lighting
"]=>
array(7) {
["title"]=>
string(14) "1 800 Lighting"
["cCommisions_percentage"]=>
string(5) "8.00%"
["url"]=>
string(49) "http://www.gopjn.com/t/S0BHSUVJQERDREpHSUBHS0xKRg"
["id"]=>
string(3) "539"
["logo"]=>
string(14) "1389213829.gif"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["1-800 CONTACTS
"]=>
array(7) {
["title"]=>
string(14) "1-800 CONTACTS"
["cCommisions_percentage"]=>
string(73) "2.4%(Repeat Customer Repeat Sale) and 7.2%(New Customer First Time Sale) "
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.274994&fot=9999&foc=1"
["id"]=>
string(3) "598"
["logo"]=>
string(14) "1389289817.jpg"
["publisher_id"]=>
string(1) "4"
["subid"]=>
string(13) "&fobs={subid}"
}
["1-800-Bakery
"]=>
array(7) {
["title"]=>
string(13) "1-800-Bakery "
["cCommisions_percentage"]=>
string(30) "10.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=266793&type=3&subid=0"
["id"]=>
string(2) "22"
["logo"]=>
string(14) "1387403703.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["1-800-BASKETS.COM
"]=>
array(7) {
["title"]=>
string(17) "1-800-BASKETS.COM"
["cCommisions_percentage"]=>
string(5) "8.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.216994&fot=9999&foc=1"
["id"]=>
string(3) "599"
["logo"]=>
string(14) "1389289896.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["1-800-FLOWERS.COM
"]=>
array(7) {
["title"]=>
string(17) "1-800-FLOWERS.COM"
["cCommisions_percentage"]=>
string(6) "10.40%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.225130&fot=9999&foc=1"
["id"]=>
string(4) "1659"
["logo"]=>
string(14) "1390516089.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["1-800-PetMeds
"]=>
array(7) {
["title"]=>
string(13) "1-800-PetMeds"
["cCommisions_percentage"]=>
string(42) "12.00%-16.00% commission on selected goods"
["url"]=>
string(85) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=56753&type=3&subid=0"
["id"]=>
string(2) "24"
["logo"]=>
string(14) "1387403850.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["100 Percent Pure
"]=>
array(7) {
["title"]=>
string(16) "100 Percent Pure"
["cCommisions_percentage"]=>
string(29) "8.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=270135&type=3&subid=0"
["id"]=>
string(2) "25"
["logo"]=>
string(14) "1387404086.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["123Inkjets.com
"]=>
array(7) {
["title"]=>
string(15) "123Inkjets.com "
["cCommisions_percentage"]=>
string(41) "25.6% and 4%(OEM and Office Supply items)"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.217798&fot=9999&foc=1"
["id"]=>
string(3) "596"
["logo"]=>
string(14) "1389289585.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["123Print
"]=>
array(7) {
["title"]=>
string(8) "123Print"
["cCommisions_percentage"]=>
string(5) "6.40%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.276533&fot=9999&foc=1"
["id"]=>
string(3) "597"
["logo"]=>
string(14) "1389289736.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["191 Unlimited
"]=>
array(7) {
["title"]=>
string(13) "191 Unlimited"
["cCommisions_percentage"]=>
string(5) "5.00%"
["url"]=>
string(49) "http://www.pntrs.com/t/S0BLS0ZGQERDREpHSUBKS0ZGSg"
["id"]=>
string(3) "540"
["logo"]=>
string(14) "1389214452.png"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["2020ave.com
"]=>
array(7) {
["title"]=>
string(11) "2020ave.com"
["cCommisions_percentage"]=>
string(6) "10.00%"
["url"]=>
string(50) "http://www.pjtra.com/t/TEFNSkZIQUVERUtISkFFREZHSks"
["id"]=>
string(3) "542"
["logo"]=>
string(14) "1389214732.png"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["24 Hour Fitness
"]=>
array(7) {
["title"]=>
string(15) "24 Hour Fitness"
["cCommisions_percentage"]=>
string(8) "4% to 8%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.256270&fot=9999&foc=1"
["id"]=>
string(3) "601"
["logo"]=>
string(14) "1389290288.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["2bstores.com
"]=>
array(7) {
["title"]=>
string(12) "2bstores.com"
["cCommisions_percentage"]=>
string(5) "5.00%"
["url"]=>
string(51) "http://www.pntrac.com/t/TEFNSUZMQUVERUtISkFFRERKR0s"
["id"]=>
string(3) "543"
["logo"]=>
string(14) "1389214901.png"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["360training
"]=>
array(7) {
["title"]=>
string(12) "360training "
["cCommisions_percentage"]=>
string(6) "12.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.737181&fot=9999&foc=1"
["id"]=>
string(3) "602"
["logo"]=>
string(14) "1389290350.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4 All Memory
"]=>
array(7) {
["title"]=>
string(12) "4 All Memory"
["cCommisions_percentage"]=>
string(29) "8.00% commission on all goods"
["url"]=>
string(85) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=99169&type=3&subid=0"
["id"]=>
string(2) "28"
["logo"]=>
string(14) "1387404383.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4 Wheel Drive Hardware
"]=>
array(7) {
["title"]=>
string(22) "4 Wheel Drive Hardware"
["cCommisions_percentage"]=>
string(5) "5.60%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.730591&fot=9999&foc=1"
["id"]=>
string(3) "603"
["logo"]=>
string(14) "1389290418.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4 Wheel Parts
"]=>
array(7) {
["title"]=>
string(14) "4 Wheel Parts "
["cCommisions_percentage"]=>
string(5) "4.80%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.730526&fot=9999&foc=1"
["id"]=>
string(3) "604"
["logo"]=>
string(14) "1389290467.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4imprint
"]=>
array(7) {
["title"]=>
string(8) "4imprint"
["cCommisions_percentage"]=>
string(5) "3.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.161896&fot=9999&foc=1"
["id"]=>
string(3) "605"
["logo"]=>
string(14) "1389290519.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["525 America
"]=>
array(7) {
["title"]=>
string(11) "525 America"
["cCommisions_percentage"]=>
string(5) "6.00%"
["url"]=>
string(49) "http://www.pntra.com/t/S0BLSkZKQERDREpHSUBKQ0dFRw"
["id"]=>
string(3) "544"
["logo"]=>
string(14) "1389215113.jpg"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["60 Minute Payday
"]=>
array(7) {
["title"]=>
string(17) "60 Minute Payday "
["cCommisions_percentage"]=>
string(4) "$48 "
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.164179&fot=9999&foc=1"
["id"]=>
string(3) "606"
["logo"]=>
string(14) "1389290591.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["7 For All Mankind
"]=>
array(7) {
["title"]=>
string(17) "7 For All Mankind"
["cCommisions_percentage"]=>
string(29) "4.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=256004&type=3&subid=0"
["id"]=>
string(2) "29"
["logo"]=>
string(14) "1387404461.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
}
["A"]=>
array(83) {
["A.N.S.I
"]=>
array(7) {
["title"]=>
string(7) "A.N.S.I"
["cCommisions_percentage"]=>
string(30) "10.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=208750&type=3&subid=0"
["id"]=>
string(2) "48"
["logo"]=>
string(14) "1387408300.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["ABC Distributing LLC
"]=>
array(7) {
["title"]=>
string(20) "ABC Distributing LLC"
["cCommisions_percentage"]=>
string(69) "4.00% commission on all goods; [2.00% commission on selected goods]; "
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=238767&type=3&subid=0"
["id"]=>
string(2) "30"
["logo"]=>
string(14) "1387404636.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["ABCmouse.com
"]=>
array(7) {
["title"]=>
string(12) "ABCmouse.com"
["cCommisions_percentage"]=>
string(27) "$4.00 flat fee on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=256295&type=3&subid=0"
["id"]=>
string(2) "31"
["logo"]=>
string(14) "1387405955.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["Abe's of Maine
"]=>
array(7) {
["title"]=>
string(15) "Abe's of Maine "
["cCommisions_percentage"]=>
string(5) "4.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.290656&fot=9999&foc=1"
["id"]=>
string(3) "607"
["logo"]=>
string(14) "1389290715.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["Abhair
"]=>
array(7) {
["title"]=>
string(6) "Abhair"
["cCommisions_percentage"]=>
string(37) "12.00%-17.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=280480&type=3&subid=0"
["id"]=>
string(2) "33"
["logo"]=>
string(14) "1387406425.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["About Airport Parking
"]=>
array(7) {
["title"]=>
string(21) "About Airport Parking"
["cCommisions_percentage"]=>
string(6) "50.00%"
["url"]=>
string(49) "http://www.pntrs.com/t/S0BMRUVFQERDREpHSUBMRUpHSA"
["id"]=>
string(3) "560"
["logo"]=>
string(14) "1389224403.jpg"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["Absolute LoJack
"]=>
array(7) {
["title"]=>
string(16) "Absolute LoJack "
["cCommisions_percentage"]=>
string(73) "$1.00 flat fee on selected goods; [15.00% commission on selected goods]; "
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=274953&type=3&subid=0"
["id"]=>
string(2) "34"
["logo"]=>
string(14) "1387406520.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["Abt.com
"]=>
array(7) {
["title"]=>
string(7) "Abt.com"
["cCommisions_percentage"]=>
string(5) "3.00%"
["url"]=>
string(50) "http://www.pjatr.com/t/TEFNSUtIQUVERUtISkFFREVIREc"
["id"]=>
string(3) "547"
["logo"]=>
string(14) "1389215949.jpg"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["Accessorize
"]=>
array(7) {
["title"]=>
string(11) "Accessorize"
["cCommisions_percentage"]=>
string(29) "5.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=215113&type=3&subid=0"
["id"]=>
string(4) "1660"
["logo"]=>
string(14) "1390518973.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["AccessoryGeeks.com
"]=>
array(7) {
["title"]=>
string(19) "AccessoryGeeks.com "
["cCommisions_percentage"]=>
string(6) "13.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.227987&fot=9999&foc=1"
["id"]=>
string(3) "608"
["logo"]=>
string(14) "1389291008.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
This should do it:
$storeArray = array();
foreach ($data['stores_cat_data'] as $store) {
$title = $store['title'];
$first_char = substr($title, 0, 1);
if (preg_match("/^[0-9]$/", $first_char)) {
$key = "0-9";
} else {
$key = strtoupper($first_char);
}
if (!isset($storeArray[$key])) {
$storeArray[$key] = array();
}
$storeArray[$key][$title] = $store;
}
var_dump($storeArray);
Your code looks almost correct. The way you determine $curr is a little strange and you have to account for uppercase/lowercase. Also, to avoid array warnings make sure you initialize each letter of the alphabet, as you go.
You might also want to remove all non-alphanumeric characters, like " and - that might mess up the detection. A title of "Best" book shop will be put under 0-9 where B would be better.
A regexp like: preg_replace("/[^a-zA-Z0-9]/", "", $str) should do it.
Here is a sample var_dump. Now, how do I know how to construct a foreach from it to load any particular word or fragment in to an array?
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
Before we can decipher it, we have to format it.
Tobias Kun's answer shows a very good way to format the var_dump output so you can read it.
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
You have a stdClass object with a property called ,"noun". noun is an abject with a property called "syn", which is an array of strings.
Suppose we call the object $object. Then we can access the array like:
echo $object->noun->syn[23];
which gives us canine. So a loop might look like:
foreach($data->noun->syn as $value) {
echo $value . "<br>\n";
}
First of all you should really increase the quality of your questions. The code is not formatted at all.
If you use echo "<pre>" . print_r($your_data_object_or_array,1) . "</pre>" your data will be formatted fine.
If i understand you right this should help you:
foreach($data['noun']['syn'] as $value) {
//with this you loop through all your words in "syn" e.g. domestic, "Canis familiaris etc."
echo $value . "<br>";
}
//Ouput:
domestic
Canis familiaris
frump
cad
etc ...