Skip to content

Instantly share code, notes, and snippets.

@levantoan
Last active August 27, 2016 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levantoan/2a66dafad7a9a3a88468170ecce0cdab to your computer and use it in GitHub Desktop.
Save levantoan/2a66dafad7a9a3a88468170ecce0cdab to your computer and use it in GitHub Desktop.
<?php
/*
Array
(
[name] => Array
(
[0] => 0as
[1] => 1as
[2] => 2as
[3] => 3as
)
[top] => Array
(
[0] => 0
[1] => 34
[2] => 68
[3] => 102
)
[left] => Array
(
[0] => 0
[1] => 24
[2] => 48
[3] => 72
)
)
Kết quả ===================>
Array(
[0] => Array(
[name] => 0as
[top] => 0
[left] => 0
)
[1] => Array(
[name] => 1as
[top] => 34
[left] => 24
)
[2] => Array(
[name] => 2as
[top] => 68
[left] => 48
)
[3] => Array(
[name] => 3as
[top] => 102
[left] => 72
)
)
*/
// input
$aInput = array(
"name" => array("0as","1as","2as","3as","4as"),
"top" => array(0,34,68,102,104),
"left" => array(0,24,48,72,04),
"bot" => array(0,24,48,72,04),
"right" => array(0,24,48,72,04),
"toan" => array(23,24,48,72,04)
);
// output
$aOutput = array();
$firstKey;
foreach ($aInput as $key => $value){
$firstKey = $key;
break;
}
$nCountKey = count($aInput[$firstKey]);
for ($i =0; $i<$nCountKey;$i++){
$element;
foreach ($aInput as $key => $value){
$element[$key] = $value[$i];
}
array_push($aOutput,$element);
}
echo "<pre>";
print_r($aOutput);
echo "</pre>";
<?php
// input
$aInput = array(
"name" => array("0as","1as","2as","3as","4as","60"),
"top" => array(0,34,68),
"left" => array(0,24,48,72,04),
"bot" => array(0,24,48,72,04,0,24,48,72,04),
"right" => array(0)
);
// output
$aOutput = array();
$firstKey;
// get first key
foreach ($aInput as $key => $value){
$firstKey = $key;
break;
}
// get length max
foreach ($aInput as $key => $value){
if (count($aInput[$firstKey]) < count($value)){
$firstKey = $key;
}
}
// get length first key
$nCountKey = count($aInput[$firstKey]);
echo $firstKey."<br>";
echo $nCountKey."<br>";
// n key of first key
for ($i =0; $i<$nCountKey;$i++){
// element in array output
$element;
// get each key in array input
foreach ($aInput as $key => $value){
if (array_key_exists($i, $value)) {
$element[$key] = $value[$i];
}
}
// add in array output
array_push($aOutput,$element);
unset($element);
}
echo "<pre>";
print_r($aOutput);
echo "</pre>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment