Forum Moderators: coopster

Message Too Old, No Replies

making array into string value?

         

Dreap

9:41 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



Lets say I have

$array[1] or $array['data']
with data 20 50 30 80
Now I want to make that array into string somehow.

$string = $array[1]

this one doesnt work how to get it to work?

Matthew1980

9:48 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Dreap,

Are you just trying to echo a keys of an array all in one?

a bit convoluted but this could do you:-

$string = $array['0'].$array['1'].$array['2'].$array['3'];

echo $string;

though there are better ways, that will do what you as though, I'm sure there will be more than one suggestion ;-p

Dreap

9:58 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



Thank You!

how can I echo more then one string at once?
echo $username $something;
that gives me error

[edited by: Dreap at 10:17 pm (utc) on Mar 17, 2010]

Matthew1980

10:14 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Dreap,

as a quick fix:-

echo $username;
echo "<br/>";
echo $something;

or join them:-

echo $username.$something;

or join them with something else:-

echo $username."&nbsp;some text in the middle&nbsp;".$something;

or in html:-

<div>
hi my name is<?php echo $myname;?> and my fave colour is <?php echo $color;?><br/>this is on a new <?php echo $pet;?> line
</div>


Hope that's clearer now ;-p

Cheers,
MRb

Dreap

10:18 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



Thanks once again. :)

rocknbil

11:49 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



?

spaces

$string = implode(' ',$arrayname);

no spaces

$string = implode(' ',$arrayname);

Combining strings:

echo "$string1 $string2";

:-)

Dreap

12:05 am on Mar 18, 2010 (gmt 0)

10+ Year Member



Any possible way to make one string out of 4 strings?

$string1 = "value1"
$string2 = "value2"
$string3 = "value3"
$string4 = "value4"

Need to make one string out of them separated by spaces.

$stringtogether = "value1 value2 value3 value4"

Readie

12:08 am on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you could echo them inside double quotes like Rocknbil has above, or you could concatonate them like so:

$combined = string1 . $string2 . $string3 . $string4;

Dreap

12:14 am on Mar 18, 2010 (gmt 0)

10+ Year Member



I asked before I tried xD
$stringtogether = "$string1 $string2 $string3 $string4" ;

worked too. Thanks for quick reply tho :))

Dreap

12:29 am on Mar 18, 2010 (gmt 0)

10+ Year Member



any idea how to sort echo?

If I want to show list of usernames and their budget for example.

lets say there are four users
Ann with 500$
Mary with 200$
Dave with 50$
Brandon with 600$

$string1 = "Ann"
$string2 = "Mary"
$string3 = "Dave"
$string4 = "Brandon"

$string5 = "500"
$string6 = "200"
$string7 = "50"
$string8 = "600"

now

I would need to sort users who have more in buget to be sorted to top of page.

Result should be.

Brandon 600
Ann 500
Mary 200
Dave 50

Readie

12:37 am on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you could make an array with the key being the name. arsort (array sort (keeps key => value integrity) - descending) them, and then foreach loop through them

$nameBudget = array(
'Ann' => '500',
'Mary' => '200',
'Dave' => '50',
'Brandon' => '600'
);

arsort($nameBudget);

foreach($nameBudget as $name => $budget) {
echo '<br />' . $name . ' ' . $budget;
}

And I'm afraid this is the last post from me tonight. My bed is calling me :).

Matthew1980

8:43 am on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rocknbil; Readie,

Just shows sometimes that you should not post when too tired! I completely forgot about: $string = implode(' ',$arrayname); But concatonation is just my preferred method of joining ;-p

Cheers for pointing the obvious to me.
MRb