PHP Append array in nested array - php

I have below array, I need to append a new array inside $newData['_embedded']['settings']['web/vacation/filters']['data'], How can I access and append inside it ?
$newData = [
"id" => "47964173",
"email" => "abced#gmail.com",
"firstName" => "Muhammad",
"lastName" => "Taqi",
"type" => "employee",
"_embedded" => [
"settings" => [
[
"alias" => "web/essentials",
"data" => [],
"dateUpdated" => "2017-08-16T08:54:11Z"
],
[
"alias" => "web/personalization",
"data" => [],
"dateUpdated" => "2016-07-14T10:31:46Z"
],
[
"alias" => "wizard/login",
"data" => [],
"dateUpdated" => "2016-09-26T07:56:43Z"
],
[
"alias" => "web/vacation/filters",
"data" => [
"test" => [
"type" => "teams",
"value" => [
0 => "09b285ec-7687-fc95-2630-82d321764ea7",
1 => "0bf117b4-668b-a9da-72d4-66407be64a56",
2 => "16f30bfb-060b-360f-168e-1ddff04ef5cd"
],
],
"multiple teams" => [
"type" => "teams",
"value" => [
0 => "359c0f53-c9c3-3f88-87e3-aa9ec2748313"
]
]
],
"dateUpdated" => "2017-07-03T09:10:36Z"
],
[
"alias" => "web/vacation/state",
"data" => [],
"dateUpdated" => "2016-12-08T06:58:57Z"
]
]
]
];
$newData['_embedded']['settings']['web/vacation/filters']['data'] = $newArray;
Any Hint to quickly append it, I don't want to loop-in and check for keys inside loops.

The settings subarray is "indexed". You first need to search the alias column of the subarray for web/vacation/filters to find the correct index. Using a foreach loop without a break will mean your code will continue to iterate even after the index is found (bad coding practice).
There is a cleaner way that avoids a loop & condition & break, use array_search(array_column()). It will seek your associative element, return the index, and immediately stop seeking.
You can use the + operator to add the new data to the subarray. This avoids calling a function like array_merge().
Code: (Demo)
if(($index=array_search('web/vacation/filters',array_column($newData['_embedded']['settings'],'alias')))!==false){
$newData['_embedded']['settings'][$index]['data']+=$newArray;
}
var_export($newData);
Perhaps a more considered process would be to force the insert of the new data when the search returns no match, rather than just flagging the process as unsuccessful. You may have to tweak the date generation for your specific timezone or whatever... (Demo Link)
$newArray=["test2"=>[
"type" =>"teams2",
"value" => [
0 => "09b285ec-7687-fc95-2630-82d321764ea7",
1 => "0bf117b4-668b-a9da-72d4-66407be64a56",
2 => "16f30bfb-060b-360f-168e-1ddff04ef5cd"
],
]
];
if(($index=array_search('web/vacation/filters',array_column($newData['_embedded']['settings'],'alias')))!==false){
//echo $index;
$newData['_embedded']['settings'][$index]['data']+=$newArray;
}else{
//echo "couldn't find index, inserting new subarray";
$dt = new DateTime();
$dt->setTimeZone(new DateTimeZone('UTC')); // or whatever you are using
$stamp=$dt->format('Y-m-d\TH-i-s\Z');
$newData['_embedded']['settings'][]=[
"alias" => "web/vacation/filters",
"data" => $newArray,
"dateUpdated" => $stamp
];
}

