I have an array that looks like this:
Array
(
[0] => Array
(
[0] => malaria
[1] => pneumonia
[2] => HIV
[3] => malnutrion
)
[1] => Array
(
[0] => allfields
[1] => title
[2] => keywords
[3] => abstract
)
[2] => Array
(
[0] => and
[1] => or
[2] => or
[3] => or
)
)
I would like to convert this array into a string and combine the elements like this:
(malaria,allfields,and), (pneumonia,title,or), (HIV,keywords,or), (malnutrion,abstract,or)
You will notice that all values with the key[0]
are grouped together as well as all values with key[1]
and so on and so forth. My question is how can i do this in PHP. I have tried using array map
as recommended here but with little success. The code i have used can be seen below:
echo implode(', ', array_map(function ($entry) {
return $entry[0];
}, $myarray));
which returns this result:
malaria, allfields, and
The data for the array comes from a form with three dropdowns like so:
<div class="input-group-prepend">
<!--<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">AND</button>-->
<select name = choice2 class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >
<div class="dropdown-menu">
<option value="and" name="and">AND</option>
<option value="or" name="or">OR</option>
<option value="not" name="not">NOT</option>
</div>
</select>
</div>
<div class="input-group-prepend">
<!--<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All fields</button>-->
<select name="choice1" class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<div class="dropdown-menu">
<option value = "allfields" name = "allfields">All fields</option>
<option value = "author" name = "author">Author</option>
<option value = "title" name = "title">Title</option>
<option value = "keywords" name = "keywords">Keywords</option>
<option value = "abstract" name = "abstract">Abstract</option>
<option value = "affiliation" name = "affiliation">Affiliation</option>
</div>
</select>
</div>
<input type="text" name="item_name[]" class="form-control" aria-label="Text input with dropdown button">
Thanking you in advance