Jump to content

How do I group this?


KingOfHeart

Recommended Posts

I'm not sure how to set this up but I'll describe what I need.

 

I'm using a script to read the values in the file. Then I take those values and record them. Now I need to organize it into a string which will create a grid.

 

Let me make an example of the values.

Object1 (0,0)

Object2 (1,0)

Object3(1,1)

 

So we have three objects and their grid coordinates. Here are their string values

Object1 - A

Object2 - B

Object3 - C

 

If the grid is blank it would be a 0.

If it's the end of the row it would be a |

So basically the string data will look like this once it's outputted.

 

AB|

0C

 

Hopefully your not lost.

The part I need help with is grouping the coordinates with the Object.

 

If you are lost then maybe you'll understand the part below.

 

$name = array('object1','object2','object3');

$grid = array(0.0,1.0,1.1);

 

Should I use a decimal first of all? I need to group the $name with the coordinates.

 

Link to comment
https://forums.phpfreaks.com/topic/176624-how-do-i-group-this/
Share on other sites

Hi

 

I think if I was storing them in code like that then I would store it as an array of arrays

 

$grid = array(array(0,0),array(1,0),array(1,1));

 

However I would also be tempted to store it within a database. You should be able to bring back a full grid with a marker for the contents of any cell (or blank if no contents).

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/176624-how-do-i-group-this/#findComment-931165
Share on other sites

That entire string will be sent to a database.

I have a game that's going to download that string (already works) and use it to make a level.

 

So you can do a double array.

How would that look as a variable?

 

$name = "object1";

$x = 0;

$y = 0;

 

$name = "object2";

$x = 1;

$y = 0;

 

$name = "object3";

$x = 1;

$y = 1;

 

How do I make that into array(s)?

Link to comment
https://forums.phpfreaks.com/topic/176624-how-do-i-group-this/#findComment-931177
Share on other sites

This will print the same thing either way - its a matter of which you prefer it to look like (not too much difference in code)

<pre>
<?php

// Your grid
$grid = array(
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0)
);

// All your objects in one variable
$objects = array(
'object1' => array(
	'string' => 'A',
	'position' =>
		array(
			'x' => 1,
			'y' => 0
		)
	),
'object2' => array(
	'string' => 'B',
	'position' =>
		array(
			'x' => 3,
			'y' => 2
		)
	),
'object3' => array(
	'string' => 'C',
	'position' =>
		array(
			'x' => 3,
			'y' => 1
		)
	),
);


// Loop through each object, and set the position on the grid.
foreach($objects as $o) {
$grid[$o['position']['x']][$o['position']['y']] = $o['string'];
}

// Show the grid
print_r($grid);


// Or alternatively...


// Your grid
$grid = array(
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0)
);

// Each object
$object1 = array(
'string' => 'A',
'position' =>
	array(
		'x' => 1,
		'y' => 3
	)
);
$object2 = array(
'string' => 'B',
'position' =>
	array(
		'x' => 2,
		'y' => 1
	)
);
$object3 = array(
'string' => 'C',
'position' =>
	array(
		'x' => 3,
		'y' => 3
	)
);

// A list of the object names
$objects = array('object1', 'object2', 'object3');

// Loop through each one - using variable variables
foreach($objects as $o) {
$o = $$o;
$grid[$o['position']['x']][$o['position']['y']] = $o['string'];
}

// show the grid
print_r($grid);

?>

Link to comment
https://forums.phpfreaks.com/topic/176624-how-do-i-group-this/#findComment-931192
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.