PHP array keys to a string - php

Hi I have an array that looks something like this
Array ( [0] => T [1] => T [2] => T [3] => T [4] => T [5] => T [6] => T [7] => T [8] => T [9] => T [10] => T [11] => T [12] => T [13] => T [14] => T [15] => T [16] => T [17] => T [18] => T [19] => T [20] => T [21] => T [22] => T [23] => T [24] => T [25] => T [26] => T [27] => T [28] => T [29] => T [30] => T [31] => T [32] => T [33] => T [34] => T [35] => T [36] => T [37] => T [38] => T [39] => T [40] => T [41] => T [42] => T [43] => T [44] => T [45] => T [46] => T [47] => T [48] => T [49] => T [50] => T [51] => T [52] => T [53] => T [54] => T [55] => T [56] => T [57] => T [58] => T [59] => T [60] => T [61] => T [62] => T [63] => T [64] => T [65] => T [66] => T [67] => T [68] => T [69] => T [70] => T [71] => T [72] => 0 [73] => 0 [74] => 0 [75] => 0 [76] => 0 [77] => 0 [78] => 0 [79] => 0 [80] => 0 [81] => 0 [82] => 0 [83] => 0 [84] => 0 [85] => 0 [86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 0 [97] => 0 [98] => 0 [99] => 0 )
What I need to do is group keys [0]-[49], [50]-[99], and [100]-[149] into three string variables so tht they can be inserted into the database fields 'answ1-50', 'answ51-100', and 'answ101-150. Im a little bit lost on this one as I am still somewhat of a php novice. How do i break up this array into three groups as a string?

While not good practice, the answer to your posted question is to:
list($group1, $group2, $group3) = array_map("implode", array_chunk($arr, 50));
(assumes that you always have 150 answers. if you don't, skip the list and you will have an array of results $groups[0], $groups[1], $groups[2])

I'm not sure why you want to do this, but this should do the trick.
//assuming content in $myarray
$newarr = array();
for($i = 0; $i < count($myarray); $i++ {
$j = floor($i / 50);
if($j == $i / 50) {
$newarr[$j] = "";
}
$newarr[$j] += (string)$myarray[$i];
}
Each element of $newarr should be a string with 50 answers in it.

You can just simply write it under the loop and concatenate the values to a string.
for($i = 0; $i < count($yourarray); $i++) {
if(i>=0 && $i<50)
{
$str1=$str1.$yourarray[i];
}
else if(i>=50 && $i<100)
{
$str2=$str2.$yourarray[i];
}
else if(i>=100 && $i<150)
{
$str3=$str3.$yourarray[i];
}}

Related

Remove consecutive occurrences of string "start" from array PHP ARRAY

I have the following array
Array ( [0] => **start** [1] => **start** [2] => name [3] => producer [4] => contact name [5] => 03354222271 [6] => fzahid001#gmail.com [7] => day contact name [8] => 03354222271 [9] => venue name [10] => adress [11] => country [12] => city [13] => desc [14] => file [15] => 2016-01-01 [16] => 01:00 [17] => 06:00 [18] => 2 [19] => music_festival [20] => 2000+ [21] => quick [22] => alcohol [23] => quick [24] => 10x10 [25] => ***no*** [26] => ***no*** [27] => ***no*** [28] => 2 [29] => 0 [30] => 4 [31] => $158 [32] => $118.5 [33] => $284 [34] => $960 [35] => **start** [36] => na [37] => producer [38] => con [39] => 1 [40] => fzahid001#gmail.com [41] => nam [42] => 1 [43] => venue [44] => ad [45] => co [46] => ci [47] => description test [48] => download555ssss.png [49] => 2016-12-07 [50] => 13:00 [51] => 19:00 [52] => 2 [53] => manual_selection [54] => ATV [55] => 10x10 [56] => no [57] => no [58] => no [59] => 1 [60] => 1 [61] => 1 [62] => $109.5 [63] => $118.5 [64] => $99.5 [65] => $728 [66] => **start** [67] => Race [68] => Race Club [69] => Faizan [70] => 03354222271 [71] => fzahid001#gmail.com [72] => Faizan Zahid [73] => 03354222271 [74] => DHA [75] => 90-H Tariq Gardens [76] => Pakistan [77] => Lahore [78] => [79] => images.jpg [80] => 2017-01-01 [81] => 00:00 [82] => 07:00 [83] => 2 [84] => cycling_road_race [85] => 1_field [86] => 1_399 [87] => electronic [88] => quick [89] => 10x10 [90] => no [91] => no [92] => no [93] => 1 [94] => 0 [95] => 1 [96] => $109.5 [97] => $118.5 [98] => $99.5 [99] => $796.5 [100] => )
Now what I want to do is, remove the consecutive duplicates of "start". I am currently using following code
foreach ($updateddata as $value){
if($value != $previousvalue){
array_push($finaldata, $value);
}
$previousvalue = $value;
}
but is also removes other consecutive duplicates as well which I don't want to remove. Kindly help me how to do this.
I have highlighted the occurrences of "start" which I want to remove and make it one single entry, while leaving the other string as it is e.g. I don't want to remove the duplicates of string "no"
Try this
if($value != $previousvalue){
array_push($finaldata, $value);
} else if( $value != 'start'){
array_push($finaldata, $value);
}
To remove the consecutive duplicates of "start" value you should restrict the crucial condition to that value. Use the following approach:
// exemplary array
$updateddata = [
'start', 'start', 'producer', 'contact', 'no', 'no'
];
foreach ($updateddata as $k => $v) {
if (isset($updateddata[$k-1]) && $v == 'start' && $v == $updateddata[$k-1]) {
unset($updateddata[$k]);
}
}
The output:
Array
(
[0] => start
[2] => producer
[3] => contact
[4] => no
[5] => no
)

How to delete the last element of an array if it is empty

Below is the print_r version of my array ($hamle). As you can see index(80) is empty or there is a end of line or a similar character there. I tried many options to delete it if it exists and is empty but couldn't do it. I strongly think that it is reading an empty line during the 'read while' loop. It doesn't appear in all files. It appears sometimes. Can anyone think of a chic way out?
Array ( [0] => 1.b4 [1] => d5 [2] => 2.Bb2 [3] => Qd6 [4] => 3.a3 [5] => e5 [6] => 4.e3 [7] => a5 [8] => 5.b5 [9] => Nf6 [10] => 6.c4 [11] => Bg4 [12] => 7.Be2 [13] => Bxe2 [14] => 8.Qxe2 [15] => Nbd7 [16] => 9.d4 [17] => dxc4 [18] => 10.Nf3 [19] => e4 [20] => 11.Ne5 [21] => Nb6 [22] => 12.Nd2 [23] => Qe6 [24] => 13.O-O [25] => Bd6 [26] => 14.Nexc4 [27] => Nxc4 [28] => 15.Nxc4 [29] => O-O [30] => 16.Rfc1 [31] => Be7 [32] => 17.a4 [33] => Nd5 [34] => 18.Ba3 [35] => Bb4 [36] => 19.Qb2 [37] => Rfc8 [38] => 20.Bxb4 [39] => axb4 [40] => 21.Nd2 [41] => c6 [42] => 22.b6 [43] => Qe7 [44] => 23.Rc5 [45] => Ra6 [46] => 24.a5 [47] => g6 [48] => 25.Nb3 [49] => Rca8 [50] => 26.Rac1 [51] => Kg7 [52] => 27.Qd2 [53] => Qd6 [54] => 28.R1c4 [55] => Rd8 [56] => 29.Rxb4 [57] => Nxb4 [58] => 30.Qxb4 [59] => Qe7 [60] => 31.h3 [61] => Rd5 [62] => 32.Kf1 [63] => Ra8 [64] => 33.Qa4 [65] => Qd6 [66] => 34.Ke2 [67] => Kf8 [68] => 35.Rxd5 [69] => cxd5 [70] => 36.Nc5 [71] => Ke7 [72] => 37.Qb5 [73] => Rb8 [74] => 38.Kd2 [75] => f5 [76] => 39.Kc3 [77] => g5 [78] => 40.Kb4 [79] => 1-0 [80] => )
The code =>
while(! feof($file))
{
$n=$n+1;
$hamle1= fgets($file);
$hamle1 = str_replace("\n", "", $hamle1);
$hamle1 = str_replace("\r", "", $hamle1);
$hamle1 = trim($hamle1);
$hamle1 = explode(" ", $hamle1);
foreach($hamle1 as $item)
{
$hamle[] = $item;
}
array_filter will remove empty entries
$hamle = array_filter($hamle);
To delete the last element of array if it's empty then use below code.
// get the last element of array
if(trim(end($hamle)) == '')
{
// Remove last element from array
array_pop($hamle) ;
}
Kindly visit below links to understand the functions which are used in above code.
trim
end
array_pop

split big array into multiple arrays where empty element occurs in php

This is Input:-print_r($result4);
Output:-Array ( [0] => A-I-only [1] => B-III-only [2] => C-I-and-II-only [3]=> D-II-and-III-only [4] => E-I,-II,-III [5] => [6] => A-Hepatitis-A [7] => B-Hepatitis-B [8] => C-Hepatitis-C [9] => D-Hepatitis-B-and-C [10] => E-None-of-the-above [11] => [12] => A)-Cholestasis [13] => B)-Cholecystitis [14] => C)-Cholelithiasis [15] => D)-Hepatic-encephalopathy [16] => E)-Ascites [17] => [18] => A-Acetyl-salicylic-acid [19] => B-Ibuprofen [20] => C-Acetaminophen [21] => D-Pepto-Bismol [22] => E-All-of-the-above [23] => [24] => [25] => A-dark-urine [26] => B-stomach-pain [27] => C-blood-in-stools [28] => D-yellowing-of-skin [29] => E-Yellowing-of-eye-and-mucus [30] => [31] => A-Hepatitis-A [32] => B-Hepatitis-B [33] => C-Dukoral [34] => D-Gerdasil [35] => E-None-of-the-above [36] => [37] => cervical-cancer-caused-by-papilloma-virus [38] => A-Glucuronidation [39] => B-Glutathione-conjugation [40] => C-Acetylation [41] => D-Sulfate-conjugation [42] => E-Methylation [43] => [44] => A-Acetylcysteine [45] => B-cysteine [46] => C-Mercapturic-acid [47] => D-Glutathione-conjugation [48] => E-Glutathione-only [49] => [50] => administered-in-teenagers-to-prevent-cervical-cancer. [51] => A-Hepatitis-A [52] => B-Hepatitis-C [53] => C-Hepatitis-D [54] => D-Hepatitis-A,-B-&-C [55] => E-Hepatitis-A,-B-&-D [56] => [57] => [58] => A)-ALT [59] => B)-AST [60] => C)-Bilirubin- [61] => D)-Albumin [62] => E)-Proteins [63] => [64] => A--Saliva- [65] => B-Bile- [66] => C-Pancreatic-duct---- [67] => D-Gastric-secretions [68] => [69] => A)-Sexual-contact [70] => B)-Blood-transfusion [71] => C-Food-and-drink-contamination [72] => D-Traveling-abroad [73] => E-Drugs [74] => [75] => A)-Hepatitis-A-only [76] => B)-Hepatitis-B-only [77] => C)-Hepatitis-A-and-B [78] => D)-Hepatitis-A,-B-and-C [79] => E)-Hepatitis-B-and-C [80] => [81] => [82] => A)-Proton-pump-inhibitors [83] => B)-Warfarin [84] => C)-antacids [85] => D)Lipid-soluble-drugs [86] => E)-Parenteral-drugs [87] => [88] => A)-spironolactone- [89] => B)-NSAIDs- [90] => C)-acetaminophen- [91] => D)-Ibuprofen- [92] => E)-codeine [93] => [94] => A)-constipation- [95] => B)-ascites- [96] => C)-encephalitis- [97] => D)-liver-cirrhosis- [98] => E)-Hepatitis [99] => [100] => encephalitis. [101] => contaminated-hepatitis? [102] => A)-Hepatitis-A [103] => B)-Hepatitis-B [104] => C)-Hepatitis-A-and-B [105] => D)-Hepatitis-C [106] => E)-Hepatitis-D [107] => [108] => A)-Hepatitis-A [109] => [110] => B)-Hepatitis-A-and-B [111] => C)-Hepatitis-B-and-C [112] => D)-Hepatitis-A,-B,-C [113] => [114] => [115] => )
now i want to split array into multiple arrays where empty element occurs like
Array ( [0] => A-I-only [1] => B-III-only [2] => C-I-and-II-only [3]=> D-II-and-III-only [4] => E-I,-II,-III ) Array([6] => A-Hepatitis-A [7] => B-Hepatitis-B [8] => C-Hepatitis-C [9] => D-Hepatitis-B-and-C [10] => E-None-of-the-above )....similarly
Below will work,
$new_array = array();
$i = 0;
foreach($array as $k => $v)
{
if (!empty($v))
{
$new_array[$i][$k] = $v;
continue;
}
$i++;
}
//This will fix your missing fourth index<br/>
$new_array = array_values($new_array);
var_dump($new_array);
DEMO.

