PHP: How to add integers only from strings sorted

Hello

I thought a lot about how it, but not have made any progress in my efforts.

I have a few strings to form that are displayed in the variables when a form is submitted:

String 1 = "Mark (2)"

String2 = "Chris (5).

String 3 = "Mark (5).

Channel 4 = 'Angie (5).

String 5 = "Chris (1).

A shape is I want all integers to add for each person.

So my final result:

Brand = 7

Chris = 6

Angie = 5

I don't know how to use php to add only integers of the variables that have the same names in them.

Any suggestions?

Chris

It is essentially the construction of a database table that is structured as follows (the name is the key, score is the value):

Table)

[Mike]-online 1.

[Chris]-online 8.

[Mark]-online 9

)

As he travels the elements of the parsed string, it first checks if there is already an entry for the name if found. If this isn't the case, it creates one. If there is already an entry corresponding to the name, it adds the new score to what is already there.

Here is another example of script where I removed the second function, that I used for the test output, and instead, it sends the results to variables. If it finds a key in the table corresponding to the name (it is case sensitive), and then it retrieves the score, otherwise he attributes a score of 0. I also added in the lines of comments explaining what is happening. If you need something explained, just post back. Here's the updated code:

<>

function to parse strings of name/stat

function parseNameStats ($string, & $stats)

{

separate the string into groups, separated by commas

$sets = explode (",", $string);

Browse the main groups

foreach ($sets as $subject)

{

divide the substring of parenthesis to separate the name of partition

$temp = explode ('(', $subject);

If (count ($temp) == 2)

{

first part must be a name, so trim whitespace

$name = trim($temp[0]);

second part is score, so create stat entry to add to the existing

$int = (integer) trim($temp[1]);

If ($name & $int)

{

If (! array_key_exists ($name, $stats)) $stats [$name] = $int;

else $stats [$name] = $int;

}

}

}

}

do it for her

$stats = array();

parseNameStats ('Mike (1), Chris (2), Mark (5)', $stats);

parseNameStats ('Chris (5)', $stats);

parseNameStats ('Chris (1), 4 ', $stats);

assign scores to variables

$mikeScore = (array_key_exists ('Mike', $stats))? $stats ['Mike']: 0;

$markScore = (array_key_exists ('Brand', $stats))? $stats ['brand']: 0;

$chrisScore = (array_key_exists ('Chris', $stats))? $stats ["Chris"]: 0;

output test

print ("Mike: '.") $mikeScore.', Mark: '. $markScore.', Chris: '. $chrisScore);

?>

Tags: Dreamweaver

