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
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.
Convert String to an array with PHP
Don't put the array in quotes, it should be an ordinary PHP array.
$objectresult = ['Username' => $currentusername, 'Password' => $currentpassword, 'Age' => $currentage];
How to convert String Array to php Array?
Assuming you are not allowing multi dimensional arrays, this will work:
$stringArray = "['a'=>'value one','key2'=>'value two']";
$array = array();
$trimmedStringArray = trim( $stringArray, '[]' );
$splitStringArray = explode( ',', $trimmedStringArray );
foreach( $splitStringArray as $nameValuePair ){
list( $key, $value ) = explode( '=>', $nameValuePair );
$array[$key] = $value;
}
Output:
Array
(
['a'] => 'value one'
['key2'] => 'value two'
)
You will probably want to do some error checking to make sure the input is in the correct format before you process the input.
Php convert string into array for group?
Assuming that is one string value, this is probably as efficient as you can get:
$str = 'operation.user.add
operation.user.edit
operation.permission.add
performance.tool.add
performance.tool.remove
operation.permission.edit
operation.tool.delete
operation.tool.remove';
$output = [];
foreach (explode("\n", $str) as $string) {
$parts = explode('.', $string);
$output[$parts[0]][$parts[1]][] = $parts[2];
}
print_r($output);
Output:
Array
(
[operation] => Array
(
[user] => Array
(
[0] => add
[1] => edit
)
[permission] => Array
(
[0] => add
[1] => edit
)
[tool] => Array
(
[0] => delete
[1] => remove
)
)
[performance] => Array
(
[tool] => Array
(
[0] => add
[1] => remove
)
)
)
convert string array to integer array in php
You can use array_map
$TaxIds = array_map(function($value) {
return intval($value);
}, $TaxIds);
How to convert JSON string to arrays
This is what you need, json_decode($json,true);
<?php
$json = '[{"name":"Menu","sub":[{"name":"Menu 2","url":"menu-2.php"}]}]';
$array = json_decode($json,1);
print_r($array[0]);
?>
DEMO: https://3v4l.org/JZQCn
OR use it as a parsable string representation of a variable with var_export()
<?php
$json = '[{"name":"Menu","sub":[{"name":"Menu 2","url":"menu-2.php"}]}]';
$array = var_export(json_decode($json,1)[0]);
print($array);
?>
DEMO: https://3v4l.org/rLA9R
How to convert string with special character as array php
Code
foreach(array_filter(explode("|", "|dm:12|em:.25|fm:10|wm:85|de:143|qty:1")) as $ListItem){
$Item = explode(":", $ListItem);
$KVP[$Item[0]] = isset($Item[1]) ? $Item[1] : null;
}
var_dump($KVP);
Output
array(6) {
'dm' =>
string(2) "12"
'em' =>
string(3) ".25"
'fm' =>
string(2) "10"
'wm' =>
string(2) "85"
'de' =>
string(3) "143"
'qty' =>
string(1) "1"
}
Related Topics
Class 'App\Http\Controllers\Db' Not Found and I Also Cannot Use a New Model
How to Reset MySQL Pointer Back to the First Row in PHP
How to Format a Number to a Dollar Amount in PHP
Aescrypt Decryption Between iOS and PHP
Shell Run/Execute PHP Script with Parameters
How to Move an Array Element with a Known Key to the End of an Array in PHP
How to Measure the Strength of a Password
Regex - How to Insert String Before File Extension
Array Combine Three or More Arrays with PHP
Doctrine2 Association Mapping with Conditions
How to Check for Special Characters PHP
How to Replace the Deprecated Set_Magic_Quotes_Runtime in PHP
PHP & SQL Injection - Utf8 Poc
How to Check If a File Exists on a Remote Server Using PHP
Get Only Filename from Url in PHP Without Any Variable Values Which Exist in the Url