Page 1 of 1

**Array Problem** PLEASE HELP

Posted: Wed Aug 12, 2009 8:43 am
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.

Re: **Array Problem** PLEASE HELP

Posted: Wed Aug 12, 2009 8:50 am
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!)

Re: **Array Problem** PLEASE HELP

Posted: Wed Aug 12, 2009 8:56 am
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

Re: **Array Problem** PLEASE HELP

Posted: Wed Aug 12, 2009 7:31 pm
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.

Re: **Array Problem** PLEASE HELP

Posted: Thu Aug 13, 2009 2:24 am
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 :)

Re: **Array Problem** PLEASE HELP

Posted: Fri Aug 14, 2009 5:04 pm
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