Array to string conversion in php mysql insert

You can [and have to] use a pair of curly braces around the expressions, and also quote the array keys:

$insert = mysqli_query[$con, "INSERT INTO table1 [uid, materials, num, cost]
            VALUES
            [
            '{$_POST['uid']}',
            '{$_POST['material'][$i]}',
            '{$_POST['num'][$i]}',
            '{$_POST['cost'][$i]}',
            ]"];

See php.net > Strings

It's because PHP's variable parsing in strings is limited to single-dimension array. For two or more dimensions you need to use the curly braces to indicate PHP the end of the variable. Same applies to accessing arrays with a string key.

You also need to quote the string array keys because they are actual strings, which you should always quote them properly

Also note that the code is fine if it is not production code. However you should be aware that your code opens a big security vulnerability called SQL Injection. If this is more serious work, you should refer to the link and fix the security vulnerability.

While working with PHP, we face many problems related to arrays, where we cannot work on the array directly. To solve it, first we have to convert an array into a string, and then we can efficiently work on a single element.

A string is used to store characters in a sequence and represent it as a single entity with a single data type. Thus, data storage is flexible, making it easy to work with, whereas Array stores data as a particular entity, and the data storage is fixed.

We will be working on three ways to convert array elements into strings in PHP.

1] Implode [] function in PHP:

The IMPLODE [] function is a built-in PHP function that is mainly used to join all the elements of a declared array. When all the array elements are joined together, they form a string, implode [] works similarly to the joint [] function in PHP, and return the value as a string.

NOTE: - The array is represented in the form of a string, but the base data type remains array. If we use the gettype [] function on the converted string, it will still show an array.

Here the separator refers to what we have to add between elements of an array while converting it to a string. The default value is an empty string "", but we can add many different values like "," "-" "_" "+" ";" ":" etc.

Array stands for the array that will be joined as a string.

Example: To convert an array to string.

When we execute the code as mentioned above, the compiler will give the output -

Array [ [0] = > This [1] = > Array [2] = > Will [3] = > Be [4] = > Converted [5] = > To [6] = > A [7] = > String ] 
converted array 
This Array Will Be Converted To A String

  • In the above code, first, we have declared an array with the name $arra and assigned some values to it. Here each element is considered a separate identity.
  • To show the original, we have used the Print_r function, which displays each array separately.
  • And finally, we have used the implode [] function that will convert all the elements of an array into a string. We have initialized both the parameters separator "" to add space between elements and array name to define the array.

Example : To convert an array to string with different parameters.

When we execute the code as mentioned above, the compiler will give the output -

Default array
Array [ [0] => This [1] => Is [2] => An [3] => ARRAY [4] => ! ] 
Array to string
This Is An ARRAY ! 
This _ Is _ An _ ARRAY_ ! 
This - Is - An - ARRAY- ! 
This / Is / An / ARRAY/ ! 
Data type: -
Array

  • In the above code, we first declared an array with the name $arra and assigned some values. Here each element is considered a separate identity.
  • To show the original, we have used the Print_r function, which displays each element of the array separately.
  • And then, we have used the implode [] function that will convert all the array elements into a string. Finally, we have initialized different separators to add space between elements.
  • In the end, we have used the gettype [] function on our converted array to show that even after conversion to string, the initial data type of the array remains the same.

NOTE: We can again convert the string back to array using the explode [] function

Example -> to convert string back to array

When we execute the code as mentioned above, the compiler will give the output -

original string -> we will convert the string back to an array 
converted array -> Array [ [0] = > we [1] = > will [2] = > convert [3] = > string [4] = > back [5] = > to [6] = > array ]

  • In the above code, we first declared an array with the name $strng and assigned some values to it.
  • And then, we have used the explode [] function that will convert all the string elements back to the array. Finally, we have stored the converted string to a variable $arra.
  • To show the original, we have used the Print_r function, which displays each array separately.

2] Join [] function in PHP:

The Join [] function is a built-in PHP function commonly used to join arrays and return a string.

A separator refers to what we have to add between elements of an array while converting it to a string. The default value is an empty string "", but we can add many different values like ", " "-" "_" "+" ";" ":" etc.

Array stands for the array that will be joined as a string

Example: To convert an array to string using join.

When we execute the code as mentioned above, the compiler will give the output -

original array 
Array [ [0] = > This [1] = > Array [2] = > Will [3] = > Be [4] = > Converted [5] = > To [6] = > A [7] = > String ] 
converted array 
This Array Will Be Converted To A String 
This _ Array _ Will _ Be _ Converted _ To _ A _ String

  • In the above code, first, we have declared an array with the name $arra and assigned some values to it. Here each element is considered a separate identity.
  • To show the original, we have used the Print_r function, which displays each array separately.
  • And finally, we have used the join [] function that will convert all the elements of an array into a string. We have initialized both the parameters separator "" and " _ " to add space between elements and array name to define the array.

3] Json_encode [] function in PHP:

The Json_encode [] function is a built-in PHP function commonly used to convert arrays and objects in PHP into JSON string [JavaScript Object Notation] representation.

NOTE: - JSON is used commonly to retrieve data from web servers and display the same on web pages

Array/object name stands for the array that will be joined as a string.

Example -> to convert an array to string using json_encode

When we execute the code as mentioned above, the compiler will give the output -

{"f-name":"ABC","l-name":"DEF","0":{"email":"[email protected]","mobile":"abcdefghij","0":{"address":"klmnopqrst","city":"OAB","pincode":"110090"}}}

  • In the above code, we first declared an array with the name $profile and declared a multi-dimensional array inside the variable.
  • After we have declared a variable $json which will be used to hold the return value from
  • And then, we have used the json_encode [] function that will convert all the elements of an array into a string.
  • In the end, we have used echo[$json] to get the output string.

How to convert an array into string in PHP?

There are two ways to convert an array into a string in PHP..
Using implode[] function..
Using json_encode[] function..

How to convert string to array and array to string in PHP?

Using json_encode[] function to convert an Array to a string To convert an array to a string, one of the common ways to do that is to use the json_encode[] function which is used to returns the JSON representation of a value.

Which function is used to convert array to string PHP?

In this article, we are using two methods to convert array to string. Method 1: Using implode[] function: The implode[] method is an inbuilt function in PHP and is used to join the elements of an array. The implode[] method is an alias for PHP | join[] function and works exactly same as that of join[] function.

How to store string in array in PHP?

PHP | str_split[] Function The str_split[] is an inbuilt function in PHP and is used to convert the given string into an array. This function basically splits the given string into smaller strings of length specified by the user and stores them in an array and returns the array.

Chủ Đề