Echo "part" of array (decoded from JSON) - arrays

I'am quite new to JSON and more "advanced" arrays. Therefore I don't know what I should search for...
I have this "JSON array" (what do you call it?):
{
"id": "321123321",
"statuses": {
"data": [
{
"message": "testmessage",
"updated_time": "2012-12-25T16:33:29+0000",
"id": "123321123"
}
],
"paging": {
"previous": "1",
"next": "1"
}
}
}​
I want to create a variable from "message" that is called $message and a variable from "up_datedtime" that is called $updated.
To get id I simple:
$json_a=json_decode($string,true);
$id $json_a['id'];
And for statuses:
$json_a=json_decode($string,true);
$status = $json_a['id']['statuses'];
But when I try to get "message" I get " Cannot use string offset as an array in":
$message = $json_a['id']['statuses']['data']['message'];
How do I get $message from the array the proper way?

You can get like this
$message = $json_a['id']['statuses']['data'][0]['message'];
or you can get from loop
$dataArr = $json_a['id']['statuses']['data'];
foreach ($dataArr as $val) {
echo "message".$val['message'];
}

Related

Remove duplicates from different JSON Arrays

I have a JSON structure that has a lot of arrays.I want to check if there are duplicates.Not inside the same array but in different arrays.Also i want the other fields to stay as is.
Here is an example of my structure:
{
"Collection":[
{
"field0":"string",
"field1":"string",
"field2":"string",
"field3":"string",
"field4":"string",
"field5":"string",
"field6":"string",
"field7":"string",
"field8":"string",
"field9":[
"test1"
"test2"
"test3"
]
},
{
"field0":"string",
"field1":"string",
"field2":"string",
"field3":"string",
"field4":"string",
"field5":"string",
"field6":"string",
"field7":"string",
"field8":"string",
"field9":[
"test8"
"test2"
"test9"
]
}
]
}
And here is what I expect:
{
"Collection":[
{
"field0":"string",
"field1":"string",
"field2":"string",
"field3":"string",
"field4":"string",
"field5":"string",
"field6":"string",
"field7":"string",
"field8":"string",
"field9":[
"test1"
"test2"
"test3"
]
},
{
"field0":"string",
"field1":"string",
"field2":"string",
"field3":"string",
"field4":"string",
"field5":"string",
"field6":"string",
"field7":"string",
"field8":"string",
"field9":[
"test8"
"test9"
]
}
]
}
I don't know if this is relevant but this is a firestore collection.
O'k, I don't know Swift, but I can show you how to do it:
<?php
$json = <<<JSON
{
"Collection":[
{
"field1":[
"test1",
"test2",
"test3"
]
},
{
"field2":[
"test8",
"test2",
"test9"
]
}
]
}
JSON;
$entities = json_decode($json, true); // Decode JSON
$collection = $entities['Collection']; // Grab array of fields inside collection
$elements = []; // Initialize an empty array of unique elements
$result = [];
foreach ($collection as $index => $fieldObject) {
$fieldName = array_keys($fieldObject)[0]; // Get field name
// Get value from array of values of this field
foreach ($fieldObject[$fieldName] as $valueKey => $value) {
// Check if your value is not in array of unique elements
if (!in_array($value, $elements)) {
$elements[] = $value; // Add value if is not
// Add value to your new array
$result['Collection'][$index][$fieldName][] = $fieldObject[$fieldName][$valueKey];
}
}
}
$result = json_encode($result); // Encode it back to JSON
Here is the working example in sandbox: http://sandbox.onlinephpfunctions.com/code/a78cff49cc31c15e7e1373f7e1f66b7951f129e9

How to update the document on solr 6.0?

I am using solr 6.0 version.
This is my data
{
"id" : "14",
"solrid" : "solr|school|14",
"name" : "test update solr 14",
"status" : "pending",
"state" : "Andhra Pradesh",
"board" : "CISCE",
"updated" : "2016-05-26T02:24:25Z",
"pincode" : "0"
}
I want to update the data on document as per id.
example i want to change the name
$doc = $update->createDocument();
$doc["id"] =$id;
$doc["name"]="school";
$update->addDocument($doc);
$update->addCommit();
$client->update($update);
This code is correct? Or i want to use other flow.
PHP Solarium code.
Yes, basically the flow is correct. You can always check the solarim examples gist at https://gist.github.com/basdenooijer/894286.
Alternatively you can do something like this:
$update = $client->createUpdate();
$document = $update->createDocument();
$document->setKey('id', $id);
foreach ($data as $field => $value) {
if ($field == 'id') {
continue;
}
$document->setField($field, $value, null, 'set');
}
$update->addDocument($document, true, 1500);
$result = $client->update($update);

in array, json data and put in MySql with php

