Page 1 of 2

2D => 3D conversion

Posted: Wed Dec 28, 2005 6:27 pm
by evilmonkey
This should be elementary programming, and I distincltly remeber being taught this, but for the life of me I can't remeber and can't figure out how to do it. I have an array of two dimensions, where the first dimension is simply a number from 0 to n. The second dimension is just some information about a user. What I want to do is I want to turn it into a 3D array, the first dimension of which can be 0 to n, but the second dimension has to be 0 to 2. The third dimension is still info about the user. Basically, I want to go from this:

Code: Select all

searchresults Array=>
[0]=>Array
     ['username']=>evilmonkey
     ['picture'] => evil.jpg
[1]=>Array
    ['username']=>joeblow
    ['picture']=>joeblow.jpg
[2] => array
    [username] => foobar
    [pic] => foobar.jpg 
[3] => array
    [username] => bazfoo
    [pic] => bazfoo.jpg 
[3] => array
    [username] => zoobar
    [pic] => zoobar.jpg
to this:

Code: Select all

results [0] => array
           [0] => array
               [username] => evilmonkey
               [pic] => evil.jpg
             [1] => array
                [username] => joeblow
                [pic] => joeblow.jpg
             [2] => array
                [username] => foobar
                [pic] => foobar.jpg 
         [1] => array
             [0] => array
                 [username] => bazfoo
                 [pic] => bazfoo.jpg 
    	      [1] => array
   		  [username] => zoobar
    	          [pic] => zoobar.jpg
I know I need to use some for loops, but for the life of me I can't figure out how. Sorry about such a newb question. :(

Thanks!

Posted: Wed Dec 28, 2005 6:41 pm
by Chris Corbyn
I can't see what you want to add cos you didnt post it but:

Code: Select all

$array = $users; //Multidimensional array you posted

foreach ($array as $sub_array)
{
    foreach ($sub_array as $sub_sub_array)
    {
        $array1[$sub_array][$sub_sub_array][] = 'foo';
    }
}
As for your exact logic you havent given us enough info to work with :D

Posted: Wed Dec 28, 2005 6:52 pm
by evilmonkey
No no. I want to keep the values of the original array, but add another dimension to the front. This of it this way:

I have:
$var[0]='foo', $var[1]='bar', $var[2]='foobar', $var[3]='baz', $var[4]='foobaz'] and so on
and I want to convert it to:
$var[0][0]='foo', $var[0][1]='bar', $var[0][2]='foobar', $var[1][0]='baz', $var[1][1]='foobaz' and so on

Posted: Wed Dec 28, 2005 7:05 pm
by timvw
So you need a loop that adds 4 numbers ([x][0] to [x][3]) and then increment x by 1.

Posted: Wed Dec 28, 2005 7:09 pm
by evilmonkey
I know the structure of a for loop (I've been doign PHP for a number of years)...this is just a case of mental block...can you tell what you mean by "adds 4 numbers ([x][0] to [x][3]) and then increment x by 1."?

Posted: Wed Dec 28, 2005 7:23 pm
by Chris Corbyn

Code: Select all

$array = $users; //Multidimensional array you posted

foreach ($array as $sub_array)
{
    foreach ($sub_array as $sub_sub_array => $arbitrary_value)
    {
        $array1[$sub_array][$sub_sub_array][] = array('foo');
    }
}
:?: :?:

:lol: I'm lost now :P

Posted: Wed Dec 28, 2005 7:43 pm
by evilmonkey
That doesn't do what I need it to do though. I want to keep the values, just shift the elements...look at the array dumps in the original post...

Posted: Wed Dec 28, 2005 11:53 pm
by n00b Saibot
You can do this :arrow:

Code: Select all

$Idx = 0;
$Arr3D = array();
foreach ($Results as $No=>$userInfo)
{
 $Arr3D[$Idx][] = $userInfo;
 $Idx = ($No>0 && $No%3==0)?$Idx++:$Idx;
}
of course there are other ways to do this too... :wink:

Posted: Thu Dec 29, 2005 1:19 am
by Jenk
without knowing the logic of what you want we cannot possibly answer this question for you.

On what criteria is the dimension set?

Posted: Thu Dec 29, 2005 7:13 am
by BDKR
Since you know you need to do some iteration here, just create a function for this. Within the function create a new array and populate with the rearragened elements of the old. Once you are finished, return it. Shuffling the items in the existing array isn't worth the effort.

Posted: Fri Dec 30, 2005 12:20 pm
by evilmonkey
BDKR and noob sailboat, thank you very much. I combined your ideas to make this:

Code: Select all

$arr = array(0=>0,1,2,3,4,5,6,7,8,9,10);
for ($i=0; $i<=sizeof($arr)-1; $i++){
	$dimension1 = floor($i/3);
	$dimension2 = $i % 3;
	$newarr[$dimension1][$dimension2]=$arr[$i];
}

Posted: Sat Dec 31, 2005 12:26 am
by n00b Saibot
evilmonkey wrote:BDKR and noob sailboat
Saibot not sailboat :(

Posted: Sat Dec 31, 2005 12:35 am
by josh
noob canoe

Posted: Sat Dec 31, 2005 2:00 am
by nigma
jshpro2 wrote:noob canoe
ROFLLMFAO!

Posted: Sat Dec 31, 2005 8:10 pm
by evilmonkey
Oops, sorry n00b. I guess you're going to be the butt of the jokes for a while. :P Seriously, sorry about that. I gotta pay more attention.