I have this 2 arrays x 3 key=>value:
$a = array ( [PLTS] => 400 [SCMT] => 300 [PROG] => 100 )
$b = array ( [PLTS] => 800 [SCMT] => 400 [PROG] => 200 )
I want to convert it to 3 arrays x 2 index value like this:
$PLTS = array ( [0] => 400 [1] => 800)
$SCMT = array ( [0] => 300 [1] => 400 )
$PROG = array ( [0] => 100 [1] => 200 )
How can I do it??
Here is the general process. Build an array of the arrays and extract each column into a new var:
$PLTS = array_column(array($a, $b), 'PLTS');
$SCMT = array_column(array($a, $b), 'SCMT');
$PROG = array_column(array($a, $b), 'PROG');
To do all arrays in one:
foreach($a as $k => $v) {
${$k} = array_column(array($a, $b), $k);
}
However, I would go for a result array:
foreach($a as $k => $v) {
$result[$k] = array_column(array($a, $b), $k);
}
Here is the general process. Build an array of the arrays and extract each column into a new var:
Given your two arrays:
$a=array("PLTS"=>400,"SCMT"=>300,"PROG"=>100);
$b=array("PLTS"=>800,"SCMT"=>400,"PROG"=>200);
You can achieve it in only one foreach loop with this write less do more code:
foreach($a as $k=>$v){
extract(array($k=>array($v,$b[$k])));
}
it exactly create what you were expecting:
$PLTS = array ( [0] => 400 [1] => 800) $SCMT = array ( [0] => 300 [1] => 400 ) $PROG = array ( [0] => 100 [1] => 200 )