Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7525743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:38:14+00:00 2026-05-30T03:38:14+00:00

When I use str_replace with an array as second argument like in the much

  • 0

When I use str_replace with an array as second argument like in the much upvoted and accepted answer to this question, I get an array to string conversion notice. Why?
Example:

$str = 'a b a ba a';
$numerals = range(1, 10);
$str = str_replace('a', $numerals, $str);

gives :

PHP Notice: Array to string conversion in php shell code on line 1

and the following output:

Array b Array bArray Array

instead of

1 b 2 b3 4

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:38:15+00:00Added an answer on May 30, 2026 at 3:38 am

    You are trying to replace one character (‘a’) with multiple characters (1,2,3,4,5,6,7,8,9,10). PHP can’t figure that out and tries to convert this array to string. When you’re using string as $search parameter you must also use string as $replace parameter. An array as $replace parameter can be used only when $search is also an array. Quoting from documentation:

    If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search. The converse would not make sense, though.

    Here’s the code that will work:

    $str = 'a b a ba a';
    $count = 1;
    while(($letter_pos = strpos($str, 'a')) !== false) {
        $str = substr_replace($str, $count++, $letter_pos, 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table like this -> http://jsfiddle.net/jurisKaste/FvgeQ/ And I need to get array of
For exemple, if I use a function like: function string_cleaner($string) { $replace = array('Ø',
You can use arrays with str_replace(): $array_from = array ('from1', 'from2'); $array_to = array
I basically want to use str_replace() all values of a multidimensional array. I can't
I want to use php str_replace on an array of some words, but I
use this website a lot but first time posting. My program creates a number
I am trying to use str_replace, but can't figure out how to use \b
A better example would be this: $string = That is a very nice ford
i am use this code to upload image ..this code save uploads url into
I can't seem to figure out how to get a JS array into PHP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.