str_replace多维数组

时间:2015-11-03 11:00:59

标签: php arrays multidimensional-array str-replace

我想将,替换为某些密钥中的.,例如[Price]

鉴于此数组:

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);
            }
        }
    }   
}
是的,有人能帮帮我吗?我试了几件但是无法让它发挥作用。

4 个答案:

答案 0 :(得分: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);
            }
        }
    }   
}

在casu中你想只对一组键做,你需要声明这些键并使用另一个函数:

$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 :(得分: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)

替换,后,

.检查输出

答案 2 :(得分: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
        )

)

答案 3 :(得分:0)

备用

array_walk_recursive($r, function (your args){
});