Similar Questions

  • My bank site refused my log in because of 'old' browser, but I don't want to upgrade the next operating system (Yosemite) because he ruined my computer before. How can I upgrade only from Safari?

    My bank site refused my log in because of 'old' browser, but I don't want to upgrade the next operating system (Yosemite) because he ruined my computer before. How can I upgrade only from Safari?

    Safari cannot be updated past 6.2.8 on Mountain Lion. If you want to, switch browsers.

    (144681)

  • How to add listbox form in string array objects

    Hello

    I need help related to listbox

    1. currently, I am able to select and implement string in array

    Prob is

    ex.

    When I select the point 1 and 1 point will be created on arrayString but when I select article 2 and article 3 then arrary string poster article 2 and article 3 and article 1 is deleted.

    So please let me know how to add, add and delet listbox to string in array.

    2. how popup listbox and allow the user to add, delet or add the items in the listbox control and the transfer to the string table

    I have my code atatched. Please help me.

    Thank you

    Clement

    Sorry, try this:

  • How to add Admin master from a different directory server profile: OAM

    Hello

    I installed OAM and gave the base OU where all users reside in a domain in AD. After that, I added another area where another set of users are stored. How to add users of this directory server second to MasterAdmin in OAM?

    Thank you

    Hello

    It seems that you have a problem with the searchbase.

    Please try these steps-

    1 Add a profile directory OAM for the new AD domain server. Now you should be able to connect to User Manager / Group Manager as users of the new domain, but it must refuse to let you access the admin console - the user is not added as administrators. When you connect to OAM, you must choose one of the two available areas / profiles.
    2. Add a disjoint searchbase so that Identity server is able to search users in the new domain.
    3. connect to the existing Administrator console system and add new directors to the new domain.

    Please let me know if it solves the problem or you need more details.

    Kind regards
    Christelle

  • How to add audio files from iTunes to Windows Media Player library

    Downloaded my first song from i-tunes, but cannot get ' in 'my music library from windows media. " How to do it here?

    I have an existing library of songs on my windows media played, but I must just have a song I hear and purchased from i-tunes. Unfortunately, I can't seen so he could copy/move etc from my files of i-tunes on my media files on windows. How can I do this? I realize that most people are going in the opposite direction and can find a lot of help to get my songs from windows to i-tunes, but I'm rather uncomfortable with windows media player and relatively late as I have nor have an i - pod, or plan to get one.

    Any help you can offer would be much appreciated.

    Thank you

    farmgirl92

    Downloaded my first song from i-tunes, but cannot get ' in 'my music library from windows media. " How to do it here?

    Drag and drop the file in your "Music" folder

  • How to add photos only when someone click mouse in the box

    As in the thread.

    I want my picture that someone will see only when click a box on my site.

    You can create objects on the page, and then set the States, for example, insert a rectangle and set reports with the mouse to the bottom of the State, so that once the user clicks the rectangle, image used for the mouse to the bottom of the State will be displayed on the page.

    If this isn't what you are looking for, please let me know.

    Thank you

    Sanjit

  • How to add (remove) items from the context Menu in Windows XP

    I want that some programs appear in the OPEN WITH context menu. This allows me to choose another in the associated program. I don't want to change the association, and I don't want to search the program folder programs whenever I need.
    morphingstar

    Right click on a file you want to modify the 'open with'.
    Click 'Open with' and "Choose program."
    Do not forget that you uncheck "Always use this program to open this type of file" except default if you want to use this program each time.

    Select the program you want to use, if it is not listed, then click Browse and navigate to the program. And then click OK.
    The new program now appears when you right click on the file or program.

    Please let us know if that helps.

  • How to add user from another computer as a Local user for file sharing permissions

    Hello, I'll put up for two computers to share files on a network.

    I just need to figure out how to add a user from another computer as a local user, I can change its permissions of file sharing.

    Here's where I'm stuck:

    I am trying to add a new user, but:

    How can I add a user somewhere else on my network to this list?

    I tried to add one here in the computer management:


    How can I add my network user?  I tried to do it in this format:

    Thank you!

    Sure thing. You can add a list in the registry with the name of the account that you want to hide from the login screen.  This site explains how: http://www.tech-recipes.com/rx/6222/windows-7-vista-xp-hide-user-account-from-welcome-screen-login-screen/

    Windows XP and beyond.

  • Cannot add or subtract from the choice of backup discs

    I can't find how to add or subtract from the disks to use for storing upOne of my disks (3) is complete and I want to use new selecta. the preferences window is not resized

    IN aid, he said:
    «If you already set up Time Machine, your backup disks are listed, and information on each disc of the backup is displayed.» To select, add, or remove backup disks, click Select Disk or add or remove the backup disc. »

    I don't see this option in preferences:

    Simon,

    Scroll down in the window pane to display the add/remove disk backup option:

  • How to add a new printer

    Firefox has several options of the printer, but not the one I need to use at home. I just need to know how to add a printer from the drop-down list. Thank you!

    Nevermind, I figured it. Thanks anyway!

  • How to add all THE tabs not only the current criteria

    Hello

    I want to be able to bookmark ALL the sites I've visited. I know CTRL + D and SHIFT + CTRL + D and right mouse clicking on a tab and choose bookmark all tabs. What I'm looking for, it's mark recursively. Here is an explanation:

    I start Firefox and load the page from my home.
    I'll tell site Google.com.
    On the same tab, I leave Google and go to Microsoft.
    On the same tab, I leave Microsoft and go to Mozilla.org
    On the same tab, I'm leaving, I leave Mozilla and go to CNN

    So, if I right-mouse-click the Go Back button, I see all these pages in order of the last went to the first visit.

    How to add all these pages of homepage-> Google-> Microsoft->... all the way

    I hope this makes sense and that a solution is there.

    Waseemn

    If you open the page in different tabs, then you use "bookmark all tabs" in the context menu of the tab bar.

    You can middle-click on a link to open the link in a new tab or hold down the CTRL key and click to the left.

    ALT + Enter opens links through the address bar in a new tab.

  • I don't know how to add additional devices to my list of "trusted devices" in my Apple account.  When I opened my account on Apple using my apple id, I see only my iphone6 listed as a "trusted device";  There is a button to edit, for example. Delete the d

    I don't know how to add additional devices to my list of "trusted devices" in my Apple account.  When I opened my account on Apple using my apple id, I see only my iphone6 listed as a "trusted device";  There is a button to edit, for example. deleting devices, but I can't, or I do not understand how to add my other devices to my approved devices - but is anyone know how to add my Mac and iPad to this list?

    Download a purchase on a device should associate this device with your Apple ID take a look at this.

    View and delete devices in iTunes - Apple Support

  • I'm trying to get audio only from a video. I went to record audio only: (as shown) and there is no ' audio only ' click. do I have to upgrade the machine to live music, and if so, how?

    I'm trying to get audio only from a video. I went to record audio only: (as shown) and there is no ' audio only ' click. do I have to upgrade the machine to live music, and if so, how?

    If you are looking to get audio only from a YouTube video, all you need to do is read your video and while it's going to copy the link. Then on another tab, open listentoyoutube.com. Just follow the easy directions and paste the link.

    Hope that helps. I found out a month ago and I have 4 CD made from these audio files. There is a lot of fun to find obscure stuff and making CD of songs.

    Jon

  • Item number: 314481 how to manually remove programs from the add tool / remove program does not work! What's next?

    Tried to uninstall a program using the Add / Remove program.  The tool has requested a copy of 'Install the CD' I did not.  Then I tried to uninstall the program by following the instructions in the Article ID: 314481 how to manually remove programs from the add tool / remove program.  It didn't not t work!  The name of the program calmed listed in the tool of installed programs list even if the "Delete" button is no longer visible.   The registry key and its subkeys appear to have been deleted but still the program name in the list of installed programs tool.  WHAT IS PLANNED?

    Some people have good results with the free copy of Revo Uninstaller that you can get from here:

    http://www.revouninstaller.com/index.html

  • How can I add a network from Windows XP to Windows 7 network. I use a Linksys WRT54G2 router.

    How can I add a network from Windows XP to Windows 7 network.  I use a Linksys WRT54G2 router.

    Try the method here...

    http://www.howtogeek.com/HOWTO/15247/complete-guide-to-networking-Windows-7-with-XP-and-Vista/

Maybe you are looking for