Array value to string in PHP

php associative array to string
array to string in php
array to string conversion in php mysql
php array
php implode multidimensional array
array to string conversion laravel
php array to json
php array to comma separated string

I've got some code which randomly fetches data from a database, but whenever I prints the data in the browser is looks like that - ["data"]. I want it to be a clear string. How can I make that? Thank you!

Here is the code:

<?php 
   require '../wordbeater/adminpanel/includes/dbh.inc.php'; 

   $result=mysqli_query($con, "SELECT * FROM users ORDER BY RAND() LIMIT 6");

   $count = 0;
   while($row=mysqli_fetch_row($result)){
       $postsarray["one.$count."] = $row[1];
       $postsarray["two.$count."] = $row[2];
       $count++;
   }

   $encodedArray = array_map(utf8_encode, $postsarray);
   echo $encodedArray;


   mysqli_close($con);

   ?>

Try using:

   $result=mysqli_query($con, "SELECT * FROM users ORDER BY RAND() LIMIT 6");

   $count = 0;

   while ($row = $result->fetch_row()) {
         $postsarray["one.$count."] = $row[1];
         $postsarray["two.$count."] = $row[2];
         $count++;
    }

How to Convert PHP Arrays to Strings, Convert PHP Arrays to Strings. PHP's implode function returns a string consisting of array element values joined using a string that you specify: $ar = ['apple'� In PHP, the implode () method joins array elements and outputs them as a single string. This is useful when you need to create one string out of a set of array values. The function can be invoked in two ways (with or without the glue string): implode (string $glue, array $pieces) – The glue is used to combine the array pieces.

I think you should go for mysqli_fetch_assoc as it will give you the same associative array as like your database. for e.g.

while($row=mysqli_fetch_assoc($result)){
   $postsarray["one.$count."] = $row["column_name_u_want_to_fetch"];
   $postsarray["two.$count."] = $row["2_column_name_u_want_to_fetch"];
   $count++;
}

implode - Manual, The array of strings to implode. Return Values �. Returns a string containing a string representation of all the array elements in the same order, with the glue� WHY JSON : You can use it with most of the programming languages, string created by serialize() function of php is readable in PHP only, and you will not like to store such things in your databases specially if database is shared among applications written in different programming languages

You can directly store it in a variable so it will take it as a string. for e.g.

$stringone="";
$stringtwo="";
while($row=mysqli_fetch_assoc($result)){
$stringone.$count = $row["column_name_u_want_to_fetch"];
$stringtwo.$count = $row["2_column_name_u_want_to_fetch"];
$count++;

}

for($i=0; $i<mysqli_num_rows($result); $i++)
{

echo $stringone.$i;
echo $stringtwo.$i;
}

PHP "Array to String" (Examples of How to Use the implode Function , This is useful when you need to create one string out of a set of array values. The function can be invoked in two ways (with or without the glue� The json_encodemethod takes a JSON encoded string and converts it to a PHP variable. You can use this method to convert an array into a string.

try it by removing 'ORDER BY RAND() LIMIT 6'

Array to String PHP?, Anything else is useless, JSON keeps the array relationship intact for later usage! now you can insert this $string_array value into Database. 1) Type casting (Type Juggling) From PHP official docs: “PHP does not require (or support) explicit type definition in variable declaration; a variable’s type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string.

converting array values to string, converting array values to string � php arrays mongodb. [a] => Array ( [0] => MongoId Object ( [$id] =>� The implode() function returns a string from the elements of an array. Note: The implode() function accept its parameters in either order. However, for consistency with explode(), you should use the documented order of arguments. Note: The separator parameter of implode() is optional. However, it is recommended to always use two parameters for backwards compatibility.

PHP implode() Function, Return Value: Returns a string from elements of an array. PHP Version: 4+. Changelog: The separator parameter became optional in PHP 4.3.0� A comment on array_merge mentioned that array_splice is faster than array_merge for inserting values. This may be the case, but if your goal is instead to reindex a numeric array, array_values() is the function of choice.

Convert PHP Arrays to String Using Implode() Function in PHP 7, This parameter allows you to insert seprator type in your array element. It comes with ” “ empty string. Array, This is an array which will be� How to get single value from an array in PHP. Topic: PHP / MySQL Prev|Next. Answer: Use the Array Key or Index. If you want to access an individual value form an indexed, associative or multidimensional array you can either do it through using the array index or key. Let's check out the following example to understand how it basically works:

Comments
  • Show us what you got and what you expected
  • use implode, not array_map
  • You cannot use echo on arrays - use var_dump($encodedArray);
  • Important question: is your data stored in ISO 8859-1 / Windows 1252 encoding? Because that's what utf8_encode assumes, and if not, it will mangle your text. Many people use it wrong (or just unnecessarily) because of its poor choice of name.
  • array_map(utf8_encode, $postsarray) - are you sure that line does not yield an error message? Where is that constant utf8_encode defined?
  • Please add some explanation to that code such that others can learn from it
  • Now i got 00 as output.
  • $stringone.$count - what should that do? That looks pretty wrong to me
  • Please add some explanation to that code such that others can learn from it