You need to find the key that corresponds to web/vacation/filters. For Example you could use this.
foreach ($newData['_embedded']['settings'] as $key => $value) {
if ($value["alias"]==='web/vacation/filters') {
$indexOfWVF = $key;
}
}
$newData['_embedded']['settings'][$indexOfWVF]['data'][] = $newArray;
From the comments. Then you want to merge the arrays. Not append them.
$newData['_embedded']['settings'][$indexOfWVF]['data'] = array_merge($newData['_embedded']['settings'][$indexOfWVF]['data'],$newArray);
Or (if it's always Filter1):
$newData['_embedded']['settings'][$indexOfWVF]['data']['Filter1'] = $newArray['Filter1'];

Related

How to extract each level in nested array to separated arrays with specific key-value pairs?

I've got a 4-level nested array, each level represent a hiereachy in an organization. Each level's key is the hierarchy value, and inside it there is the id of it in the array. The last level is only the value-id pair:
$data = [
"top_level_data1" => [
'top_level_id' => 0,
'sub_level_1' => [
'sub_level_1_id' => 0,
'sub_level_2_data1' => [
'sub_level_2_id' => 0,
'sub_level_2_data' => [
0 => "some_val"
1 => "some_other_val"
]
]
]
],
"top_level_data2" => [
'level_1_id' => 1,
'sub_level_1_other' => [
'sub_level_1_id' => 1,
'sub_level_2_data2' => [
'sub_level_2_id' => 1,
'sub_level_3_data1' => [
2 => "another_val"
3 => "bar"
4 => "foo"
]
],
'sub_level_2_data3' => [
'sub_level_2_id' => 2,
'sub_level_3_data2' => [
5 => "foobar"
6 => "hello"
7 => "goodbye"
]
]
]
]
];
I want to extract it to separate arrays that would contain the hierarchy value-id pairs.
Expected output from the above example (without the ids of the above level):
$top_level = [
0 => "top_level_data1",
1 => "top_level_data2"
]
$sub_level_1 = [
0 => "sub_level_1",
1 => "sub_level_1_other"
]
$sub_level_2 = [
0 => "sub_level_2_data1",
1 => "sub_level_2_data2",
2 => "sub_level_2_data3"
]
$sub_level_3 = [
0 => "some_val"
1 => "some_other_val"
2 => "another_val"
3 => "bar"
4 => "foo"
5 => "foobar"
6 => "hello"
7 => "goodbye"
]
I've tried using the RecursiveIterator but that does not work (At first I wanted to use it just to check if it iterates correctly, even before assignting the key-values as I wanted, but it just doesn't do it as I wanted):
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
foreach($it as $key => $val) {
$level[$it->getDepth()][] = $val;
}
This can do it:
$result = [];
$recursive = null;
$recursive = function($args,$level=0) use (&$recursive,&$result){
foreach($args as $k=>$v){
if(is_array($v)){
$result[$level][]=$k;
$recursive($v,$level+1);
} else if(is_string($v)){
$result[$level][]=$v;
}
}
};
$recursive($data);
var_export($result);
But your expected output isn't ok. Because i think sub_level_2_data must be sub_level_3_data (in the $data array above). And the hole sub_level_3*level is missing in your expected output.
With this code you get an result array instead of separate filled variables.
Will not explain it in detail, but try to understand whats happening here and you can learn some new php stuff.

How do i merge these arrays into one [duplicate]

This question already has answers here:
Merge two arrays into one associative array
(2 answers)
Closed 7 months ago.
I want to change the following php array
"extra_charge_item" => [
0 => "Massage",
1 => "Pool table",
2 => "Laundry"
],
"extra_charge_description" => [
0 => "Paid",
1 => "Paid",
2 => "We wash everything"
],
"extra_charge_price" => [
0 => "200",
1 => "100",
2 => "1000"
],
I haven't been able to solve for a whole 2hrs
This is the expected output
"new_data" => [
0 => [
"Maasage", "Paid", "200"
],
1 => [
"Pool table", "Paid", "100"
],
2 => [
"Laundry", "we wash everything", "1000"
]
]
Rather than doing all the work for you, here's some pointers on one way to approach this:
If you are happy to assume that all three sub-arrays have the same number of items, you can use array_keys to get those keys from whichever you want.
Once you have those keys, you can use a foreach loop to look at each in turn.
For each key, use square bracket syntax to pluck the three items you need.
Use [$foo, $bar, $baz] or array($foo, $bar, $baz) to create a new array.
Assign that array to your final output array, using the key from your foreach loop.
Just use foreach with key => value
$data = [
"extra_charge_item" => [
0 => "Massage",
1 => "Pool table",
2 => "Laundry"
],
"extra_charge_description" => [
0 => "Paid",
1 => "Paid",
2 => "We wash everything"
],
"extra_charge_price" => [
0 => "200",
1 => "100",
2 => "1000"
],
];
$newData = [];
foreach ($data as $value) {
foreach ($value as $k => $v) {
$newData[$k][] = $v;
}
}
var_dump($newData);
I found an answer. Seems someone else had the same problem
Merge two arrays into one associative array
Here's the solution for my case;
//These are arrays passed from front end in the name attribute e.g extra_charge_item[] e.t.c
$extra_charge_item_array = $request->input('extra_charge_item');
$extra_charge_description_array = $request->input('extra_charge_description');
$extra_charge_price_array = $request->input('extra_charge_price');
$new_extra_charges_data = array_map(
function ($item, $description, $price) {
return [
'item' => $item,
'description' => $description,
'price'=> $price
];
}, $extra_charge_item_array, $extra_charge_description_array, $extra_charge_price_array);
//save the data
foreach ($new_extra_charges_data as $extra_charge) {
Charge::create([
'item' => $extra_charge['item'],
'description' => $extra_charge['description'],
'price' => $extra_charge['price']
]);
}

