saurabh kamble
saurabh kamble

Reputation: 1549

explode associative array in php and adding two fields in the array

output of form:

Array (
    [0] => (19.979802102871425,73.78671169281006)
    [1] => (19.97978382724978,73.78682289971039)
    [2] => (19.979765551626006,73.78697712672874) 
)

expect output:

Array (
    [0] => 
          [lag]=>19.979802102871425,
          [long]=>73.78671169281006,   
    [1] => [lag]=>19.97978382724978,
           [long]=>73.78682289971039
    [2] => [lag]=>19.979765551626006,
             [long]=>73.78697712672874 
)

following code working is not given me desired output as i am unaware of adding two associate element in array

   foreach($_POST['textbox'] as $value):
        $value = str_replace(array('(',')'), '',$value);
       foreach (explode(',', $value) as $topic) : 
           list($name, $items) =$topic;  
         ///stuck up here   
        }
             $test[] = explode(',', $value);
       endforeach;                            
     endforeach;

could please suggest the changes in the code as i reffered following links but the expected out is different from mine.thanks in advance php-explode-and-put-into-array explode-function

Upvotes: 0

Views: 222

Answers (2)

Fky
Fky

Reputation: 2173

Try this :

 foreach($array as $value){
         list($lat,$lng) = explode(',',trim(trim($value,')'),'('));
         $out[] = array( 'lat' => $lat , 'lng' => $lng);
 }
 var_dump($out);

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

You can try this -

foreach($your_array As &$array) {
   $temp = explode(',', str_replace(array('(', ')'), '', $array)); // replace the ()s & explode the value
   $array = array('lat' => $temp[0], 'long' => $temp[1]); // store with keys
}

Upvotes: 1

Related Questions