开发者

manipulate multiarrays

I am pulling some data from a mysql table via the following:

$result = mysql_query("SELECT characters_ID, name, borndate, deathdate, marrieddate, ispregnant FROM characters WHERE isfemale='1'",$db);
$femaledata = array();

while ($row_user = my开发者_开发百科sql_fetch_assoc($result))
$femaledata[] = $row_user;

This gives me an array that looks like this:

Array ( 
    [0] => Array ( [characters_ID] => 2 [name] => Helene [borndate] => 35 [deathdate] => 431 [marrieddate] => 157 [ispregnant] => 0 ) 
    [1] => Array ( [characters_ID] => 4 [name] => Isabelle [borndate] => 161 [deathdate] => [marrieddate] => 303 [ispregnant] => 1 ) 
    [2] => Array ( [characters_ID] => 7 [name] => Helene [borndate] => 326 [deathdate] => [marrieddate] => [ispregnant] => 0 ) 
    [3] => Array ( [characters_ID] => 72 [name] => Faylinn [borndate] => 335 [deathdate] => [marrieddate] => [ispregnant] => 0 ) 
    [4] => Array ( [characters_ID] => 74 [name] => Relina [borndate] => 349 [deathdate] => [marrieddate] => [ispregnant] => 0 ) 
    )

Now I need to remove any characters who have a value for deathdate or ispregnant, and then I need to run some code on the others. For instance I need to grab the borndate value, compare it to the current date to find age, and based partly on age, I need to run code for each to determine if the character has become pregnant on the turn.

Apologies that this seems like a long-reaching question. Multidimensional arrays still seem to confound me.

Edit: (question needs to be more clear)

Can you please suggest the best way that I would either explode or break up the array, and then do conditional modification to the data, or instead how I could remove unneeded data and then do conditional modification to the data.

My ultimate output here would be taking suitable female characters (not dead or pregnant already), and based on their age, giving them a chance at becoming pregnant. If true, I'd throw some code back at the SQL database to update that character.

Thanks!


All the things you need could probably get done with SQL :

Now I need to remove any characters who have a value for deathdate or ispregnant

Simply add some argument to your WHERE condition :

isPregnant IS NULL AND deathdate IS NULL

For instance I need to grab the borndate value, compare it to the current date to find age

Depending of your field format the maths could be done in SQL , have look to the DATE function of mysql

Don't underestimate the power of your sql server , 99% of the time it is probably faster than php to work on data set.


Instead if immediately removing some rows from your array, try limiting the data you recieve through SQL.

You can loop through your array like this:

foreach($femaledata as $female)
{
 echo $female['name'];
}


do you mean something like this?

          $femaledata = array();
          while ($row_user = mysql_fetch_assoc($result)) {
               $ok = false;
               // do you validation for every user 

               if($ok) array_push($femaledata,$row_user);
          }


TJHeuvel gave you the right answer, and you should accept that answer. However, to inform: multidimensional arrays need not confound. Let me see if I can explain.

In PHP, you can put any object at all into an array, including other arrays. So, let's say you have an array that contains other arrays. When you iterate over that array using a looping construct (usually a foreach loop), each iteration of the loop will give you another array; if you want to access the elements of this sub-array, just loop over it. This is called a nested loop. Example:

$r = array(
        array(1,2,3),
        array(4,5,6),
        array(7,8,9)
    );

foreach ($r as $cur)  {
    foreach ($cur as $num)  {
        echo $num;
    }
}

In each iteration of the outer loop, $cur contains an array; the inner loop iterates over contents of this array. This technique allows you to process arrays of any dimension.

However, in your specific case, you don't need to use an inner loop to iterate over your subarrays. You only need to access certain elements of your subarrays by their keys, rather that processing all of them in turn. So, a simple foreach loop will do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