Gundars Mēness
Gundars Mēness

Reputation: 498

Fastest way to convert a formatted string to an array?

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
)

Upvotes: 0

Views: 1460

Answers (6)

mickmackusa
mickmackusa

Reputation: 47934

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.

Upvotes: 0

mario
mario

Reputation: 145482

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]);

Upvotes: 1

Buddha
Buddha

Reputation: 4476

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.

Upvotes: 0

Zul
Zul

Reputation: 3608

<?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);
?>

Upvotes: 1

Teson
Teson

Reputation: 6736

json_decode close to what you're requesting.

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

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270637

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"
}

Upvotes: 8

Related Questions