Jump to content

Finding a variable in an array


Go to solution Solved by benanamen,

Recommended Posts

I have an array of $my_heroes = ("Batman", "Superman", "Spiderman", "Hulk");

 

Note that this array will expand over time as more 'Heroes' are added.

 

Each Hero is to have a value added, depending on several things, and then sorted at the end.  My thinking is to have a 2 Dimensional array.

 

So my question is how do I change my array into a 2D array and then add a value, so for example after round one of fighting I wish to give Batman 10 points, Superman 15 points and Hulk 20 points.

 

I have tried

$$my_heroes[0] += 10;

but this turned out to be a disaster.

 

So the end result I want is

$my_heroes_results[0][0]="Batman"; $my_heroes_results[0][1]=10;

$my_heroes_results[1][0]="Superman"; $my_heroes_results[1][1]=15;

$my_heroes_results[2][0]="Spiderman"; $my_heroes_results[2][1]=0;

$my_heroes_results[3][0]="Hulk"; $my_heroes_results[3][1]=20;

 

Then I can sort them into order.

Link to comment
https://forums.phpfreaks.com/topic/306847-finding-a-variable-in-an-array/
Share on other sites

  • Solution

You mean like this?

$my_heros = [
              'Batman' => 0
            , 'Superman' => 0
            , 'Spiderman' => 0
            , 'Hulk' => 0
            ];

$my_heros['Batman'] = 10;
$my_heros['Superman'] = 15;
$my_heros['Hulk'] = 20;
asort($my_heros);// Sorted by Points  

echo '<pre>';
print_r($my_heros);
echo '</pre>';

Result:

 

Array
(
[spiderman] => 0
[batman] => 10
[superman] => 15
[Hulk] => 20
)

Edited by benanamen
  • Like 1

a) you should be using a database for this, it will be much easier to handle new data and get the results you want, and b) even if not using a database, your data should be normalized and have an audit trail, where each different type of data is stored in its own array/table and each transaction/round that affects a value is a separate entry in the array/table, not just a number that gets updated.

 

you would have a hero array/table that defines the hero names and assigns them unique ids.

 

the points should be a separate array/table with a hero id, a round number, and point value. to get the total points for any/all heroes, you would sum up the point values for each hero id. if you want the results sorted by the total points, you apply the array sort function to the result array holding the sums. all pretty much the same way that a database engine would do this based on an sql query.

Thanks Benanamen, that's excactly what I'm after.

 

This code is working out ratings based on what is in the database, so all the data has been stored, I now have to work out the results.  This is part of a much bigger project so I need to be able to integrate the data with other parts which this array has done.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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