Not sure if my question is clear, but here's what I'm trying to achieve. Let’s say I have a multidimensional array like so:
$arr['client1']**['dog']['Jack']**
$arr['client2']['cat']['Stacy']
How can I get the second portion of the array (between **), knowing it can be anything. For client 3, it could be a crocodile. For Client 4, it could be a car.
So I'm looking to "build" the structure of the array, dynamically. Something like so:
$arr['client1']{partBetweenThe**InTheExemple}
{partBetweenThe**InTheExemple} would be constructed "on the fly" (hence, the dynamically).
EDIT: Hopefully some clarifications...
The array changes every time. Basically, I'm building an addon to poll any API on the web. The data structure I'm getting can be anything. So what I need to do is build the key combination "on the fly", with variables.
In the exemple above, my variable would be something like $query = ['dog']['Jack'] and to get the value, I would poll it like so (from a logistic perspective, I know this doesn't work):
$arr['client1'][$query] or $arr['client1']$query or $arr['client1']{$query}
You can define the query as an array with each level as an element. Then we can iterate through that and check if we find a matching key in the response:
function findInArray(array $query, array $data)
{
foreach ($query as $key) {
if (!array_key_exists($key, $data)) {
// The key was not found, abort and return null
return null;
}
// Since the key was found, move to next level
$data =& $data[$key];
}
return $data;
}
// Example response
$response = [
'client1' => [
'dog' => [
'Jack' => 'Some value',
],
]
];
// Define the query as an array
$query = ['dog', 'Jack'];
$result = findInArray($query, $response['client1']);
Demo: https://3v4l.org/WjXTn
Edit:
So since the array's structure can't be changed this will return the client if the structure remains ['client']['animal']['name'].
$clients = [
'client1' => [
'dog' => [
'Jack' => []
]
],
'client2' => [
'cat' => [
'Stacy' => []
]
]
];
$animal = 'dog';
$name = 'Jack';
foreach ($clients as $client => $options) {
if (
array_key_exists($animal, $options) &&
array_key_exists($name, $options[$animal])
) {
echo $client;
break;
}
}
Related
Both of my arrays have over 500 000 elements.
I would like to return only those elements from multidimensional array that ARE NOT PRESENT in indexed array.
Here is what my multidimensional array looks like:
$new_codes = [
0 => [
'id' => 1,
'code' => 'code1',
... another values
],
1 => [
'id' => 2,
'code' => 'code2',
... another values
],
2 => [
'id' => 3,
'code' => 'code3',
... another values
]
];
Another array is just plain indexed array with code values:
$old_codes = [
'code1',
'code2',
];
For this limited example, after filtering, $new_codes should only have index 2 because that value doesn't exist in $old_codes array.
I've tried using the code bellow, but because the arrays are so huge, the operation takes so long that I thought that I somehow created infinite loop, but it seems that checking if the values for over 500000 elements exist in another array that also has over half a million elements takes very long time.
// option 1
$new = array_filter($new_codes, function ($var) use ($old_codes) {
return !in_array($var['code'], $old_codes);
});
// option 2
$filtered = [];
foreach($new_codes as $code) {
if(in_array($code['code']){
continue;
}
$filtered[] = $code;
}
Any suggestions for more optimized solutions are welcome.
Thanks in advance.
Reading this question I realized that using isset() is much better option for handling such a large amount of elements so I did this:
// switch array keys and values
$old_array = array_flip($old_array);
foreach($new_codes as $code) {
if(isset($old_array[$code['code']])){
continue;
}
$filtered[] = $code;
}
Doing this reduced the time to just few seconds.
I am trying to access values in a nested array that I send to my API but when do something like $data['inputs']{0} it only gives me back the first letter of the key when what I want is the whole value of the value and not the key but if I try something like $data['inputs']['type'] it gives me an offset error I ament sure how to correctly access the values the way I need to
public function saveEdit(Request $request)
{
try {
$unsanitizedData = $request->all();
$data = [];
$fid = $unsanitizedData['formId'];
$data['form_name'] = json_encode($unsanitizedData['cred']['name']);
$data['org'] = json_encode($unsanitizedData['cred']['organization']);
$data['updated_by'] = json_encode($unsanitizedData['updatedBy']);
$data['user_id'] = json_encode($unsanitizedData['id']);
$data['activated'] = json_encode($unsanitizedData['cred']['activated']);
$data['inputs'] = json_encode($unsanitizedData['cred']['inputs']);
$pattren = array("[","]","'",'"',"/","\\");
$data = str_replace($pattren,'', $data);
foreach ($unsanitizedData as $st) {
admin_form::where('id', $fid)->update([
'form_name' => $data['form_name'],
'org' => $data['org'],
'updated_by' => $data['updated_by'],
'user_id' => $data['user_id'],
'activated' => $data['activated']
]);
foreach ($data['inputs'] as $input) {
admin_form_fields::where('form_id', $fid)->update([
'type' => $input,
'name' => $input
]);
}
}
$res['status'] = true;
$res['message'] = 'Success';
return response($res, 200);
} catch (\Illuminate\Database\QueryException $ex) {
$res['status'] = false;
$res['message'] = $ex->getMessage();
return response($res, 500);
}
}
I thought if I use a foreach loop inside another foreach loop it would work because its a nested array so loop through the main one and then through the nested one but that did also not work
Data Structure when I do a data dump:
array:6 [
"form_name" => "Testname",
"org" => "TestOrg",
"updated_by" => "test",
"user_id" => "29",
"activated" => "false",
"inputs" => "{type:number,name:Phone},{type:input,name:Name},{type:input,name:Address},{type:email,name:Email}"
]
In your case, $data['inputs'] is a JSON encoded string from which you have removed the [ and ] characters so when you try to access its first element it's the first char (since strings are kind of arrays of strings in PHP, they are really array of strings in C).
The problem is that you call json_encode() in the first place. If it's how you sanitize input, you're doing it wrong. Since you're using an ORM, there's no real need to manually sanitize the input. Just keep your input as sent by the client and perform all your operations then use them unsanitized in the QueryBuilder
As far as I can see, you just need to use json_decode($data['inputs']) since your array is in fact just a string :)
I'm trying to construct an array where there only strings and the array would look like this
key->key->value
To explain it I attached two screenshots below.
I start with this:
After my code below I'm 90% there, yet there is an array in value on the third level instead of simple value.
Here is some code:
$theme = ThemeHandler::with('sections.settings')->find($activeTheme);
$themeSettings = $theme->sections;
$themeSettings = collect($themeSettings->toArray());
// dd($themeSettings);
$themeSections = [];
foreach ($themeSettings as $key => $value) {
$settings = collect($value['settings']);
$settings = $settings->mapToGroups(function ($item) {
return [$item['key'] => $item['value']];
});
$themeSections[$value['key']] = $settings->toArray();
}
dd($themeSections);
I would like to end up with this structure
key->key->value
and not
key->key->single_element_array->value
I'm not sure how I end up with an array at the bottom level when I do this
return [$item['key'] => $item['value']];
inside the mapToGroups, which is a function found here: https://laravel.com/docs/5.8/collections#method-maptogroups
Maybe I misunderstand how mapToGroups work. Anybody has an idea how to get key->key->value structure output?
Use mapWithKeys() instead of mapToGroups().
You're getting an array instead of the simple value you expect because the value is a group, albeit a group with only one member.
mapToGroups() groups all the values with the same key, but mapWithKeys() will assign a single value to each key.
You can see in the examples in the collection documentation, mapToGroups() produces a result like this:
[
'Sales' => ['John Doe', 'Jane Doe'],
'Marketing' => ['Johnny Doe'],
]
And mapWithKeys() result is like this:
[
'john#example.com' => 'John',
'jane#example.com' => 'Jane',
]
I'm working in PHP with what are essentially JSON objects (Highchart configs to be specific). So I might have something like the following:
$chart = [
'plotOptions' => [
'column' => [
'dataLabels' => [
'enabled' => true
]
]
],
'legend' => [
'enabled' => false
],
'yAxis' => [
'title' => [
'text' => null
]
]
];
I'm writing some code to build these config objects in the PHP and it's a pain to set nested properties because I have to check if the parent exists. For example, if I wanted to add a title to the xAxis to the config above, I'd have to do this:
if(!array_key_exists('xAxis', $chart)){
$chart['xAxis'] = [];
}
$chart['xAxis']['title'] = 'x title';
This is especially annoying when I have a really deep property to set because I have to do a "array_key_exists" for each level.
I'm wondering if anyone has any idea on how to make this simpler and cleaner. Thanks.
You can use a simple function to create associative keys automatically.
function setKey($key, $value, &$array) {
$path = explode('.', $key);
$arrayContext =& $array;
foreach( $path as $segment ) {
if( !isset($arrayContext[$segment]) ) {
$arrayContext[$segment] = [];
}
$arrayContext =& $arrayContext[$segment];
}
$arrayContext = $value;
}
Then use dot notation to set the key value within the array (e.g. "xAxis.title" is the same as $chart['xAxis']['title']):
setKey('xAxis.title', 'x title', $chart);
This can be read as "set the value of the key xAxis -> title to 'x title' in the $chart array variable."
I have a query that populates an array from the database. In some cases, this query returns a great amount of data, (let's say for purpose of an example, 100.000 records). Each row of the database has at least 6 or 7 columns.
$results = [
['id' => 1, 'name' => 'name', 'status' => true, 'date' => '10-01-2012'],
['id' => 2, 'name' => 'name 2', 'status' => false 'date' => '10-01-2013'],
...
]
I need to perform a substitution of some of the data inside the $results array, based on another one that give me some information about how i would change the values in the rows.
$model = [
'status' => ['function' => 'formatStatus', params => ['status']],
'date' => ['function' => 'formatDate', params => ['date']]
]
Now that i have all the data and what do i do with it i have the following routine.
foreach ($results as &$itemResult) {
$oldValues = $itemResult;
foreach ($itemResult as $attribute => &$value) {
if (isset($model[$attribute]['function'])) {
$function = $model[$attribute]['function'];
$params = $model[$attribute]['params'];
$paramsSize = count($params);
for ($i = 0; $i < $paramsSize; $i++) {
$newParams[] = $oldValues[$params[$i]];
}
$itemResult[$attribute] = call_user_func_array([$this, $function], $newParams);
$newParams = null;
}
}
}
So, for each attribute for each row of my data array, i run check for the existence of a function and params information. When the attribute in question needs to be replaced, i call the function via call_user_func_array and replace the value with the function return value.
Also notice that i am replacing the current array, not creating another, by passing the reference &$itemResult inside the loop, so in the end, i have the same array from the beginning but with all columns that needed to be replaced with its new values.
The thing is, for little arrays, this method is quite good. But for big ones, it becomes a pain.
Could you guys provide me some alternative to the problem?
Should i use another data structure instead of the PHP array?