Questions about arrays

Hi, I have a couple of questions about arrays, I wondered if anyone could help.

I want to store some data for the duration of a script. The data is a project and its status.

Project ID can be any number, active can be either one of zero.

I’d like to be able to:

Question 1. Store the following information in an array (or better data structure if appropriate). I’d rather not store it in a database until the script finished as in theory the data could update many thousands of times before the script completes and it seems it would put a strain on the database and take longer to run if it had to keep reading and writing the data.

Data is (example only):

Project ID | Active
134 | 1
54 | 1
12 | 1
56 | 1
5476 | 1

There could be a lot more entries (although probably never more than 50). Is an array the best way to store this data, bearing in mind the ‘active’ value needs to stay associated with the relevant project ID?

Question 2. Move an item to a different position in the array. For example I might have:

Project ID | Active
134 | 1
54 | 1
12 | 1
56 | 1
5476 | 1

I’d like to then be able to move the entry with 56 | 1 (for example) to the second entry in the array (or any other position of my choice) so the new order of the array is:

Project ID | Active
134 | 1
56 | 1
54 | 1
12 | 1
5476 | 1

Can this be done with a simple command or would I need to re-create the whole array?

Question 3: When a project is changed from Active 1 to Active 0 I’d like to reorder the array so that the inactive project is moved below all of the active ones, but above any that have previously been marked as inactive. So for example if I have the following array:

Project ID | Active
134 | 1
56 | 1
54 | 1
12 | 1
5476 | 0

And then the entry 56 | 1 is changed to 56 | 0 I’d like the array to become:

Project ID | Active
134 | 1
54 | 1
12 | 1
56 | 0
5476 | 0

Again, can this be done with a simple command or would I need to re-create the whole array?

If there is a better way to do all of the above I’d love to know! If not, can anyone point me towards the necessary commands?

Cheers!

This is quite easily stored as an associated array.

$projects = array(
    array('id' => 134, 'active' => 1),
    array('id' => 54, 'active' => 1),
    ...
);

The simple command is array_splice

The usort command allows you to sort using a custom sort function, which you can use to sort based on the active property of the sort items.

Thanks a lot, I’ll go through all of that and see what I can do!

Hi,

I wondered if someone could help with populating my array from my database table?

I have a SQL query and the array setup and I need to take the data from the query and enter it into the array.

The SQL is:

$sqlPROJ = "SELECT projectid, active FROM projects ";

And the array structure (as above) is:

$projects = array(
	array('projectid' => $projectid, 'active' => $active),
array('projectid' => $projectid2, 'active' => $active2),
	);

The array does currently work using set variables, however I’d like to get this all out of the database and into the array in one go using a loop if possible.

Any assistance would be much appreciated!

:slight_smile:

Fetching the data as an associative array normally does that.


$projects = array();
while ($row = mysql_fetch_assoc($result)) {
    $projects[] = $row;
}

Lovely, thanks a lot. Is it possible to insert a value from a variable set in the script as well as the data from the sql query?

So I get the values projectid and active from the SQL, but I’d also like each entry to the array to have another value called update which would always be 1.

So at the moment I have:


	$sql = "SELECT projectid, active FROM grid WHERE id = ".sql_value($id)."";
	$result = mysql_query($sql);

	$allProj = array();
	while ($row = mysql_fetch_assoc($result))
		{
    	$allProj[] = $row;
		}

At the moment this correctly outputs the following:

Array ( [0] => Array ( [projectid] => 1 [active] => 1 ) [1] => Array ( [projectid] => 2 [active] => 2 ) )

Instead I’d like the contents of the array to be:

Array ( [0] => Array ( [projectid] => 1 [active] => 1 [update] => 1 ) [1] => Array ( [projectid] => 2 [active] => 2 [update] => 1 ) )

So the value for update is always 1, but appears in each entry to the array. Is that possible?

Thanks :slight_smile:

Yes, just add the additional array elements to $row before you add it to $allProj

How? :smiley: :frowning:

$row[‘update’] = 1;