PHP MongoDB aggregate $match and $group and $addToSet

Having this document structure in MongoDB :
{
"_id":<MongoDBID>,
"chatUser1ID": 2,
"chatUser2ID": 3
}
Now i want to get all chat partners from Mongo where the Chat Partner with the ID 2 is included in either "chatUser1" or "chatUser2". For that i want to use the $match and $group function.
$chatUserID = $_POST["chatUserID"]; // 2 in my example
$chatCursor = $chatCollection->aggregate([
[
'$match' =>
[
'$or' =>
[
["chatUser1ID" => $chatUserID],
["chatUser2ID" => $chatUserID]
]
]
]
,[
'$group' =>
[
'_id' => 0,
'chatUsers' => ['$addToSet' => '$chatUser1ID'],
'chatUsers' => ['$addToSet' => '$chatUser2ID'],
'chatUsers1' => ['$addToSet' => '$chatUser1ID'],
'chatUsers2' => ['$addToSet' => '$chatUser2ID'],
]
]
]);
'chatUsers1' => ['$addToSet' => '$chatUser1ID'],
This is putting all the ID's of the field chatUser1ID in the chatUsers1 set and
'chatUsers2' => ['$addToSet' => '$chatUser2ID']
is putting all all the ID's of the field chatUser2ID in the chatUsers2 set where the $chatUserID is either in the chatUser1ID or chatUser2ID field of the document.
After that i want want to get the unique ID's
$chatUserIDs = array();
foreach ($chatCursor as $counter => $document) {
$bson = MongoDB\BSON\fromPHP($document);
$value = MongoDB\BSON\toJSON($bson);
$value = json_decode($value, true);
$chatUserIDs = array_unique(array_merge($value['chatUsers1'], $value['chatUsers2']));
}
unset($chatUserIDs[array_search($chatUserID, $chatUserIDs)]);
array_unshift($chatUserIDs);
So basically it's working but i want the solution where i get a list of unique ID's right away from the database.
Originally i thought the lines
'chatUsers' => ['$addToSet' => '$chatUser1ID'],
'chatUsers' => ['$addToSet' => '$chatUser2ID'],
would add the ID's to the set chatUsers but unfortunately the set is overwritten in the second line. Is there a way to "append" the ID's to the set instead of overwrite them ? And maybe a way where i can exclude the $chatUserID because i want only the chatPartners.
Thanks in advance.
If you don't care about the order they appear in, you can build two arrays of user1 and user2, then concat them together in a later stage. This won't handle deduplicating though.
$chatUserID = $_POST["chatUserID"]; // 2 in my example
$chatCursor = $chatCollection->aggregate([
[
'$match' => [
'$or' =>[
["chatUser1ID" => $chatUserID],
["chatUser2ID" => $chatUserID]
]
]
], [
'$group' => [
'_id' => 0,
'chatUsers1' => ['$addToSet' => '$chatUser1ID'],
'chatUsers2' => ['$addToSet' => '$chatUser2ID'],
]
], [
'$addFields' => [
'chatUsers' => [
'$concatArrays' => [
'$chatUsers1',
'$chatUsers2'
]
]
]
],
]);

How can I manage some irregular array to regular array?

