2D => 3D conversion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

2D => 3D conversion

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

So you need a loop that adds 4 numbers ([x][0] to [x][3]) and then increment x by 1.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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."?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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...
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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];
}
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

evilmonkey wrote:BDKR and noob sailboat
Saibot not sailboat :(
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

noob canoe
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

jshpro2 wrote:noob canoe
ROFLLMFAO!
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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.
Post Reply