0

This is my data:

$d = $request->request->get('data');

The output:

[{"name":"form[id]","value":"10"},{"name":"form[name]","value":"Telefon2"},{"name":"form[uuid]","value":"bb80878ad4"},{"name":"form[productgroup]","value":"6"},{"name":"form[category]","value":"1"},{"name":"form[documents]","value":"7"}

I want to create a new array, that is removing extracting the variable inside the brackets.

 function trim($d) {
          preg_match('#\[(.*?)\]#', $d, $match);
          return $match[1];
   }


$dData = array_combine(array_column(trim($d), 'name'), array_column($d, 'value'));
$json = json_encode($dData);

But the error is

Warning: preg_match() expects parameter 2 to be string, array given

6
  • Isn't your output missing a ] ? If it is then your output must be a json Commented Mar 21, 2019 at 8:16
  • Yes, sorry forgot one line Commented Mar 21, 2019 at 8:18
  • 1
    In that case I suggest you to use json_decode() Commented Mar 21, 2019 at 8:20
  • Why do you try regex at all? What is the expected $json result? Commented Mar 21, 2019 at 8:22
  • I neither understand why do you use preg_match. Did you consider using array_map? Commented Mar 21, 2019 at 8:25

2 Answers 2

2

You're going to need to do a few things before you begin.

Firstly, rename the trim function to something like extract_name because PHP already has a built in trim function that removes whitespace from a string - or any character present in the character mask.
Secondly, you are going to need to iterate over each of the elements in the name column. You will notice the error you are getting from preg_match is because you are passing all values in one go.
Thirdly, I am assuming that the value of $d is an array of PHP objects. This is done, and assumed, in my solution using $d = json_decode($d);.

Using array_map instead of a foreach means we can have a nice one liner:

function extract_name($d) {
    preg_match('#\[(.*?)\]#', $d, $match);
    return $match[1];
}

$dData = array_combine(array_map('extract_name', array_column($d, 'name')), array_column($d, 'value'));

The output being:

array:6 [
  "id" => "10"
  "name" => "Telefon2"
  "uuid" => "bb80878ad4"
  "productgroup" => "6"
  "category" => "1"
  "documents" => "7"
]

Live demo

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, this sounds like a good solution. I tested it but I get the error message Warning: array_map() expects parameter 1 to be a valid callback, function 'new_trim' not found or invalid function name
@monkeylove I have updated the question. As I said in the answer, you need to rename your original function to be called extract_name and then it will work.
Yes, this is what is confusing me.. However I name it, the error message is: Warning: array_map() expects parameter 1 to be a valid callback, function 'extract_name' not found or invalid function name
@monkeylove Are you sure you are copying this answer correctly? I have added a live demo to this answer to make it clearer what I think the poster was trying to accomplish.
@monkeylove Is the function declared in the same scope as $dData? Have you copied my snippet? The function must have the same name as a the string that you are passing to array_map. If that doesn't work, try creating an anonymous function instead and seeing if you are still having problems.
-1

I believe you need smth like this


$d = ['{"name":"form[id]","value":"10"}', '{"name":"form[name]","value":"Telefon2"}', '{"name":"form[uuid]","value":"bb80878ad4"}', '{"name":"form[productgroup]","value":"6"}'];

function combine(array $d): array
{
    $res = [];
    foreach ($d as $item) {
        $value = json_decode($item, true);
        preg_match('#\[(.*?)\]#', $value['name'], $match);;
        $columnName = $match[1];
        $res[$columnName] = $value['value'];
    }

    return $res;
}


$dData = combine($d);
$json = json_encode($dData);
echo $json;```

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.