1bigbear Posted October 23, 2009 Share Posted October 23, 2009 I am having problems converting this array to a string using print_r($val[1]); I get: Array ( [0] => Array ( [name] => John ) [1] => Array ( [name] => Dan ) [2] => Array ( [name] => Jim ) ) I have tried several methods but they don't work Quote Link to comment Share on other sites More sharing options...
smerny Posted October 23, 2009 Share Posted October 23, 2009 check out the implode function http://php.net/manual/en/function.implode.php Quote Link to comment Share on other sites More sharing options...
1bigbear Posted October 23, 2009 Author Share Posted October 23, 2009 How to use <?php $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname,email,phone ?> Quote Link to comment Share on other sites More sharing options...
1bigbear Posted October 23, 2009 Author Share Posted October 23, 2009 I want to use the data from $val[1][1] in another place but it needs to be a string Quote Link to comment Share on other sites More sharing options...
samoi Posted October 23, 2009 Share Posted October 23, 2009 foreach will be good catch! Quote Link to comment Share on other sites More sharing options...
1bigbear Posted October 23, 2009 Author Share Posted October 23, 2009 could you explain? Quote Link to comment Share on other sites More sharing options...
samoi Posted October 23, 2009 Share Posted October 23, 2009 <? $array = array('lastname', 'email', 'phone'); foreach($array as $key => $value){ echo $value; echo ', '; } ?> try this Quote Link to comment Share on other sites More sharing options...
samoi Posted October 23, 2009 Share Posted October 23, 2009 is it what you mean? Quote Link to comment Share on other sites More sharing options...
1bigbear Posted October 23, 2009 Author Share Posted October 23, 2009 what do I put instead of 'lastname', 'email', 'phone' I don't have that? Quote Link to comment Share on other sites More sharing options...
samoi Posted October 23, 2009 Share Posted October 23, 2009 Sorry I didn't understand exactly what you want to do with the array actually! do you want it to be something like keys and values? Or you need to import values inside the array so you can use those values as we just did? Quote Link to comment Share on other sites More sharing options...
1bigbear Posted October 23, 2009 Author Share Posted October 23, 2009 The array $val has this data Array ( [0] => Array ( [name] => John ) [1] => Array ( [name] => Dan ) [2] => Array ( [name] => Jim ) ) And I want to use the content of $val[1][1] that is Dan, but the script that I use needs a string not a array And I don't know how to convert the array $val[1][1] to string Quote Link to comment Share on other sites More sharing options...
samoi Posted October 23, 2009 Share Posted October 23, 2009 The array $val has this data Array ( [0] => Array ( [name] => John ) [1] => Array ( [name] => Dan ) [2] => Array ( [name] => Jim ) ) And I want to use the content of $val[1][1] that is Dan, but the script that I use needs a string not a array And I don't know how to convert the array $val[1][1] to string Well, I think you have a mistake ! so, <? $users = array('John', 'Dan', 'someone'); echo $users[1][1]; // will give you the litter a of the user d*A*n! echo $users[1]; // will give you dan! // So you don't need extra index [1] for $val ! // just use $val[1], then it will give you the string of the index 1 ! // what you actually doing is adding an index to the index ! so it's starting to pick letter as index of the index name! ?> Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted October 24, 2009 Share Posted October 24, 2009 <?php $array[0]['name']='John'; $array[1]['name']='Dan'; $array[2]['name']='Jim'; $string=''; foreach($array as $subarray){ $string.=$subarray['name'].','; } $string=rtrim($string,','); echo $string; ?> OR this way... <?php $array[0]['name']='John'; $array[1]['name']='Dan'; $array[2]['name']='Jim'; for($i=0;$i<count($array);$i++){ $names[]=$array[$i]['name']; } $string2=implode(',',$names); echo $string2; ?> im sure there are many ways Quote Link to comment 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.