Array Help - Modifying parts of array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Greenconure
Forum Commoner
Posts: 30
Joined: Mon Jun 16, 2008 8:19 am

Array Help - Modifying parts of array

Post by Greenconure »

I need to store multiple combinations of an ID and a number within an array.
Right now I'm storing them by combining the two strings together with a "-" in the middle and then exploding when I need to access the contents.

My problem lies in that I don't know how to, when the user is adding a new ID/number, check if the ID already exists within the array and modify that entry so that the entry's number value = Old Number + New Number.

Any help?

Also, is there a better way to store the ID/number within an array than how I am currently doing this?

Thanks,
Greenconure

Array "print_r" Example:

Code: Select all

Array
(
    [0] => 1-123
    [1] => 1-100
)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: Array Help - Modifying parts of array

Post by Oren »

Why not a 2D array?
User avatar
Greenconure
Forum Commoner
Posts: 30
Joined: Mon Jun 16, 2008 8:19 am

Re: Array Help - Modifying parts of array

Post by Greenconure »

Oren wrote:Why not a 2D array?
That's quite a good point.
I'll most likely end up using an array like the following.
(But there still is the problem of modifying the individual values that I don't know how to do)

Array "print_r" Example:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => Item Number 1
            [1] => Quantity for Item Number 1
        )
 
    [1] => Array
        (
            [0] => Item Number 2
            [1] => Quantity for Item Number 2
        )
 
)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Array Help - Modifying parts of array

Post by RobertGonzalez »

Arrays can be used like little mini database tables. First question I have is why can you not do this with a database? If you can't, then what process are you trying to accomplish with your logic? Not the code process, but the application process. What is your app doing?
Post Reply