简体   繁体   English

str_replace 多维数组中的特定值

[英]str_replace specific value in multidimensional array

I would like to replace the , with .我想将,替换为. in some Keys like [Price] for instance.例如,在[Price]等某些键中。

Given this array:给定这个数组:

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
        )
)

And the following function:以及以下功能:

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);
            }
        }
    }   
}

Can someone help me please?有人能帮助我吗? I tried a couple of things but can't get it to work.我尝试了几件事,但无法使其正常工作。

You can iterate through each element of associative array using array_walk_recursive function.您可以使用array_walk_recursive函数遍历关联数组的每个元素。

Here $result is your input array.这里$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) to check the output after replacing , with . var_dump($result)用 . 替换,后检查输出.

Change the main array, like this:更改主数组,如下所示:

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);
            }
        }
    }   
}

In casu you wanted to do only on a set of keys, you would need to declare those keys and use this other function:如果您只想对一组键进行操作,则需要声明这些键并使用其他功能:

$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);
            }
        }
    }   
}

You can try this:你可以试试这个:

$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);

Output:输出:

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

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

)

Alternate备用

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

Via array_walk_recursive if you want to change the values of an existing multi-dimensional array you need to specify the first argument of the callback as a reference by preceding the variable with an ampersand (&)通过array_walk_recursive如果要更改现有多维数组的值,则需要通过在变量前面加上 & 来将回调的第一个参数指定为引用

array_walk_recursive($data, function(&$item, $key){
            
    if( $key == 'Price' ){
                
        $item = str_replace(',','.',$item);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM