在php中爆炸关联数组并在数组中添加两个字段

时间:2015-09-21 08:38:47

标签: php arrays

表单输出:

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

期望输出:

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

以下代码工作没有给出我想要的输出,因为我不知道在数组中添加两个关联元素

   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;

可以请求建议代码中的更改,因为我在链接后提到但是预期的输出与mine.thanks提前不同 php-explode-and-put-into-array explode-function

2 个答案:

答案 0 :(得分:1)

你可以试试这个 -

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
}

答案 1 :(得分:0)

试试这个:

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