Get data from array to array in magento 2 - magento2

This is my array
array(5) {
[0]=>
string(2) "10"
[1]=>
string(2) "22"
[2]=>
string(2) "25"
[3]=>
string(2) "37"
[4]=>
string(2) "38"
}
How to get it to array like this
array(1) {
[0]=>
string(2) "10"
}
array(2) {
[0]=>
string(2) "22"
}
array(3) {
[0]=>
string(2) "25"
}
array(4) {
[0]=>
string(2) "37"
}
array(5) {
[0]=>
string(2) "38"
}
I'm using this code
$sellerIds = * My Array *
if(is_array($sellerIds)){
foreach($sellerIds as $key => $value){
$sellerIds = $value;
}
}
// do something with $sellerIds
But when i var_dump it, it like this
string(2) "10"
string(2) "22"
...
Please Help Me ! Thanks All

Using array_chunk function :-
print_r(array_chunk($input_array,1,true)); //pass your Array
array_chunk

$array=array("10","22","25","37","38");
$finalArrays=(array_chunk($array, 1,true));
foreach($finalArrays as $value)
{
var_dump($value);
}

Related

Drop a key-value pair from document when it isn't exist and update when it is exist

Insert this
db.test.insertMany([{"_id":"T1","us":"a11","jp":"b21"},{"_id":"T2","us":"a12","jp":"b22"},{"_id":"T3","us":"a13","jp":"b23"},{"_id":"T4","us":"a14","jp":"b24"}])
My data looks like
{ "_id" : "T1", "us": "a11", "jp": "b21" }
{ "_id" : "T2", "us": "a12", "jp": "b22" }
{ "_id" : "T3", "us": "a13", "jp": "b23" }
{ "_id" : "T4", "us": "a14", "jp": "b24" }
I have a key list, and a key-value list
pks=['T1', 'T2']
kvs=[{"us": "a88"}, {"us": "a99"}]
If there are another key in my documents, ex: 'cn', 'uk', kvs will change to
kvs=[{"jp": "b55"}, {"jp": "b99"}]
The key in the kvs list will be the same. But may be change another time.
Use kvs to update if _id in pks then change the value of us,
or else delete us from all documents that its _id not in pks
That will result
{ "_id" : "T1", "us": "a88", "jp": "b21" }
{ "_id" : "T2", "us": "a99", "jp": "b22" }
{ "_id" : "T3", , "jp": "b23" }
{ "_id" : "T4", , "jp": "b24" }
Is there an API I can use?
How to do?
Or if it is to hard to solve the problem, change kvs or pks is ok.
Thanks in advance.
if you don't care about performance
here is my answer(pymongo)
pks=['T1', 'T2']
kvs=[{"us": "a88"}, {"us": "a99"}]
for i in range(len(pks)):
db.test.update_many( {'$or': [{'_id': pks[i]}, {'cn': pks[i]} ]},
{ '$set': { list(kvs[i].keys())[0]: list(kvs[i].values())[0] } })
db.test.update_many({'$and': [{'_id': {'$nin': pks}}, {'cn': {'$nin': pks}}]},
{'$unset': { list(kvs[i].keys())[0] : 1 } })

Product with tags with pivot how can i access relationship

