**Array Problem** PLEASE HELP

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
adiosamigos
Forum Newbie
Posts: 8
Joined: Sat Jun 27, 2009 3:52 am

**Array Problem** PLEASE HELP

Post by adiosamigos »

Hi i have a php script that gets and associative array from mysql.

I need a function that converts it to a string. I need it so that the key and the value is separated by a "'-" and then those are separated by a ";"

So it outputs like this:

grams-1;ounces-3;slices-4;

I have spent hours trying to do this but can't. Will someone please point me in the right direction.

Thanks, Tom.
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: **Array Problem** PLEASE HELP

Post by robnet »

You probably just need a foreach:

Code: Select all

$string='';
foreach($array as $key => $value) {
 $string.= "$key-$value;";
}
If you need more info can you post a print_r($array) output (Or part of it if's big!)
mars_rahul
Forum Newbie
Posts: 12
Joined: Thu Aug 06, 2009 12:22 am

Re: **Array Problem** PLEASE HELP

Post by mars_rahul »

Hello Friend, this part of forum is not to make ready-made solution for you. If u need such function better post such threads to Job Hunts Page.

So about your problem, will you give us a piece of code on which you are currently working so that we can understand what do you mean. Isn't it?

I am right guys?

Rahul -IT Professional at Lumbini Bank Limited, Nepal
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: **Array Problem** PLEASE HELP

Post by aceconcepts »

@mars_rahul

Tom's post seems genuine enough and honest enough. It's often hard to determine whether someone is trying to get code written for them or whether they're truly stuck. At the end of the day, if the person comes back again and again asking the same of us then we can assume they're taking the mickey. Alternatively, write their code by pseudo style.

@tom
Anyhow, that foreach suggestion sounds about right. You seem to know what you're talking about regarding your logic.
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: **Array Problem** PLEASE HELP

Post by robnet »

@aceconcepts Agreed. Tom asked to be pointed in the right direction rather than to be given the code. I figured it was a quick request and writing the fairly generalised foreach function was easier and just as useful to learn from as pointing to a manual or anything else which would have taken me longer anyway :)
adiosamigos
Forum Newbie
Posts: 8
Joined: Sat Jun 27, 2009 3:52 am

Re: **Array Problem** PLEASE HELP

Post by adiosamigos »

Thanks guys that was really helpful; worked a treat.

I fully appreciate what you are saying but I had no idea what I was looking for whether it was a specific function i needed to search for like implode or just a keyword someone could give me so i thought a quick question might help me.

I'd come across 100+ line long scripts for doing this and figured there must be a quicker way.

Thanks a lot :)
Tom
Post Reply