Jump to content

[SOLVED] very expert an tough question..


zgkhoo

Recommended Posts

i wanna sort the number in an array ..which consist of

1 2 3 4 5 6 7 8

 

become this(split the middle of the back part of array and put into the bottom)

1 2 3 4
5 6 7 8

 

then become (split middle again )

 

1 2
5 6
3 4
7 8

then become

 

1
5
3
7
2
6
4
8

 

then finally the array sort from

 1 2 3 4 5 6 7 8 

to

1 5 3 7 2 6 4 8

 

anyone how to write it?  ???

 

 

 

 

 

 

 

 

move the back middle part of the array to

Link to comment
Share on other sites

here you go

 

 

<?php

global $arrReturn;
$arrReturn = array();
$arrInput = array(0=>array(1, 2, 3, 4, 5, 6, 7,);


function fnSplitArray($arrInput,$intCount)
{
Global $arrReturn;
$arrOutput = array();
$arrOne = array();
$arrTwo = array();
for ($i=0;$i<count($arrInput);$i++)
{
	$arrOne[] = array_splice($arrInput[$i],0,count($arrInput[$i])/2);
	$arrTwo[] = $arrInput[$i];

}
$arrOutput = array_merge($arrOne,$arrTwo);

$arrReturn = $arrOutput;
if ($intCount>count($arrOutput))
{
	fnSplitArray($arrOutput,$intCount);
}
}

fnSplitArray($arrInput,count($arrInput[0]));
print_r($arrReturn);
?>

Link to comment
Share on other sites

a little modified

 

global $arrReturn;
$arrReturn = array();
$arrInput = array(0=>array(1, 2, 3, 4, 5, 6, 7,);


function fnSplitArray($arrInput,$intCount)
{
Global $arrReturn;
$arrOutput = array();
$arrOne = array();
$arrTwo = array();
for ($i=0;$i<count($arrInput);$i++)
{
	$arrOne[] = array_splice($arrInput[$i],0,count($arrInput[$i])/2);
	$arrTwo[] = $arrInput[$i];

}
$arrOutput = array_merge($arrOne,$arrTwo);

$arrReturn = $arrOutput;
if ($intCount>count($arrOutput))
{
	fnSplitArray($arrOutput,$intCount);
}
else 
{
	$arrReturn = array();
	foreach ($arrOutput as $strKey => $arrValues)
	{
			array_push($arrReturn,$arrValues[0]);
	}

}
}

fnSplitArray($arrInput,count($arrInput[0]));
print_r($arrReturn);

Link to comment
Share on other sites

try

<?php
function new_order($a){
$out = array();
$n = count($a) / 2;
foreach ($a as $k => $v){
	$m = $n;
	$newk = 0;
	while ($k > 0){
		$newk += ($k % 2) * $m;
		$k = (int) $k / 2;
		$m = $m / 2;
	}
	$out[$newk] = $v;
}
ksort($out);
return $out;
}
$a = array(1,2,3,4,5,6,7,;
$b = new_order($a);
print_r($b);
?>

Link to comment
Share on other sites

Array ( [0] => Array ( ) [1] => Array ( [0] => 16 ) [2] => Array ( [0] => 8 ) [3] => Array ( [0] => 24 ) [4] => Array ( [0] => 4 ) [5] => Array ( [0] => 20 ) [6] => Array ( [0] => 12 ) [7] => Array ( [0] => 28 ) [8] => Array ( [0] => 2 ) [9] => Array ( [0] => 18 ) [10] => Array ( [0] => 10 ) [11] => Array ( [0] => 26 ) [12] => Array ( [0] => 6 ) [13] => Array ( [0] => 22 ) [14] => Array ( [0] => 14 ) [15] => Array ( [0] => 30 ) [16] => Array ( [0] => 1 ) [17] => Array ( [0] => 17 ) [18] => Array ( [0] => 9 ) [19] => Array ( [0] => 25 ) [20] => Array ( [0] => 5 ) [21] => Array ( [0] => 21 ) [22] => Array ( [0] => 13 ) [23] => Array ( [0] => 29 ) [24] => Array ( [0] => 3 ) [25] => Array ( [0] => 19 ) [26] => Array ( [0] => 11 ) [27] => Array ( [0] => 27 ) [28] => Array ( [0] => 7 ) [29] => Array ( [0] => 23 ) [30] => Array ( [0] => 15 ) [31] => Array ( [0] => 31 ) )

output if enter only 31 number

Link to comment
Share on other sites

a little modified

 

global $arrReturn;
$arrReturn = array();
$arrInput = array(0=>array(1, 2, 3, 4, 5, 6, 7,);


function fnSplitArray($arrInput,$intCount)
{
Global $arrReturn;
$arrOutput = array();
$arrOne = array();
$arrTwo = array();
for ($i=0;$i<count($arrInput);$i++)
{
	$arrOne[] = array_splice($arrInput[$i],0,count($arrInput[$i])/2);
	$arrTwo[] = $arrInput[$i];

}
$arrOutput = array_merge($arrOne,$arrTwo);

$arrReturn = $arrOutput;
if ($intCount>count($arrOutput))
{
	fnSplitArray($arrOutput,$intCount);
}
else 
{
	$arrReturn = array();
	foreach ($arrOutput as $strKey => $arrValues)
	{
			array_push($arrReturn,$arrValues[0]);
	}

}
}

fnSplitArray($arrInput,count($arrInput[0]));
print_r($arrReturn);

 

why modified???

Link to comment
Share on other sites

how to let print_r($array) in a proper way?

 

which this ...very unreadable

Array ( [0] => Array ( ) [1] => Array ( [0] => 16 ) [2] => Array ( [0] => 8 ) [3] => Array ( [0] => 24 ) [4] => Array ( [0] => 4 ) [5] => Array ( [0] => 20 ) [6] => Array ( [0] => 12 ) [7] => Array ( [0] => 28 ) [8] => Array ( [0] => 2 ) [9] => Array ( [0] => 18 ) [10] => Array ( [0] => 10 ) [11] => Array ( [0] => 26 ) [12] => Array ( [0] => 6 ) [13] => Array ( [0] => 22 ) [14] => Array ( [0] => 14 ) [15] => Array ( [0] => 30 ) [16] => Array ( [0] => 1 ) [17] => Array ( [0] => 17 ) [18] => Array ( [0] => 9 ) [19] => Array ( [0] => 25 ) [20] => Array ( [0] => 5 ) [21] => Array ( [0] => 21 ) [22] => Array ( [0] => 13 ) [23] => Array ( [0] => 29 ) [24] => Array ( [0] => 3 ) [25] => Array ( [0] => 19 ) [26] => Array ( [0] => 11 ) [27] => Array ( [0] => 27 ) [28] => Array ( [0] => 7 ) [29] => Array ( [0] => 23 ) [30] => Array ( [0] => 15 ) [31] => Array ( [0] => 31 ) )

 

Link to comment
Share on other sites

thank you very much

wat if i enter no enough number ? eg..only 6 number

1,2,3,4,5,6,7

wat will happen?

 

what you want to script do?

but you say:

the total number is binary ..which will only

2 number

4 number

8 number (example of above is this)

16 number

32 number

64 number

and so on..

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.