The following $product returns Product object.
But how can i access the relations:array "tags" by foreach?
When i do $tags->tags i get the tags of the product table.
Foreach:
foreach ($product as $tags) {
}
Instance:
$product = Product::with('tags')->where('id',$id)->get();
Output:
Product {#371 ▼
#table: "products"
#connection: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:10 [▶]
#original: array:10 [▶]
#relations: array:1 [▼
"tags" => Collection {#399 ▼
#items: array:2 [▼
0 => Tag {#397 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:4 [▶]
#original: array:6 [▶]
#relations: array:1 [▶]
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
Use first instead of get to get a model instance instead of collection.
$product = Product::with('tags')->where('id', $id)->first();
Then you can loop through each tag.
foreach ($product->tags as $tag) {
// do something
}

Extracting sub string using Perl

I have string as follows.
{
"id": "1304145"
,"Name" : "Ravi"
,"State" : "KAR"
,"Comp" : "CTL"
}
,{
"id": "2488398"
,"Name" : "Abhik"
,"State" : "TEL"
,"Comp" : "WFG"
}
,{
"id": "89039487"
,"Name" : "Jana"
,"State" : "ODS"
,"Comp" : "TOT"
}
I want to extract each of the sub-string present in a {} and make a format as follws
ID Name State Comp
1304145 Ravi KAR CTL
2488398 Abhik TEL WFG
89039487 Jana ODS TOT
I tried it, but my scripts are not giving the exact outputs.
Need your help in solving this.
Looks a lot like JSON.
If it is:
#!/usr/bin/env perl
use strict;
use warnings 'all';
use JSON;
my $json = from_json(do{local $/;<DATA>});
my #header = qw ( id Name State Comp );
print join ( "\t", #header ), "\n";
foreach my $row ( #$json ) {
print join ( "\t",#{$row}{#header} ),"\n";
}
__DATA__
[{
"id": "1304145"
,"Name" : "Ravi"
,"State" : "KAR"
,"Comp" : "CTL"
},
{
"id": "2488398"
,"Name" : "Abhik"
,"State" : "TEL"
,"Comp" : "WFG"
}
,{
"id": "89039487"
,"Name" : "Jana"
,"State" : "ODS"
,"Comp" : "TOT"
}
]
Gives:
id Name State Comp
1304145 Ravi KAR CTL
2488398 Abhik TEL WFG
89039487 Jana ODS TOT
That's assuming that you do have JSON though - I had to add [] around your snippet. If it isn't JSON then someone's done something dirty, in creating something that looks a lot like JSON, but that isn't.
If for some reason the data isn't supposed to be JSON, and the keys and values are always enclosed in double-quotes, then you can parse it easily with regular expressions
use strict;
use warnings 'all';
my $s = <<END_STRING;
{
"id": "1304145"
,"Name" : "Ravi"
,"State" : "KAR"
,"Comp" : "CTL"
}
,{
"id": "2488398"
,"Name" : "Abhik"
,"State" : "TEL"
,"Comp" : "WFG"
}
,{
"id": "89039487"
,"Name" : "Jana"
,"State" : "ODS"
,"Comp" : "TOT"
}
END_STRING
my #data = map { { /"([^"]*)"/g } } $s =~ /\{[^{}]*\}/g;
my $fmt = "%-11s %-7s %-7s %s\n";
printf $fmt, qw/ ID Name State Comp /;
for my $item ( #data ) {
printf $fmt, #{$item}{qw/ id Name State Comp /};
}
output
ID Name State Comp
1304145 Ravi KAR CTL
2488398 Abhik TEL WFG
89039487 Jana ODS TOT

Spring Data MongoDB Index Query on Sub Document

In the MongoDB Documentation we've got the following example:
db.factories.insert( { name: "xyz", metro: { city: "New York", state: "NY" } } );
db.factories.ensureIndex( { metro : 1 } );
// this query can use the above index:
db.factories.find( { metro: { city: "New York", state: "NY" } } );
I'm currently looking for a solution to archieve this query in spring data. My first approach was
Query.query(Criteria.where("metro")
.andOperator(Criteria.where("city").is("New York")
.and("state").is("NY")))
but this results in the following query
{ "metro" : { } , "$and" : [ { "city" : "New York" , "state" : "NY"}]}
Finally i found the solution by fiddling around:
Query.query(Criteria.where("metro").is(
Query.query(
Criteria.where("city").is("New York")
.and("state").is("NY")).getQueryObject()
)
)
// results in { metro: { city: "New York", state: "NY" } }
Hopefully this is the right way to do it...

MongoDB does not get updated

I use Doctrine 2 ODM\MongoDB (this and dependencies all latest from GIT) + PHP Mongo extension (v1.1.4) + Mongo (v1.8.3 x64 latest)
In several cases it happens that when I change an entity, it does not get updated
In an embedded entity I have a back reference to it's parent, to solve http://groups.google.com/group/doctrine-dev/browse_thread/thread/016422b2cc9dcec9?pli=1 (How to get parent document inside embedded document?) without using internal functionalities like UnitOfWork - this MAY or MAY NOT be relevant, I do not know... I would hope that not (my question whether it could went unanswered on the IRC so far)
However a single change in one of Character's embedded documents triggers these queries (should really be relevant only to it, there's been flush before it and a condition inside the logger on the flag that was set again right before the change)
Logger dump:
array(6) {
"update" => TRUE
"query" => array(1) {
"_id" => MongoId(1) {
"$id" => "4e3143be662c32400d000006" (24)
}
}
"newObj" => array(1) {
"$set" => array(2) {
"components.destructible.hp" => 99.28706276
"options.character" => array(3) {
"$ref" => "characters" (10)
"$id" => MongoId(1) { ... }
"$db" => "nuclearRain" (11)
}
}
}
"options" => array(0)
"db" => "someDB" (11)
"collection" => "characters" (10)
}
array(6) {
"update" => TRUE
"query" => array(1) {
"_id" => MongoId(1) {
"$id" => "4e553306662c32680800054b" (24)
}
}
"newObj" => array(1) {
"$set" => array(1) {
"createdBy" => array(3) {
"$ref" => "characters" (10)
"$id" => MongoId(1) { ... }
"$db" => "nuclearRain" (11)
}
}
}
"options" => array(0)
"db" => "someDB" (11)
"collection" => "worlds" (6)
}
array(6) {
"update" => TRUE
"query" => array(1) {
"_id" => MongoId(1) {
"$id" => "4e553306662c32680800054c" (24)
}
}
"newObj" => array(1) {
"$set" => array(1) {
"world" => array(3) {
"$ref" => "worlds" (6)
"$id" => MongoId(1) { ... }
"$db" => "nuclearRain" (11)
}
}
}
"options" => array(0)
"db" => "someDB" (11)
"collection" => "games" (5)
}
Initialization code: http://pastebin.com/1dKagqry
"components.destructible.hp" => 99.28706276 is the core part -> yet this change is never reflected in the DB
there is no php error/exception triggered
mongo's log shows no errors
generally the Mongo and Doctrine\ODM\Mongo runs fine, entities get persisted, updated, deleted...
Where can I search further or what can I do to solve this issue
Thank you
It is awkward, but the issue is that I wanted the EmbeddedDocuments indexed (like associative array)... this was however done on an actual Doctrine-maintained property, so it triggered re-pushing the EmbeddedDocument, discarding the changes
$friend = Zend_Registry::get('doctrine')->getDocumentManager()->createQueryBuilder('App\document\Message')->update()->field('unread')->set(TRUE)->field('viewer_id')->equals(10001)-> getQuery(array('multiple' => true))->execute();