Canman2005 Posted April 2, 2010 Share Posted April 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/197402-array-help/ Share on other sites More sharing options...
ialsoagree Posted April 2, 2010 Share Posted April 2, 2010 Try... $output_string = ''; $start = TRUE; foreach ($_SESSION['items'] as $key => $value) { if (!$start) $output_string .= ', '; else $start = FALSE; $output_string .= $key; } // Do whatever you want with $output_string Quote Link to comment https://forums.phpfreaks.com/topic/197402-array-help/#findComment-1036129 Share on other sites More sharing options...
teamatomic Posted April 2, 2010 Share Posted April 2, 2010 $imp=implode(",",array_keys($array)); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/197402-array-help/#findComment-1036131 Share on other sites More sharing options...
mikesta707 Posted April 2, 2010 Share Posted April 2, 2010 or just do $keys = array_keys($_SESSION['items']); $str = implode($keys, ','); array_keys(): http://php.net/manual/en/function.array-keys.php dam teamatomic beat me to the punch. but posting anyways for manual link Quote Link to comment https://forums.phpfreaks.com/topic/197402-array-help/#findComment-1036132 Share on other sites More sharing options...
Canman2005 Posted April 2, 2010 Author Share Posted April 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/197402-array-help/#findComment-1036135 Share on other sites More sharing options...
mikesta707 Posted April 2, 2010 Share Posted April 2, 2010 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]; Quote Link to comment https://forums.phpfreaks.com/topic/197402-array-help/#findComment-1036139 Share on other sites More sharing options...
Canman2005 Posted April 2, 2010 Author Share Posted April 2, 2010 thats guy, i'm basically tying to split up my array so I can do a "SELECT" query from the database. All works great, thanks Quote Link to comment https://forums.phpfreaks.com/topic/197402-array-help/#findComment-1036154 Share on other sites More sharing options...
Canman2005 Posted April 2, 2010 Author Share Posted April 2, 2010 thats guy, i'm basically tying to split up my array so I can do a "SELECT" query from the database. All works great, thanks Quote Link to comment https://forums.phpfreaks.com/topic/197402-array-help/#findComment-1036162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.