How to Convert a String to an Array in PHP

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



Leave a reply



Submit