1

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

Alex Maina
  • 296
  • 3
  • 11

2 Answers2

1

I'd suggest use a for loop instead, implode inside the block, push inside another container, then finally implode again.

Here's the idea:

$new_array = array(); // another container
$count = count($array[0]); // get the count based on column
for ($i = 0; $i < $count; $i++) { // loop and terminate based on column count
    // use array column to extract desired data column wise
    // then implode each column batch and push inside
    $new_array[] = '(' . implode(',', array_column($array, $i)) . ')';
}
// inside the new array there will be 4 joined column, and combine them again
echo implode(', ', $new_array);

Here's a sample fiddle


Sidenote: I'm guessing the feeling that this data came from a form. If you want them grouped upon submission, just create a grouping name attribute instead.

Like this:

<input type="text" name="input[0][name]" />
<input type="text" name="input[0][keywords]" />
<input type="radio" name="input[0][conjunction]" value="and" />
<input type="radio" name="input[0][conjunction]" value="or" />

If you can make your form this way, this would eliminate having any array manipulation at all. Once you submit, the data is already grouped.

Kevin
  • 41,694
  • 12
  • 53
  • 70
0
            $myarray = array(
                array('malaria','pneumonia','HIV','malnutrion'),
                array('allfields','title','keywords','abstract'),
                array('and','or','or','or'),
            );
            $temparray = [];
            $tempstr = '';
                foreach($myarray[0] as $key2 => $value){
                    $tempstr .= '(';
                    for($i = 0;$i<count($myarray);$i++)
                    {
                        $tempstr .= $myarray[$i][$key2];
                        if(count($myarray) != $i + 1){
                            $tempstr .= ' ,';
                        }
                    }
                    $tempstr .= ')';
                    $temparray[] = $tempstr;
                    $tempstr = '';
                }
            echo implode(', ',$temparray);
            // (malaria ,allfields ,and), (pneumonia ,title ,or), (HIV ,keywords ,or), (malnutrion ,abstract ,or)

the output is: (malaria ,allfields ,and), (pneumonia ,title ,or), (HIV ,keywords ,or), (malnutrion ,abstract ,or)