L
L
lfbzrwjkc2021-01-11 16:00:31
PHP
lfbzrwjkc, 2021-01-11 16:00:31

Pass function to JSON in PHP?

Hey!
Serialize array in php:

$arr = array(
            'id' => $a,
            'name' => $b, 
            'value' => $c, 
            'fill' => "somefunction('".$d."')"
        );
echo json_encode($arr);


I need the json to be shown like this:
[{
  "id": "1",
  "name": "123",
  "value": 100,
  "fill": somefunction('data')
}, {
  "id": "2",
  "name": "123",
  "value": 50,
  "fill": somefunction('data')
}]


not like this:
[{
  "id": "1",
  "name": "123",
  "value": 100,
  "fill": "somefunction('data')"
}, {
  "id": "2",
  "name": "123",
  "value": 50,
  "fill": "somefunction('data')"
}]


the recipient of the json needs to execute the somefunction function, as I think you can already see from the conditions.

Question: is there a concise way or will I have to reinvent the wheel json_encode_kak_ya_zahotel()? the framework on the client assumes the calculation of somefunction () itself, so I'm not going to perform it on the back and do not persuade it.
I'm not going to pass the assignment operator in PHP somefunction () as a server function??

thanks to all!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2021-01-11
@lfbzrwjkc

so that json is shown like this
This is not valid JSON, so yes, you need to write your own serialization to this non-standard format.
But then the deserialization of this non-standard format will also need to be written on the client.
It's much easier to pass a string and use, God forbid, eval.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question