2

I am doing some array combine in php. But while combining/mapping the key of one array to value of another array i want to do some string replace in the value of the array which is going to be combined. How to use str_replace along with array_combine in php. Mostly without forloop.

Eg:

$a1 = array("35","37","43");
$a2 = array("Testing's", "testing's", "tessting's");

Normal combine is like below, (i.e) removing ' in those string while combine.

$a3 = array_combine($a1, $a2);

Output i want is like below,

array(
    35 => "Testing",
    37 => "testing",
    43 => "tessting"
)
1

2 Answers 2

3

Then after combining them, you could use array_map on the resultant array:

$a3 = array_map(function($e){
    return str_replace("'s", '', $e);
}, array_combine($a1, $a2)); // anonymous function PHP 5.4 or greater
Sign up to request clarification or add additional context in comments.

1 Comment

How can i parse the single, double quotes and other characters in from csv file data while parsing those datas in php?
0

You can do that :

function cust_replace($n)
    {
        return  str_replace("'s","",$n);
    }
    $a1 = array("35","37","43");
    $a2 = array("Testing's", "testing's", "tessting's");
    $a3 = array_map("cust_replace",array_combine($a1, $a2));
    print_r($a3);

If you want to make it more generic, you can add another parameter to cust_replace - the "needle" you want to replace.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.