Last active
September 20, 2016 09:02
-
-
Save ethaizone/b7d3a833dcdeb80234dde516649ac06d to your computer and use it in GitHub Desktop.
Build multi dimentions array from arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = []; | |
data.push(['S', 'M', 'L']); | |
data.push(['RED', 'YELLOW']); | |
data.push(['IRON', 'COTTON']); | |
data.push(['ccc', 'ddd']); | |
// For clone array | |
Array.prototype.clone = function() { | |
return this.slice(0); | |
}; | |
/** | |
* Build multi dimention array from arrays | |
* @param array data | |
* @return array | |
*/ | |
function buildMultiDimentionArray(data, result) | |
{ | |
// shift segment for use in this call | |
var segments = data.shift(); | |
// First loop | |
if (typeof result !== 'object') { | |
// create result from first segment! | |
var result = []; | |
for (var i = 0; i < segments.length; i++) { | |
result.push([segments[i]]); | |
} | |
} else { | |
// Recursive call! | |
// create result template | |
var resultTemplate = [] | |
for (var i = 0; i < result.length; i++) { | |
resultTemplate.push(result[i]); | |
} | |
// recreate result by duplicate resultTemplate follow amount current segment | |
// and push it back to result | |
var result = []; | |
for (var i = 0; i < resultTemplate.length; i++) { | |
for (var i2 = 0; i2 < segments.length; i2++) { | |
result.push(resultTemplate[i].clone()); | |
} | |
} | |
// push data from segments to all result record | |
// via array walk | |
var index = 0; | |
for (var i = 0; i < result.length; i++) { | |
result[i].push(segments[index]); | |
index++; | |
if (index >= segments.length) { | |
index = 0; | |
} | |
} | |
} | |
// if data has another dimention, recursive call!! | |
if (data.length > 0) { | |
return buildMultiDimentionArray(data, result); | |
} | |
return result; | |
} | |
var result = buildMultiDimentionArray(data); | |
console.log(result, result.length); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = []; | |
$data[0] = ['dsasd' =>'S', 'M', 'asdasd' => 'L']; | |
$data[1] = ['RED', 'BLACK']; | |
$data[2] = ['IRON', 'COTTON']; | |
$data[5] = ['CCC', 'DDD']; | |
/** | |
* Build multi dimention array from arrays | |
* @param array data | |
* @return array | |
*/ | |
function buildMultiDimentionArray(Array $data, $result = []) | |
{ | |
// shift segment for use in this call | |
$segments = array_shift($data); | |
// First loop | |
if (empty($result)) { | |
// create result from first segment! | |
$result = []; | |
foreach ($segments as $segment) { | |
$result[] = [$segment]; | |
} | |
} else { | |
// Recursive call! | |
// create result template | |
$resultTemplate = array_values($result); | |
// recreate result by duplicate resultTemplate follow amount current segment | |
// and push it back to result | |
$result = []; | |
foreach ($resultTemplate as $t) { | |
for ($i=0; $i < count($segments); $i++) { | |
$result[] = $t; | |
} | |
} | |
// push data from segments to all result record | |
// via array walk | |
reset($segments); | |
foreach ($result as $index => $v) { | |
$value = current($segments); | |
if (! $value) { | |
$value = reset($segments); | |
} | |
$result[$index][] = $value; | |
next($segments); | |
} | |
} | |
// if data has another dimention, recursive call!! | |
if (!empty($data)) { | |
$result = buildMultiDimentionArray($data, $result); | |
} | |
return $result; | |
} | |
print_r(buildMultiDimentionArray($data)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = {0:['S','M','L','XL'],1:['Red','Blue','Black']}; | |
// For clone array | |
Array.prototype.clone = function() { | |
return this.slice(0); | |
}; | |
// Array to object | |
Array.prototype.toObject = function() { | |
var newObj = new Object; | |
for (var i = 0; i < this.length; i++) { | |
newObj[i] = this[i]; | |
} | |
return newObj; | |
}; | |
// Object to array | |
Object.prototype.toArray = function() { | |
var newArr =[]; | |
for( var i in this ) { | |
if (this.hasOwnProperty(i)){ | |
newArr.push(this[i]); | |
} | |
} | |
return newArr; | |
}; | |
/** | |
* Build multi dimention array from arrays | |
* @param array data | |
* @return array | |
*/ | |
function buildMultiDimentionArray(data) | |
{ | |
var result = []; | |
// shift segment for use in this call | |
while(segments = data.shift()) { | |
if (result.length == 0) { | |
// create result from first segment! | |
for (var i = 0; i < segments.length; i++) { | |
result.push([segments[i]]); | |
} | |
continue; | |
} | |
var newResult = []; | |
// recreate result by duplicate result follow amount current segment | |
// and push it back to result | |
for (var i = 0; i < result.length; i++) { | |
for (var i2 = 0; i2 < segments.length; i2++) { | |
newResult.push(result[i].clone()); | |
} | |
} | |
// push data from segments to all result record | |
// via array walk | |
var index = 0; | |
for (var i = 0; i < newResult.length; i++) { | |
newResult[i].push(segments[index]); | |
index++; | |
if (index >= segments.length) { | |
index = 0; | |
} | |
} | |
result = newResult.clone(); | |
} | |
return result; | |
} | |
var result = buildMultiDimentionArray(input.toArray()).toObject(); | |
console.log(result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = []; | |
data.push(['S', 'M', 'L']); | |
data.push(['RED', 'YELLOW']); | |
data.push(['IRON', 'COTTON']); | |
data.push(['ccc', 'ddd']); | |
// For clone array | |
Array.prototype.clone = function() { | |
return this.slice(0); | |
}; | |
/** | |
* Build multi dimention array from arrays | |
* @param array data | |
* @return array | |
*/ | |
function buildMultiDimentionArray(data) | |
{ | |
var result = []; | |
// shift segment for use in this call | |
while(segments = data.shift()) { | |
if (result.length == 0) { | |
// create result from first segment! | |
for (var i = 0; i < segments.length; i++) { | |
result.push([segments[i]]); | |
} | |
continue; | |
} | |
var newResult = []; | |
// recreate result by duplicate result follow amount current segment | |
// and push it back to result | |
for (var i = 0; i < result.length; i++) { | |
for (var i2 = 0; i2 < segments.length; i2++) { | |
newResult.push(result[i].clone()); | |
} | |
} | |
// push data from segments to all result record | |
// via array walk | |
var index = 0; | |
for (var i = 0; i < newResult.length; i++) { | |
newResult[i].push(segments[index]); | |
index++; | |
if (index >= segments.length) { | |
index = 0; | |
} | |
} | |
result = newResult.clone(); | |
} | |
return result; | |
} | |
var result = buildMultiDimentionArray(data); | |
console.log(result, result.length); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = []; | |
$data[0] = ['dsasd' =>'S', 'M', 'asdasd' => 'L']; | |
$data[1] = ['RED', 'BLACK']; | |
$data[2] = ['IRON', 'COTTON']; | |
$data[5] = ['CCC', 'DDD']; | |
/** | |
* Build multi dimention array from arrays | |
* @param array data | |
* @return array | |
*/ | |
function buildMultiDimentionArray(Array $data) | |
{ | |
$result = []; | |
// shift segment for use in this call | |
while($segments = array_shift($data)) { | |
if (empty($result)) { | |
// First loop | |
// create result from first segment! | |
foreach ($segments as $segment) { | |
$result[] = [$segment]; | |
} | |
continue; | |
} | |
// recreate result by duplicate result follow amount current segment | |
// and push it back to result | |
$newResult = []; | |
foreach ($result as $t) { | |
for ($i=0; $i < count($segments); $i++) { | |
$newResult[] = $t; | |
} | |
} | |
// push data from segments to all result record | |
// via array walk | |
reset($segments); | |
foreach ($newResult as $index => $v) { | |
$value = current($segments); | |
if (! $value) { | |
$value = reset($segments); | |
} | |
$newResult[$index][] = $value; | |
next($segments); | |
} | |
$result = $newResult; | |
} | |
return $result; | |
} | |
print_r(buildMultiDimentionArray($data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment