I have the following code:
$a = array('a' => 'some value', 'b' => 'some value', 'c' => 'some value');
$b = array('a' => 'another value', 'd' => 'another value', 'e' => 'another value', 'f' => 'another value');
$c = array('b' => 'some more value', 'x' => 'some more value', 'y' => 'some more value', 'z' => 'some more value');
$d = array($a, $b, $c);
Printing $d will output:
Array
(
[0] => Array
(
[a] => some value
[b] => some value
[c] => some value
)
[1] => Array
(
[a] => another value
[d] => another value
[e] => another value
[f] => another value
)
[2] => Array
(
[b] => some more value
[x] => some more value
[y] => some more value
[z] => some more value
)
)
How would I go about combining the array keys and end up with the following output?
Array
(
[0] => Array
(
[a] => some value
[b] => some value
[c] => some value
[d] =>
[e] =>
[f] =>
[x] =>
[y] =>
[z] =>
)
[1] => Array
(
[a] => another value
[b] =>
[c] =>
[d] => another value
[e] => another value
[f] => another value
[x] =>
[y] =>
[z] =>
)
[2] => Array
(
[a] =>
[b] => some more value
[c] =>
[d] =>
[e] =>
[f] =>
[x] => some more value
[y] => some more value
[z] => some more value
)
)
Answer1:
Yes you can use array_merge in this case:
$a = array('a' => 'some value', 'b' => 'some value', 'c' => 'some value');
$b = array('a' => 'another value', 'd' => 'another value', 'e' => 'another value', 'f' => 'another value');
$c = array('b' => 'some more value', 'x' => 'some more value', 'y' => 'some more value', 'z' => 'some more value');
$d = array($a, $b, $c);
$keys = array();
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($d)) as $key => $val) $keys[$key] = '';
$data = array();
foreach($d as $values) {
$data[] = array_merge($keys, $values);
}
echo '<pre>';
print_r($data);
Answer2:
$a = array('a' => 'some value', 'b' => 'some value', 'c' => 'some value');
$b = array('a' => 'another value', 'd' => 'another value', 'e' => 'another value', 'f' => 'another value');
$c = array('b' => 'some more value', 'x' => 'some more value', 'y' => 'some more value', 'z' => 'some more value');
$d = array_merge(array_merge($a, $b),$c);
foreach($d as $k=>$v){
$aN[$k] = isset($a[$k])?$a[$k]:'';
$bN[$k] = isset($b[$k])?$b[$k]:'';
$cN[$k] = isset($c[$k])?$c[$k]:'';
}
$dN = array($aN, $bN, $cN );
$a = array('a' => 'some value', 'b' => 'some value', 'c' => 'some value');
$b = array('a' => 'another value', 'd' => 'another value', 'e' => 'another value', 'f' => 'another value');
$c = array('b' => 'some more value', 'x' => 'some more value', 'y' => 'some more value', 'z' => 'some more value');
$d = array($a, $b, $c);
Printing $d will output:
Array
(
[0] => Array
(
[a] => some value
[b] => some value
[c] => some value
)
[1] => Array
(
[a] => another value
[d] => another value
[e] => another value
[f] => another value
)
[2] => Array
(
[b] => some more value
[x] => some more value
[y] => some more value
[z] => some more value
)
)
How would I go about combining the array keys and end up with the following output?
Array
(
[0] => Array
(
[a] => some value
[b] => some value
[c] => some value
[d] =>
[e] =>
[f] =>
[x] =>
[y] =>
[z] =>
)
[1] => Array
(
[a] => another value
[b] =>
[c] =>
[d] => another value
[e] => another value
[f] => another value
[x] =>
[y] =>
[z] =>
)
[2] => Array
(
[a] =>
[b] => some more value
[c] =>
[d] =>
[e] =>
[f] =>
[x] => some more value
[y] => some more value
[z] => some more value
)
)
Answer1:
Yes you can use array_merge in this case:
$a = array('a' => 'some value', 'b' => 'some value', 'c' => 'some value');
$b = array('a' => 'another value', 'd' => 'another value', 'e' => 'another value', 'f' => 'another value');
$c = array('b' => 'some more value', 'x' => 'some more value', 'y' => 'some more value', 'z' => 'some more value');
$d = array($a, $b, $c);
$keys = array();
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($d)) as $key => $val) $keys[$key] = '';
$data = array();
foreach($d as $values) {
$data[] = array_merge($keys, $values);
}
echo '<pre>';
print_r($data);
Answer2:
$a = array('a' => 'some value', 'b' => 'some value', 'c' => 'some value');
$b = array('a' => 'another value', 'd' => 'another value', 'e' => 'another value', 'f' => 'another value');
$c = array('b' => 'some more value', 'x' => 'some more value', 'y' => 'some more value', 'z' => 'some more value');
$d = array_merge(array_merge($a, $b),$c);
foreach($d as $k=>$v){
$aN[$k] = isset($a[$k])?$a[$k]:'';
$bN[$k] = isset($b[$k])?$b[$k]:'';
$cN[$k] = isset($c[$k])?$c[$k]:'';
}
$dN = array($aN, $bN, $cN );
0 comments:
Post a Comment