0

What is the fastest way to convert this string to this array?

$string = 'a="b" c="d" e="f"';

Array (
a => b
c => d
e => f
)
0

6 Answers 6

8

Assuming they're always separated by spaces and the values always surrounded by quotes, you can explode() twice and strip out the quotes. There might be a faster way to do this, but this method is very straightforward.

$string = 'a="b" c="d" e="f"';
// Output array
$ouput = array();
// Split the string on spaces...
$temp = explode(" ", $string);

// Iterate over each key="val" group
foreach ($temp as $t) {
  // Split it on the =
  $pair = explode("=", $t);
  // Append to the output array using the first component as key
  // and the second component (without quotes) as the value
  $output[$pair[0]] = str_replace('"', '', $pair[1]);
}

print_r($output);
array(3) {
  ["a"]=>
  string(1) "b"
  ["c"]=>
  string(1) "d"
  ["e"]=>
  string(1) "f"
}
Sign up to request clarification or add additional context in comments.

Comments

2

json_decode close to what you're requesting.

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

2 Comments

if the OP has control over building the string, then this is a good solution.
very nice trick, I feel the input string can be modified to this format and get it done.
1
<?php
$string = 'a="b" c="d" e="f"';
$string = str_replace('"','',$string);

$str1 = explode(' ',$string);
foreach($str1 as $val)
{
    $val2 = explode('=',$val);
    $arr[$val2[0]] = $val2[1];
}

print_r($arr);
?>

Comments

1

Instead of a flimsy explode, I would recommend a regex. That verifies the structure instead of hoping for the best. It's also so much shorter:

preg_match_all('/(\w+)="([^"]*)"/', $input, $match);
$map = array_combine($match[1], $match[2]);

Comments

0

Looks like it is php script that you are referring. But please add php tag to it as suggested.

I'm not sure if there is a direct way to split it the way you want because you want the indexes to be something other than default ones.

one Algorithm to solve this problem is as follows...

  1. Split it with a delimiter of space
  2. remove semicolons from each generated string
  3. split each generated string using '='
  4. add elements to new array with string before = as key and after = as value

I'm not sure this is fastest though.

Comments

0

I'll make no promises about its speed or its reliability because running an accurate benchmark requires your real data (quality and volume).

Anyhow, just to show readers another method, I'll use parse_str()

Code: (Demo)

$string = 'a="b" c="d" e="f"';
parse_str(str_replace(['"',' '],['','&'],$string),$out);
var_export($out);

This strips the double quotes and replaces all spaces with ampersands.

The method, like several others on this page, will mangle your data when a value contains a space or a double quote.

Output:

array (
  'a' => 'b',
  'c' => 'd',
  'e' => 'f',
)

For the record, mario's answer will be the most reliable on the page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.