how to convert a string to an array in php
Use explode function
$array = explode(' ', $string);
The first argument is delimiter
convert string array to array php
Its JSON, to convert to array use json_decode()
:
$string = '{"status":"2","holding":"","company":"Company","nik":"8981237","name":"Markonah","ownercard":"Istri"}';
$my_array = json_decode($string,true);
echo $my_array["status"]; // output: 2
Store array within a string as an array
Looks like you probably want to use the PHP serialize method which will return a string representation of your array that you can then store in the database.
You can then use unserialize to get the data out of the string again.
If you're desperate for a way to reparse a var_dump
output to an array you can check out this answer https://stackoverflow.com/a/3531880/7326037 but it's not pretty.
String array to real array
You could write the string to a file, enclosing the string in a function definition within the file, and give the file a .php extension.
Then you include the php file in your current module and call the function which will return the array.
How to add strings to an array inside a php function
You need to make it a reference parameter.
function chkEdt(&$a,$b) {
$a[]=$b;
};
Then any changes to $a
in the function will affect the array variable that's used as the argument.
How can I convert an array of strings into an associative array in PHP?
As simple as:
$arr = array(
"action: Added; amount: 1; code: RNA1; name: Mens Organic T-shirt; colour: White; size: XL",
"action: Subtracted; amount: 7; code: RNC1; name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y",
"action: Added; amount: 20; code: RNV1; name: Gift Voucher; style: Mens; value: £20",
);
$data = [];
foreach ($arr as $line) {
$newItem = [];
foreach (explode(';', $line) as $item) {
$parts = explode(':', $item);
$newItem[trim($parts[0])] = trim($parts[1]);
}
$data[] = $newItem;
}
print_r($data);
Fiddle here.
Accumulate strings and array values into an array as a class property via method
You can cut a lot of unnecessary bloat from your method.
You can cast ALL incoming data to
array
type explicitly. This will convert a string into an array containing a single element. If the variable is already an array, nothing will change about the value.Use the spread operator (
...
) to perform a variadic push into the class property.
Code: (Demo)
class Users
{
public $listOfUsers = [];
function add($stringOrArray): void
{
array_push($this->listOfUsers, ...(array)$stringOrArray);
}
}
$users = new Users;
$users->add('Terrell Irving');
$users->add(['Magdalen Sara Tanner', 'Chad Niles']);
$users->add(['Mervin Spearing']);
var_export($users->listOfUsers);
Output:
array (
0 => 'Terrell Irving',
1 => 'Magdalen Sara Tanner',
2 => 'Chad Niles',
3 => 'Mervin Spearing',
)
Related Topics
PHP 7 and Strict "Resource" Types
Mysqli Query Doesn't Work Twice
Dynamically Bind MySQLi_Stmt Parameters and Then Bind Result (Php)
PHP Strtotime Returns a 1970 Date When Date Column Is Null
Laravel 5.3 - How to Add Sessions to 'Api' Without Csrf
Phpmailer Not Sending and Not Giving Error
Warning Problem: Expects Parameter 1 to Be MySQLi_Result
How to Determine the Extension(S) Associated with a Mime Type in PHP
Redirect to 'Www' Before Ssl Requirement
Interpreting Return Value of Function Directly as an Array
PHP Domdocument Getting Attribute of Tag
Convert This String to Timestamp PHP
How to Send Email with PDF Attachment Using PHP
Is a Blob Converted Using the Current/Default Charset in MySQL