0

I am working on an API proxy that will accept request, pass it on to a server, and then filter the response such that the requester only receives subset of the response.

Given the following JSON form of response data:

"images": [
        {
            "id": 2360545,
            "src": "https://my_site.com/tester-300x300-1.png",
            "name": "tester.png",
        },
        {
            "id": 2433529,
            "src": "https://my_site.com/background-01-1.png",
            "name": "background.png",
        },
]

Using data_get($data, 'images.*.name'); correctly returns an array of the name values

[ 'tester.png', 'background.png' ]

However, in addition to the values, I need also the dotted-string key to each value, in order to data_set() it in a response_to_client array (which starts out empty). i.e. I need a return value of

[ 
 [
    'key' => 'images.0.name'
    'value' => 'tester.png', 
 ],
 [
    'key' => 'images.1.name'
    'value' => 'background.png' 
 ]
]

Doing

$response_to_client = [];
$names = data_get($data, 'images.*.name');
data_set($response_to_client,'images.*.name',$names);

results in nothing being written to $response_to_client because it has no initial image data. Doing

$response_to_client = [];
$names = data_get($data, 'images.*.name');
data_set($response_to_client,'images.name',$names);

results in

"images": {
    "name": [
        "tester.png",
        "background.png",
    ]
}

but I need it to look like

"images": [
    {
        "name": "tester.png"
    },
    {
        "name": "background.png"
    }
]

Need a solution that also works for keys with multiple wildcards. Looked all through the Laravel class and helper functions, nothing seems to give the keys of wildcard matched values.

  • Working on a solution to this which involves using `Arr::dot()` to associate each of `$data`'s values to it's dotted.string key. Then use regex, i.e. `/^images.[0-9]+.name$/m` to find all values that match key `images.*.name`. Now we have not only the values but also their associated dotted-string keys! Works for any number of wildcards, simply replace each wildcard with `[0-9]+` in the regex. – Meyer Auslander - Tst May 05 '23 at 13:49

1 Answers1

0

It's not incorrect at all. You stated yourself that when you do

$names = data_get($data, 'images.*.name');

$names becomes ['tester.png', 'background.png'].

So when you pass $names to data_set, it won't magically change to a different format.

You'd need to pass an array like this to end up with the format you want.

[
    ['name' => 'tester.png'],
    ['name' => 'background.png']
]

Which you can get using array_map or something similar.

$response_to_client = [];
$names = data_get($data, 'images.*.name');

$mapped = array_map(fn($value): array => ['name' => $value], $names);

data_set($response_to_client, 'images', $mapped);

https://www.php.net/manual/en/function.array-map

IGP
  • 14,160
  • 4
  • 26
  • 43
  • Good point @IGP! However, I need a solution that also allows for keys with more than one wildcard. – Meyer Auslander - Tst May 04 '23 at 03:12
  • There is no magic solution that will fit all your needs. The workflow is the same. Get the data (data_get). Reformat it (array_map), and set it (data_set). – IGP May 04 '23 at 17:30
  • there is no way to know how to `array_map` in a case where the data comes from a key with 2 or more wildcards. Say there are two wildcards and two results are returned. Maybe they are both from the 0th element of the first wildcard and there are two elements of the second wildcard. Or, maybe there are two elements of the first wildcard, each having one element at the level of the second wildcard. The problem is `data_get()` gives no info about where the values are coming from. – Meyer Auslander - Tst May 04 '23 at 19:37
  • Then you use something different than array_map or build your array from nothing. You can pass a second argument to array_map's Closure that takes in the array's key. – IGP May 04 '23 at 20:42
  • The problem is you don't know what the 'array's key' should be. The `array_map` solution only works when there is one wildcard. If so, the results from `data_get()` already have the correct keys, `0,1,2,..etc`. If there are two wildcards, then you need a pair of keys for each value, `0,1,2,..etc` won't help. – Meyer Auslander - Tst May 04 '23 at 21:03