Convert PHP Array String into an Array

Convert PHP array string into an array

I think you might want to look into serialize and unserialize.

$myArray = array('key1'=>'value1', 'key2'=>'value2');
$serialized = serialize($myArray);
$myNewArray = unserialize($serialized);
print_r($myNewArray); // Array ( [key1] => value1 [key2] => value2 )

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

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.

Turn an array format string into actual Array in PHP

your value is like json you need just decode it like below code :

$string = '["something", "someone", "anything", "anyone"]'; 
$array = json_decode($string);
var_dump($array);

like @elmasterlow say in comment

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.

Converting array of string to array of integers in PHP

You should use array_map instead of array_walk:

$numArray = array_map('intval', $numArray);

If you still want to use array_walk - refer to a manual which says:

If callback needs to be working with the actual values of the array, specify the first parameter of callback as a reference. Then, any changes made to those elements will be made in the original array itself.

As intval function doesn't work with references you need to wrap it in some other logics, something like:

array_walk($numArray, function(&$v){ $v = intval($v); });
// which is the same as @BizzyBob solution)))

Convert a string into array PHP

What about first exploding the string with the explode() function, using ', ' as a separator :

$str = "'middle_initial' => '', 'sid' => '1419843', 'fixed' => 'Y', 'cart_weight' => '0', 'key' => 'ABCD', 'state' => 'XX', 'last_name' => 'MNOP', 'email' => 'abc@example.com', 'city' => 'London', 'street_address' => 'Sample', 'first_name' => 'Sparsh',";
$items = explode(', ', $str);
var_dump($items);

Which would get you an array looking like this :

array
0 => string ''middle_initial' => ''' (length=22)
1 => string ''sid' => '1419843'' (length=18)
2 => string ''fixed' => 'Y'' (length=14)
3 => string ''cart_weight' => '0'' (length=20)
...


And, then, iterate over that list, matching for each item each side of the =>, and using the first side of => as the key of your resulting data, and the second as the value :

$result = array();
foreach ($items as $item) {
if (preg_match("/'(.*?)' => '(.*?)'/", $item, $matches)) {
$result[ $matches[1] ] = $matches[2];
}
}
var_dump($result);

Which would get you :

array
'middle_initial' => string '' (length=0)
'sid' => string '1419843' (length=7)
'fixed' => string 'Y' (length=1)
'cart_weight' => string '0' (length=1)
...


But, seriously, you should not store data in such an awful format : print_r() is made to display data, for debugging purposes -- not to store it an re-load it later !

If you want to store data to a text file, use serialize() or json_encode(), which can both be restored using unserialize() or json_decode(), respectively.

Convert PHP String to Associative Array

You are trying to over-engineer simple thing, which result in wasting too much time for having task done.

$str = "Name,Age,Location\nJo,28,London";

$lines = explode("\n", $str);
$keys = explode(',', $lines[0]);
$vals = explode(',', $lines[1]);

$result = array_combine($keys, $vals);

But even ordinary loop would do the trick in your case and this is what you should end up with unless you had better ideas:

$result = []; 
for($i=0; $i<count($keys); $i++) {
$result[$keys[$i]] = $vals[$i];
}

I also recommend getting thru list of available built-in array functions for future benefits.

Convert a comma-delimited string into array of integers?

You can achieve this by following code,

$integerIDs = array_map('intval', explode(',', $string));

Convert flat array to a delimited string to be saved in the database

Use implode

implode("|",$type);


Related Topics



Leave a reply



Submit