2015-11-03 126 views
0

我想用.代替,,例如[Price]str_replace多维数组中的特定值

鉴于此阵:

Array 
(
    [0] => Array 
     (
      [Product line] => Misc 
      [Seller] => aaa.com 
      [Tracking ID] => bbbb 
      [Date shipped] => October 23, 2015 
      [Price] => 60,43 
      [Referral fee rate] => 3,00%     
      [Quantity] => 2 
      [Revenue] => 120,86 
      [Earnings] => 3,62 
      [Sub Tag] => xxxx 
     ) 

    [1] => Array 
     (
      [Product line] => Misc 
      [Seller] => aaaa.com 
      [Tracking ID] => bbbb 
      [Date shipped] => October 23, 2015 
      [Price] => 9,34 
      [Referral fee rate] => 6,96%     
      [Quantity] => 1 
      [Revenue] => 9,34 
      [Earnings] => 0,65 
      [Sub Tag] => xxxx 
     ) 
) 

而且有以下功能:

function str_replace_specific_value($sSearch, $sReplace, &$aSubject){ 
    foreach($aSubject as $sKey => $uknValue) { 
     if(is_array($uknValue)) { 
      foreach($sKey as $fKey => $fuknValue) { 
       $uknValue['Price'] = str_replace($sSearch, $sReplace, $fuknValue); 
      } 
     } 
    } 
} 

有人能帮助我吗?我尝试了一些东西,但无法使其工作。

+0

谢谢,你可以去看看我刚刚发布的功能?我很接近,但有些东西仍然不正确 – cliff

+0

而不是所有的循环,为什么不使用['array_walk_recursive()'](http://php.net/array_walk_recursive)? (检查注释以查看如何更改回调函数中的数组) – kero

+1

作为一个侧面提示,您应该真正从函数返回修改后的数组,而不是通过引用发送原始数据;您目前的方法使测试和故障排除非常困难。 – jeroen

回答

0

您可以使用array_walk_recursive函数遍历关联数组中的每个元素。

这里$result是你的输入数组。

array_walk_recursive($result, 'replacer'); 

/** 
* Replace comma with dot from 'Price' element of associative array. 
* This function call recursively 
* 
* @access public 
* 
* @param string|int|null $item 
* @param string $key 
* @return void 
*/ 
public function replacer(& $item, $key) 
{ 
    if ($key == 'Price') { 
     $item = str_replace(",", ".", $item); 
    } 
} 

var_dump($result).

+0

非常感谢,这解决了我的问题。似乎也是一种有效的方法。刚刚将错字从'$ item = str_replace(“,”,“。:,$ item);'改为'$ item = str_replace(”,“,”。“,$ item);' – cliff

+0

@cliff谢谢,我已更新代码 –

1

更改主阵列,像这样:

function str_replace_specific_value($sSearch, $sReplace, &$aSubject){ 
    foreach($aSubject as $key => $sub_array) { 
     if(is_array($sub_array)) { 
      foreach($sub_array as $sub_key => $sub_value) { 
       $sSubject[$key][$sub_key] = str_replace($sSearch, $sReplace, $sub_value); 
      } 
     } 
    } 
} 

此处是指应你想只在一组键做的,你就需要声明那些键,并使用这个其他功能:

$keys_to_be_replaced = ['price','whatever']; 

function str_replace_specific_value($sSearch, $sReplace, &$aSubject, $keys_to_be_replaced){ 
    foreach($aSubject as $key => $sub_array) { 
     if(is_array($sub_array)) { 
      foreach($sub_array as $sub_key => $sub_value) { 
       if(in_array($sub_key,$keys_to_be_replaced)) 
        $sSubject[$key][$sub_key] = str_replace($sSearch, $sReplace, $sub_value); 
      } 
     } 
    } 
} 
+1

感谢您的输入vivoconunxino,我决定去@Devendra Bhandari的方法 – cliff

0

更换,你可以试试这个后检查输出:

$arr[0] = array("price" => "60,53"); 
$arr[1] = array("price" => "9,34"); 

foreach ($arr AS $key => $value) { 
    $arr[$key]["price"] = str_replace(",", ".", $arr[$key]["price"]); 
} 
echo "<pre>"; 
print_r($arr); 

输出:

Array 
(
    [0] => Array 
     (
      [price] => 60.53 
     ) 

    [1] => Array 
     (
      [price] => 9.34 
     ) 

) 
+0

感谢您的意见,我决定采用@Devendra Bhandari的方法 – cliff