MySql Query to Array to MySql IN Query

I am attempting to make an array of IDs and use that array in another query to make sure the 2nd query results have the same ids that are in the array.
As per the requirements of the rest of my code, I would prefer not to use the join method.
$topartistsquery = "SELECT main_id FROM pages WHERE catnum = '201' AND top100 = 'y'";
$result = mysql_query($topartistsquery);
$topartistarray = array();
while(($row = mysql_fetch_assoc($result))) {
$topartistarray[] = $row['main_id'];
}
//print_r($topartistarray);
$eventquery = mysql_query("SELECT * FROM TNDB_CSV2 WHERE City = '".$city."' AND PCatID = '2' AND PerformerID IN ($topartistarray) AND DateTime >= CURDATE() ORDER BY DateTime LIMIT 3");
Here is the output of the $topartistarray:
Array ( [0] => 28 [1] => 1126 [2] => 47643 [3] => 2769 [4] => 41512 [5] => 1429 [6] => 163 [7] => 48418 [8] => 198 [9] => 3748 [10] => 10161 [11] => 39890 [12] => 14160 [13] => 25407 [14] => 29219 [15] => 36980 [16] => 1860 [17] => 41299 [18] => 342 [19] => 7468 [20] => 33205 [21] => 1564 [22] => 33911 [23] => 15183 [24] => 540 [25] => 974 [26] => 8358 [27] => 30678 [28] => 4804 [29] => 4266 [30] => 517 [31] => 522 [32] => 1550 [33] => 15989 [34] => 930 [35] => 3383 [36] => 26468 [37] => 5560 [38] => 1063 [39] => 2870 [40] => 4055 [41] => 24917 [42] => 46223 [43] => 973 [44] => 334 [45] => 27970 [46] => 27985 [47] => 356 [48] => 6655 [49] => 201 [50] => 1930 [51] => 1069 [52] => 38053 [53] => 397 [54] => 21713 [55] => 53725 [56] => 4941 [57] => 864 [58] => 41904 [59] => 59233 [60] => 53970 [61] => 50875 [62] => 54175 [63] => 56861 [64] => 53317 [65] => 55258 [66] => 823 [67] => 55704 [68] => 59797 [69] => 27866 [70] => 123 [71] => 59749 [72] => 1567 [73] => 34851 [74] => 3399 [75] => 58674 [76] => 59442 [77] => 60334 )
Any constructive input is greatly appreciated. Thank you in advance
Try this:
if (isset($topartistarray) and count($topartistarray) {
$eventquery = mysql_query("SELECT * FROM TNDB_CSV2 WHERE City = '".$city."' AND PCatID = '2' AND PerformerID IN (".implode(",",$topartistarray).") AND DateTime >= CURDATE() ORDER BY DateTime LIMIT 3");
} else {
$eventquery = false;
}

Only show content of 1 array by it's name

If have and array like this:
Array (
[Example1] => Array
(
[0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] =>
)
[Example2] => Array
(
[0] => [1] => [2] => [3] => [4] =>
[5] => [6] => [7] => [8] => [9] =>
[10] => [11] => [12] => [13] => [14] =>
[15] => [16] => [17] => [18] => [19] =>
[20] => [21] => [22] => [23] => [24] =>
[25] => [26] => [27] => [28] => [29] =>
[30] => [31] => [32] => [33] => [34] =>
[35] => [36] => [37] => [38] => [39] =>
[40] => [41] => [42] => [43] => [44] =>
[45] => [46] => [47] => [48] => [49] =>
[50] => [51] => [52] => [53] => [54] =>
[55] => [56] => [57] => [58] => [59] =>
[60] => [61] => [62] => [63] => [64] =>
[65] => [66] => [67] => [68] => [69] =>
[70] => [71] => [72] => [73] => [74] =>
[75] => [76] => [77] => [78] => [79] =>
[80] => [81] => [82] => [83] => [84] =>
[85] => [86] => [87] => [88] => [89] =>
[90] => [91] => [92] => [93] => [94] =>
[95] => [96] => [97] => [98] => [99] =>
[100] => [101] => [102] => [103] => [104] =>
[105] => [106] => [107] => [108] => [109] =>
[110] => [111] => [112] => [113] => [114] =>
[115] => [116] => [117] => [118] => [119] =>
[120] => [121] => [122] => [123] => [124] =>
[125] => [126] => [127] => [128] => [129] =>
[130] => [131] => [132] => [133] => [134] =>
)
)
There's is 2 primary arrays (Example1 & Example2) and in those another array is made. I would like to know how I can only call only 1 array like "Example2" so it print only that one and ignore "Example1"
Please note that there could be more than 2 primary arrays.
And what if with that array I want to build a drop down menu, here what I have:
$__selectGroups = '';
foreach ($groups as $key => $options)
{
sort($options);
if ($key !== '')
{
$__selectGroups .= '<optgroup label="'.$key.'">';
}
$__selectGroups .= implode("\n", $options);
if ($key !== '')
{
$__selectGroups .= '</optgroup>';
}
}
How can I tell to build the drop down with only Example2 and ignore the others?
I'm not sure I understand the question... seems too simple:
print_r($yourArray['Example1']);
Since you edited your question, I think you want to do this?
foreach ($yourArray['Example1'] as $key => $value) {
}
This paradigm for accessing nested arrays applies across the board...
You need something like this:
echo '<pre>';
print_r($yourArray['Example1']);
print_r($yourArray['Example2']);
echo '<pre>';
Or you need something more elaborated?

Categories