I write code with some array that have different structure, but I must extract the data to do something else. How can I manager these array?
The array's structure are as follow:
$a = [
'pos1' => 'somedata',
'pos2' => ['data2', 'data3'],
'pos3' => '';
];
$b = [
[
'pos1' => ['data1', 'data2', ['nest1', 'nest2']],
'pos2' => ['data1', 'data2', 'data3'],
],
['data1', 'data2'],
'data4',
];
The array's Index can be a key or a position, and the value of the corresponding index may be a array with the same structure. More tough problem is that the subarray can be nesting, and the time of the nesting has different length.
Fortunately, every array has it's owe fixed structure.
I want to convert the these array to the format as follow. When the index is a value, change it to the keyword; and if the index is a keyword, nothing changed.
$a = [
'pos1' => 'somedata',
'pos2' => [
'pos2_1' => 'data2',
'pos2_2' => 'data3'
],
'pos3' => '';
];
$b = [
'pos1' => [
'pos1_1' => [
'pos1_1_1' => 'data1',
'pos1_1_2' => 'data2',
'pos1_1_3' => [
'pos1_1_3_1' => 'nest1',
'pos1_1_3_2' => 'nest2',
],
],
'pos1_2' => [
'pos1_2_1' => 'data1',
'pos1_2_2' => 'data2',
'pos1_2_3' => 'data3',
],
],
'pos2' => ['data1', 'data2'],
'pos3' => 'data4',
];
My first solution is for every array, write the function to convert the format(the keyword will specify in function). But it is a huge task and diffcult to manage.
The second solution is write a common function, with two argument: the source array and the configuration that specify the keyword to correspondent value index. For example:
$a = [0, ['pos10' => 1]];
$conf = [
// It means that when the value index is 0, it will change it into 'pos1'
'pos1' => 0,
'pos2' => 1,
];
The common funciton will generate the result of:
$result = [
'pos1' => 0,
'pos2' => ['pos10' => 1],
]
But this solution will lead to a problem: the config is diffcult to understand and design, and other people will spend a lot of time to understand the format after conversion.
Is there are some better solution to manage these array that other people can easy to use these array?
Thanks.

Elasticsearch index_options=docs for text data type does not return any search results

I have been using Elasticsearch 7.6 and PHP client API for all the operations.
I have created elasticsearch index settings and mappings as follows
$params = [
'index' => 'elasticindex',
'body' => [
'settings' => [
"number_of_shards" => 1,
"number_of_replicas" => 0,
"index.queries.cache.enabled" => false,
"index.soft_deletes.enabled" => false,
"index.requests.cache.enable" => false,
"index.refresh_interval" => -1
],
'mappings' => [
'_source' => [
"enabled" => false
],
'properties' => [
"text" => [
"type" => "text",
"index_options" => "docs"
]
]
]
]
];
I was able to index document using the following code
$params = array();
$params['index'] = 'elasticindex';
for($i = 1; $i <=2; $i++) {
$params['id'] = $i;
$params['body']['text'] = 'apple';
$responses = $client->index($params);
}
But when I use the following search query
$params = [
'index' => 'elasticindex',
'body' => [
'query' => [
'match' => [
"text" => "apple"
]
]
]
];
$results = $client->search($params);
I am getting empty results as follows
Array
(
[took] => 3
[timed_out] =>
[_shards] => Array
(
[total] => 1
[successful] => 1
[skipped] => 0
[failed] => 0
)
[hits] => Array
(
[total] => Array
(
[value] => 0
[relation] => eq
)
[max_score] =>
[hits] => Array
(
)
)
)
Without creating a static index template, if I try to index, elasticsearch dynamic mapping works well and I am getting the results.
The goal is that I want the elasticsearch to index only document id in its inverted index and not position or offset and I want to retrieve only matching document ids as results. Help is much appreciated. Thanks in advance!
Since the document is being returned by the get handler and not the query handler, your index is not being refreshed properly after indexing the document.
As you noted yourself, in your configuration you set:
"index.refresh_interval" => -1
.. which means that the index is not being refreshed automagically. There's seldom a need to change the refresh interval, except in very high throughput situations or where a particular behavior is wanted.
Try to index doc something like this.
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'elasticindex',
'type' => 'documents',
'id' => '1',
'body' => ['text' => 'apple']
];
$response = $client->index($params);
print_r($response);
Note: _id you can define dynamically if you want, else id will automatically set by elastic.
And try to get doc via a search query.
{
"query": {
"bool": {
"must": {
"term": {
"text": "apple"
}
}
}
}
}
If you want full-text search on this key, set this key property as
"properties": {
"name": {
"type": "text"
}
}

Categories