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 Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/ 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 Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943000 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 ?> Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943004 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 Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943018 Share on other sites More sharing options...
samoi Posted October 23, 2009 Share Posted October 23, 2009 foreach will be good catch! Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943020 Share on other sites More sharing options...
1bigbear Posted October 23, 2009 Author Share Posted October 23, 2009 could you explain? Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943023 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 Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943026 Share on other sites More sharing options...
samoi Posted October 23, 2009 Share Posted October 23, 2009 is it what you mean? Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943027 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? Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943028 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? Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943033 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 Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943042 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! ?> Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943062 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 Link to comment https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/#findComment-943267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.