How to insert an element in php array?

Before few days, we have a requirement to insert an element in php array, after the initialization of it.

To fulfill our requirement, we have got two options.

First one is to use “$array[] = $var;” statement and second one is to use “array_push(array, value1[,value2…])”.

Where “array_push()” inserts one or more elements onto the end of array.

It has two arguments.

First one is an array in which we want to insert an element.

And the second one is the value, which needs to be inserted in an array.

So now here an example, which inserts an element in php array, using “$array[] = $var” and “array_push()”.

$test = array("First", "Second", "Third");
$test[] = "Fourth";
array_push($test, "Fifth");
print_r($test);

Hope this will help you.

Advertisements

Assign value dynamically to variable by String Concatenation, by assigning dynamic string in loop and call the method dynamically.

Once I have need to assign a dynamically string concatenation variable value in loop

I have 3 array and in string last of digit is related to my array name so I used two logic

One for string subpart and another for assigning dynamic value.

My variable are

$Str1 = array();

$Str2  = array();

$Str3  = array();

I have checkbox in frontend form

And its has value are searchsimple1 searchadvance2 and searchprice3. so I have need to call method dynamically and assign the value dynamically to Str Pairs.

Frontend checkbox name is search[].

</pre>
foreach($_post[search] as $value)
 {

//call the method as per as value so substr for method name
 $value1 = substr($value,0,strlen($value)-1);

//assign last digit of variable to str_concate so it can assign dynamically Str1 ,etc..
 $str_concator = substr($value,strlen($value1));

//assigning value to Str and calling function dynamically
 ${"search$search_concator"} = $value1($post);

}
<pre>

I hope this will help you lot thanks in advance

How to make comma separated string from php array?

Before few days we have a requirement to make comma separated string from php array.

To do so we have used “implode()” of php, which joins array elements with a string.

“implode(separator, array)” is supporting in PHP 4, PHP 5 and it has two arguments.

First one is a separator – A string which is used to put between the array elements. Default it is “”. And this argument is optional.

Second one is an array – An array to join to a string separator. And this argument is required.

Now here is an example to make comma separated string from array using implode().

$array_elements = array("First", "Second", "Third", "Fourth", "Fifth");

$comma_separated_string = implode(', ', $array_elements);

echo "<br />Comma Separated String:<br />" . $comma_separated_string;

Try it and hope this will help you.

PHP Bug #44081: Cannot execute queries while other unbuffered queries are active

Before 8 to 10 days, I have been working with Yii framework in WAMP PHP version 5.2.9.

While working, I had got PDO Mysql Error: Cannot execute queries while other unbuffered queries are active.

To solve the above error, I have searched in the internet, and read that there was some problem while executing pdo mysql related code in php 5.2.9.

So after reading that I have changed my wamp server php version to 5.3.0 and my problem has been solved.

Try it and hope this will help you.