I have a data under format 'JSON' like this one :
{
"email": "john#john.fr",
"line_items": [
{
"sku": "123456789",
"price": "0.67",
"price_with_tax": "4.00",
"tax_lines": [
{
"title": "tax010",
"rate": 0.01,
"price": "1.11"
},
{
"title": "tax00200",
"rate": 0.02,
"price": "2.22"
}
]
},
{
"sku": "012345666",
"price": "1.67",
"price_with_tax": "5.00",
"tax_lines": [
{
"title": "tax0003000",
"rate": 0.03,
"price": "3.33"
}
]
}
]
}
I want put it in my database (MySql) by PDO::prepare. My following sql query works but second line_items have not good value im Mysql :
1st item :::: good value
email:john#john.fr
sku:123456789
price:0.67
price_with_tax:4.00
price_tax1:1.11
price_tax2:2.22
2nd item ::::
email:john#john.fr
sku:012345666
price:1.67
price_with_tax:5.00
wrong value ::::
price_tax1:1.11
price_tax2:2.22
How can I put it good value ?
here is my code :
$dataDecode = json_decode($jsondata);
$email = $dataDecode->email;
try
{
$dtbs = new PDO($dsn_dev, $pdo_user, $pdo_password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (Exception $e)
{
die('Error : ' . $e->getMessage());
}
try
{
foreach ($dataDecode->line_items as $obj)
{
$var_sku = $obj->sku;
$var_price = $obj->price;
$var_price_with_tax = $obj->price_with_tax;
$taxNewArray = array();
foreach ($dataDecode->line_items[0]->tax_lines as $obj2)
{
array_push($taxNewArray , $obj2);
}
$val1st = array_shift($taxNewArray);
$val2nd = array_pop ($taxNewArray);
$var_tax1 = $val1st->price;
$var_tax2 = $val2nd->price;
$stmt = $dtbs->prepare("INSERT INTO $tabledata ($mysql_email, $mysql_sku, $mysq_price, $mysql_price_with_tax, $mysql_price__tax1___line_items, $mysql_price__tax2___line_items)
VALUES (:email, :sku, :price, :price_with_tax, :price_tax1, :price_tax2)");
$stmt->execute(array(':email'=>$email,
':sku'=>$var_sku,
':price'=>$var_price,
':price_with_tax'=>$var_price_with_tax,
':price_tax1'=>$var_tax1,
':price_tax2'=>$var_tax2
));
}
}
catch(Exception $e)
{
throw $e;
}
Do you have a idée ?
If there's only one entry in tax_lines, your code will try to set $var_tax2 from the nonexistent second entry. You need to check for this and substitute some other value:
$var_tax2 = $val2nd ? $val2nd->price : '0.0';
The other problem is this line:
foreach ($dataDecode->line_items[0]->tax_lines as $obj2)
You're using the tax lines from the first line item every time. That should be:
foreach ($obj->tax_lines as $obj2)

how to add a document to an array in mongodb using powershell code?

I would like to insert a document into an array within another document in Powershell.
e.g.
{
"1" : "A",
"2" : "B",
"3" : [
{
"a" : "AA",
"_id" : ObjectId("5333b74c9e99569836bca41f")
}
],
"_id" : ObjectId("5333b74c9e99569836bca41c")
}
this is my document in mongodb shell.
{
"1" : "A",
"2" : "B",
"3" : [ ]
"_id" : ObjectId("5333b74c9e99569836bca41c")
}
and this is my code in powershell:
$embededDocument = new-object Mongodb.Bson.BsonDocument
$embededDocument.Add("a", "AA")
$embededDocument.Add("B2", "2")
$embededDocument.Add("C2", "3")
$parentDocument["3"].Add($embededDocument)
this is not working as the array is empty after run the code, I tried using Add, Insert and Push with same result...
Any idea what I'm doing wrong?
thank you very much
Basically you are trying to do an update of the type that is shown for the $push operator, as shown in the documentation. So you need to construct the same sort of BSON document structure as shown there:
$query = [MongoDB.Driver.Builders.Query]::EQ("_id" :
[MongoDB.Bson.BsonObjectId]::Create("5333b74c9e99569836bca41c"))
$doc = new-object MongoDB.Bson.Document
$doc.Add("a", "AA")
$inner = new-object MongoDB.Bson.Document
$inner.Add( "3", $doc )
$update = new-object MongoDB.Bson.Document
$update.Add('$push', $inner)
$collection.update($query,$update)
This comes out the be the equivalent of this:
db.collection.update(
{ _id: ObjectId("5333b74c9e99569836bca41c") },
{
"$push": { "3": { "a": "AA" } }
}
)
MongoDB does not create ObjectId values automatically inside array elements in the way you have shown. If you want to create them then you need to add the fields and created ObjectId values yourself.
For any other reference, use the C# documentation which is essentially the API you are using with powershell.
If you use the push operation or such like that,you are supposed to call the save() after that or I don't think your document would be saved.
As a result, your array will always be null.

How do I change my data accessing way from data[key] to data->key in laravel collection?

I got a data structure like below that is created by laravel collection put() function.
Code:
$array= collect();
$array->put($id, "1");
$array->put($name, "test");
Result:
{
"id": "1",
"name": "test"
}
How can I get the value with this syntax $array->id instead of this $array['id']?
Check this answers, https://stackoverflow.com/a/56565951/641611
$arr = ['id' => 3];
$arrToObject = (object) $arr;
echo $arrToObject->id;

Resources