user3723161
user3723161

Reputation: 11

Split the delimited values in a flat array and populate arrays with column values

I had a array of list like this :

A = (a.11, b.12, c.dd)

I want to store the above array values in two different arrays like

B = (a, b, c)
C = (11,12,dd)

Upvotes: 1

Views: 4777

Answers (3)

Thaiseer
Thaiseer

Reputation: 92

Hope this will help you:

$a = array("a.11,b.12,c.dd");
$b = array();
$d = array();
foreach ($a as $val)
{
    $c =explode(',', $val);
    foreach ($c as $v)
    {
        $e =explode('.', $v);
        array_push($b,$e[0]);
        array_push($d,$e[1]);
    }
}

print_r($b);
print_r($d);

Working demo

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28564

Try this,

$C = [];
$B = array_map(function($v) use(&$C){$arr = explode('.', $v); $C[] = $v[1]; return $v[0];}, $A);

Upvotes: 0

Enstage
Enstage

Reputation: 2126

foreach($A as $v) {
    $v = explode('.', $v);
    $B[] = $v[0];
    $C[] = $v[1]
}

Upvotes: 0

Related Questions