unemployment Posted May 4, 2011 Share Posted May 4, 2011 I have an array and I would like to insert commas and the word 'and' to make a sentence using an array. For example... $sports = array(baseball, soccer, tennis); Ted played baseball, soccer and tennis or Ted played baseball, soccer, boxing and tennis What's the best way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/ Share on other sites More sharing options...
unemployment Posted May 4, 2011 Author Share Posted May 4, 2011 Also, I want to combine two arrays so that they array keys match. For example... $sports = array(soccer, basketball, tennis); $locations = array(New York, London, Paris); Ted plays soccer in New York, Basketball in London and Tennis in Paris. How do you compare arrays like that to create this sentence structure? Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/#findComment-1210345 Share on other sites More sharing options...
fugix Posted May 4, 2011 Share Posted May 4, 2011 is that the only three values that will be in the array or is it dynamic? Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/#findComment-1210346 Share on other sites More sharing options...
priti Posted May 4, 2011 Share Posted May 4, 2011 one way is $sports = array(baseball, soccer, tennis); $str = implode(",",$sports); $pos = strrpos($str,','); $final_str = substr($str,0,$pos).' and '.substr($str,($pos+1),strlen($str)); echo $final_str; there may be other ways also... Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/#findComment-1210348 Share on other sites More sharing options...
priti Posted May 4, 2011 Share Posted May 4, 2011 for Ted plays soccer in New York, Basketball in London and Tennis in Paris. you may need to iterate both array and generate the required string Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/#findComment-1210349 Share on other sites More sharing options...
unemployment Posted May 4, 2011 Author Share Posted May 4, 2011 is that the only three values that will be in the array or is it dynamic? It's a dynamic array or in example two they are both dynamic arrays Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/#findComment-1210354 Share on other sites More sharing options...
unemployment Posted May 4, 2011 Author Share Posted May 4, 2011 for Ted plays soccer in New York, Basketball in London and Tennis in Paris. you may need to iterate both array and generate the required string Can you please give me an example. I'm a bit new to this whole array manipulation. Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/#findComment-1210356 Share on other sites More sharing options...
fugix Posted May 4, 2011 Share Posted May 4, 2011 to match the arrays by key, you could use array_merge_recursive() Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/#findComment-1210361 Share on other sites More sharing options...
unemployment Posted May 4, 2011 Author Share Posted May 4, 2011 to match the arrays by key, you could use array_merge_recursive() I tried working with you suggestion, but I just can't seem to get it to work the way I need it to. This is a print out of two of my arrays Array ( [0] => stageofdevelopment [1] => companylocation ) Array ( [0] => [1] => New City, South Africa ) This is a print out of the merge Array ( [0] => stageofdevelopment [1] => companylocation [2] => [3] => New City, South Africa ) This is the function I used for the merge function my_array_merge ($arr,$ins) { if(is_array($arr)) { if(is_array($ins)) foreach($ins as $k=>$v) { if(isset($arr[$k])&&is_array($v)&&is_array($arr[$k])) { $arr[$k] = my_array_merge($arr[$k],$v); } else { // This is the new loop while (isset($arr[$k])) $k++; $arr[$k] = $v; } } } elseif(!is_array($arr)&&(strlen($arr)==0||$arr==0)) { $arr=$ins; } return($arr); } This is what I want...or at least what I think I want. Array ( [0] => Array ( [0] => stageofdevelopment [1] => ) [1] => Array ( [0] => companylocation [1] => New City, South Africa ) ) Please help... Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/#findComment-1210379 Share on other sites More sharing options...
priti Posted May 5, 2011 Share Posted May 5, 2011 $arr1=array('stagedevelopment','companylocation'); $arr2=array('','New City, South africa'); echo '<pre>'; print_r($arr1);print_r($arr2); $arr[]=$arr1; $arr[]=$arr2; print_r($arr); Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/#findComment-1210806 Share on other sites More sharing options...
priti Posted May 5, 2011 Share Posted May 5, 2011 Also for the previous problem $common1=array_combine($sports,$location); function print_arr($common1) { $final_str=''; $callback =function($value,$key) use (&$final_str) { $final_str .= $key.' in '.$value.','; }; array_walk($common1,$callback); return $final_str; } $final_output=print_arr($common1); echo $final_output; you may need to tweak a little to append 'and' Quote Link to comment https://forums.phpfreaks.com/topic/235502-manipulate-array-output/#findComment-1210811 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.