{"id":2270,"date":"2013-02-08T22:03:20","date_gmt":"2013-02-08T22:03:20","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2270"},"modified":"2013-02-08T22:03:20","modified_gmt":"2013-02-08T22:03:20","slug":"php-replace-array-value-how-to-replace-array-value-using-php-example","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/php-replace-array-value-how-to-replace-array-value-using-php-example\/","title":{"rendered":"PHP Replace Array Value Example"},"content":{"rendered":"<p>One of the coolest things of <a title=\"PHP Add To An Array | Add Element To Array Using PHP Examples\" href=\"https:\/\/theextremewebdesigns.com\/blog\/php-add-to-an-array-add-element-to-array-using-php-examples\/\" target=\"_blank\">array<\/a> is that you can hold as much data as you need. So when dealing with arrays, there might have been a time when one would come across a requirement to <strong>replace or change array value with another value<\/strong>. In this article, I am going to show you <em><strong>how to use PHP to replace array valu<\/strong><\/em>\u00a0by means of simple examples. Demos follow.<!--more--><\/p>\n<h2>How To use PHP to Replace Array Value<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2270\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s consider a simple example. Let&#8217;s assume that we have an array that consists of fruit names. We will now choose a fruit name (value) and we will replace this value in this array with another value i.e. with another fruit name. In this scenario, there arises 2 cases.<\/p>\n<p>1. Replacing the array value assuming that all values in the array are unique.<\/p>\n<p>2.\u00a0Replacing the array value assuming that some or all values in the array may be duplicate values of an original value.<\/p>\n<p>So let&#8217;s start with the first case.<\/p>\n<h3>Example 1:\u00a0Replacing the Array Value Using PHP\u00a0assuming that all values in the array are unique<\/h3>\n<pre class=\"lang:php decode:true\" title=\"Example 1\">echo 'Example 1 - Replace value \"apple\" with value \"strawberry\"&lt;br \/&gt;';\n\n\/\/Declare an array\n$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple', 'apple', 'guava', 'apple');\n\n\/\/Print the contents of the array.\necho 'Following are the original array contents:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_fruits_array);\necho '&lt;\/pre&gt;';\n\n\/\/Replace the value \"apple\" with \"strawberry\" in the array\n$search_for = \"apple\";\n$replace_with = \"strawberry\";\nfor($i=0;$i&lt;count($my_fruits_array);$i++) \n{\n\tif($my_fruits_array[$i] == $search_for) \n\t{\n\t\t$my_fruits_array[$i] = $replace_with;\n\t}\n}\n\n\/\/Print the contents of the array.\necho 'Following are the array contents AFTER removing the duplicate values from array:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_fruits_array);\necho '&lt;\/pre&gt;';<\/pre>\n<h3>Example 2:\u00a0Replacing the Array Value Using PHP\u00a0assuming that all mutiple values in the array need replacement<\/h3>\n<pre class=\"lang:php decode:true crayon-selected\" title=\"Example 2: Replace Array Value Using PHP assuming that all mutiple values in the array need replacement\">echo 'Example 2: Replace the value \"apple\" with value \"strawberry\" and \"mango\" value with \"fig\" value in array &lt;br \/&gt;';\n\n\/\/Declare an array\n$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple', 'apple', 'guava', 'apple', 'mango');\n\n\/\/Print the contents of the array.\necho 'Following are the original array contents:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_fruits_array);\necho '&lt;\/pre&gt;';\n\n\/\/Replace the value \"apple\" with \"strawberry\" value and \"mango\" value with \"fig\" value in array\n$replacements_array = array( 'apple' =&gt; 'strawberry', 'mango' =&gt; 'fig');\n\n\/\/For each of the replacement values in the array, loop through the array values\nforeach ( $replacements_array as $key1 =&gt; $val1 )\n{\n\t\/\/Loop through each of the original $my_fruits_array so that we can check if the value matches with our replacement array key.If yes, we will replace the value of $my_fruits_array with the corresponding replacement value from $replacements_array array.\n\tforeach ( $my_fruits_array as $key2 =&gt; $val2 )\n\t{\t\n\t\t\/\/If the value to be reaplaced in replacements_array == already existing value in $my_fruits_array\n\t\tif( $key1 == $val2 )\n\t\t{\n\t\t\t\/\/Then simply use the index of $my_fruits_array[ to update its value with the replacement value from $replacements_array\n\t\t\t$my_fruits_array[$key2] = $val1;\n\t\t}\n\t}\n}\n\n\/\/Print the contents of the array.\necho 'Following are the array contents AFTER removing the duplicate values from array:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_fruits_array);\necho '&lt;\/pre&gt;';<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2270\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>That&#8217;s it!<\/h5>\n<p>Do you know of any other ways to use \u00a0<em><strong>PHP to replace array values<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the coolest things of array is that you can hold as much data as you need. So when dealing with arrays, there might have been a time when one would come across a requirement to replace or change array value with another value. In this article, I am going to show you how [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[126,10,128,13],"tags":[127,33],"class_list":["post-2270","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-demos","category-php-tips-and-tricks","category-php-demos","category-tips-and-tricks","tag-demos-php","tag-php-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-AC","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=2270"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2270\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=2270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}