0

I would like to replace the , with . in some Keys like [Price] for instance.

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.

4
  • Thanks, can you take a look at the function I just posted? I'm close, but something's still not right Commented Nov 3, 2015 at 11:03
  • Instead of all that loops, why not use array_walk_recursive()? (check the comments to see how to change the array in the callback function) Commented Nov 3, 2015 at 11:04
  • 1
    As a side-note, you should really return the modified array from your function instead of sending the original by reference; your current method makes testing and troubleshooting very hard. Commented Nov 3, 2015 at 11:15
  • @Uchiha I tried this but it doesn't work, the output is still comma not dot. Commented Nov 3, 2015 at 11:17

5 Answers 5

3

You can iterate through each element of associative array using array_walk_recursive function.

Here $result is your input array.

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 .

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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
        )

)

Comments

0

Alternate

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

1 Comment

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
0

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($data, function(&$item, $key){
            
    if( $key == 'Price' ){
                
        $item = str_replace(',','.',$item);
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.