Jump to content

Array help


Canman2005

Recommended Posts

Hi all

 

I have an array set under

 

$_SESSION['items']

 

so if I do a

 

print_r($_SESSION['items']);

 

then I get

 

( [1] => 4 [45] => 2 )

 

what I want to do is implode that array so that I get an output that looks like

 

1,45

 

but I cannot figure it out.

 

If I use

 

print implode(",", $_SESSION['items']);

 

then that outputs

 

4,2

 

but I want it to output

 

1,45

 

instead

 

any ideas?

 

thanks

 

ed

Link to comment
https://forums.phpfreaks.com/topic/197402-array-help/
Share on other sites

coooool, thanks for that everyone

 

I wonder if you could help me further

 

Is it possible to pull a single value from an array?

 

Let me explain.

 

My array currently looks like

 

( [1] => 4 [45] => 2 )

 

so with the above

 

45 is linked to 2  ([45] => 2)

 

and

 

1 is liked to 4  ([1] => 4)

 

so is it possible for example, to supply var such as

 

$var = 45

 

and run something so that it grabs the value

 

2

 

or if I supplied

 

$var = 1

 

then it would allow me to grab value

 

4

 

any help would be fab

 

thanks all

Link to comment
https://forums.phpfreaks.com/topic/197402-array-help/#findComment-1036135
Share on other sites

yes, just use the square bracket operator to supply whatever index you want

$key = 45
echo $_SESSION['item'][$key];//echos 2

//store the 2 from $_SESSION['item'] in a variable
$two = $_SESSION['item'][$key];

//alternatively, this will also work
$two = $_SESSION['item'][45];

 

Link to comment
https://forums.phpfreaks.com/topic/197402-array-help/#findComment-1036139
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.