-2

I have a multidimensional array containing comma-separated strings like this

[
    [
        "users" =>  [
            'email' => 'test@yahoo.com ,testuser@yahoo.com',
            'username' => 'test,testuser',
            'description' => 'description1,description2'
        ]
    ]
]

I want to access the users subarray data, explode on delimiters, and create a new associative array of indexed arrays.

Desired result:

$User = array(
    'email' => array(
        'test@yahoo.com',
        'testuser@yahoo.com'
    ),
    'username' => array(
        'test',
        'testuser'
    ),
    'description' => array(
        'description1',
        'description2'
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

4 Answers4

7

For only one index:

$arrayTwoD = array();
foreach ($valueMult[0]['User'] as $key => $value) { 
    $arrayTwoD[$key] = array_push(explode(',', $value));
}

If you have multiple indexes in $multArray:

$arrayTwoD = array();
foreach ($multArray as $keyMult => $valueMult) { 
    foreach ($valueMult['User'] as $key => $value) { 
        $arrayTwoD[$keyMult][$key] = array_push(explode(',', $value));
    }
}

or

$arrayTwoD = array();
foreach ($multArray as $array) { 
    foreach ($array['User'] as $key => $value) { 
        $arrayTwoD[$key] = array_push(explode(',', $value));
    }
}
Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
2

try this

$array = array(...); // your array data
$formedArray = array();

foreach ( $array as $arr )
{
    foreach ( $arr['user'] as $key => $value )
    {
        $formedArray[$key] = array_push(explode(",",$value));
    }
}

echo "<pre>";
print_r($formedArray);
echo "</pre>";
Dwza
  • 6,494
  • 6
  • 41
  • 73
1

It's a little bit repetitive, I know, but you can do like this as well:

foreach($array as $users) {
    foreach($users as &$value) { // &value is assigned by reference
        $users['users']["email"] = explode(",", $value['email']);
        $users['users']["username"] = explode(",", $value['username']);
        $users['users']["description"] = explode(",", $value['description']);
    }
}

But after that, you need to use $value. Refer to the official PHP manual documentation to know more about what the & symbol does here.

Demo

Community
  • 1
  • 1
Dev'Hamz
  • 478
  • 4
  • 9
  • if you wouldn't override your results every time, you would run into a bad thing here... because you manipulate the foreach base array every time... this onley works because there is not much data AND because you override it all the time... if you would prevent it from overriding... this could end in a endless loop – Dwza May 06 '14 at 10:10
0

Using array_map() can be used to access the subset data (without declaring any new variables in the global scope) and make iterate calls of preg_split() to separate the delimited values into subarrays.

Code: (Demo)

var_export(
    array_map(
        fn($csv) => preg_split('/ ?,/', $csv),
        $array[0]['users']
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136