Gadman Tang
Gadman Tang

Reputation: 91

How to split an string inside a multidimensional array in php

new to the stackoverflow community, so do be forgiving if I've done anything incorrect. As I'm an amateur in coding in php, I'm currently facing a difficulty in multi dimensional arrays.

Currently, I have this array being returned to me.

Array(
    [trialID] => 1
    [trialMixedArray] => 1,2,3,4,5,6
    [trialStatus] => active
    [trialAddedDate] => 2017-11-13 09:56:03
)

How do I split the trialMixedArray and return it to the original array so that it becomes formatted to be like the following result:

Array(
    [trialID] => 1
    [trialMixedArray] => Array(
                               [0] => 1
                               [1] => 2
                               [2] => 3
                               [3] => 4
                               [4] => 5
                               [5] => 6
    [trialStatus] => active
    [trialAddedDate] => 2017-11-13 09:56:03
)

Thanks for the help in advance! Cheers! :)

Upvotes: 2

Views: 83

Answers (3)

user3778871
user3778871

Reputation:

Use explode function to split any string based on a key.

The syntax is explode('key',string);

Upvotes: 0

Dhaval Chheda
Dhaval Chheda

Reputation: 5167

you can try something like this

$finalArray = [];
fetchItems($array_variable) {
  foreach($array_variable as $item){
    if(is_array($item) {
      fetchItems($item);
    } else {
      $finalArray[] = $item;
    }
  }
}

fetchItems($originaArray);

Upvotes: 0

Saumini Navaratnam
Saumini Navaratnam

Reputation: 8850

Use explode function, which splits string to array. Manual.

$results['trialMixedArray'] = explode(',', $results['trialMixedArray']);

Upvotes: 2

Related Questions