Get Json parameter - json

I need the message part of the json-string below. It should be possible with this, but it isn't.
echo $fb_response['data'][0]['message'];
[VGardena] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[message] => The traditional cuisine of Val Gardena - so much variety, so many different local specialities!nCome and visit us this summer at the Dolomites Market in Selva: goo.gl/6kMvsE
[story] => Val Gardena Gru00f6den added 3 new photos u2014 eating Strudel in Selva Wolkenstein.
[created_time] => 2017-04-16T17:11:00+0000
[id] => 105061549531699_1321349021236273
)
[1] => stdClass Object
(
[message] => ud83cudf37ud83cudf37ud83cudf37 Mbincion a duc na Bona Pasca! ud83cudf37ud83cudf37ud83cudf37nnu25ba Serena #Pasqua a tutti!nu25ba Wir wu00fcnschen euch allen Frohe #Ostern!nu25ba Wishing you a Happy Easter!
[story] => Val Gardena Gru00f6den celebrating Easter with Marina Demetz at Val Gardena Gru00f6den.
[created_time] => 2017-04-16T08:00:00+0000
[id] => 105061549531699_1328683843836124
)
)
[paging] => stdClass Object
(
[previous] => https://graph.facebook.com/v2.8/105061549531699/posts?limit=2&since=1492362660&access_token=EAACx6A6Hb9cBAB8tomDGj9N1FpE2xcWMVE7ad0Y2q08XUjVmGXsQ2aPCEKbX0iiRxK6M6FWYl1os3I2k5ZC1uY2cnJ1bZB2He3KTOpbwNo9KAZAzweShCUFvOt9Nfb2jZB8gHZCiO8l66dxsqdMCAbgxIlkTqJ8EZD&__paging_token=enc_AdCelydXbqb60yLCpaRWbTgLMBNNGGsZAfGqMO82Go1ZCA3ZAkSH6IS2TOBzqBSZA6SXXmltAcOE6ugchoDN3xGK1egG7qp8QtzqYD5u6YLeh9jBZAgZDZD&__previous=1
[next] => https://graph.facebook.com/v2.8/105061549531699/posts?limit=2&access_token=EAACx6A6Hb9cBAB8tomDGj9N1FpE2xcWMVE7ad0Y2q08XUjVmGXsQ2aPCEKbX0iiRxK6M6FWYl1os3I2k5ZC1uY2cnJ1bZB2He3KTOpbwNo9KAZAzweShCUFvOt9Nfb2jZB8gHZCiO8l66dxsqdMCAbgxIlkTqJ8EZD&until=1492329600&__paging_token=enc_AdBOpmW5OIQydpreytQ0eoWAdD9grbpMpB3XrkTTJL493qrDlacQCGVrn23ZBdZCKqgLxijwuQEnq5E1mwFaeiG2jRqdBvJmN2c7qu5UJQvIDnZBwZDZD
)
)

It should be possible with this, but it isn't.
No it shouldn’t, because you have an object here inside data, and not an array. (And data itself is also the property of an object.)
Object properties are accessed via -> in PHP.
$foo['VGardena']->data[0]->message

Related

Undefined index with Codeigniter CSV Import

I'm trying to import a CSV File with Codeigniter, which contains the code below:
Voornaam;Achternaam;email
John;Doe;john.doe#gmail.com
Man;Made;man#made.com
The file is successfully uploaded, and when I print the code through PHP it indeed shows me the file contents in an array:
Array ( [0] => Array ( [Voornaam] => John [Achternaam] => Doe [email] => john.doe#gmail.com ) [1] => Array ( [Voornaam] => Man [Achternaam] => Made [email] => man#made.com ) )
The code is then processed through the following code:
foreach($csv_array as $row) {
$insertData = array(
'firstName' => $row['Voornaam'],
'lastName' => $row['Achternaam'],
'email' => $row['email']
);
};
Which unfortunately 'Undefined index: Voornaam' When i replace $row['Voornaam'] with '', the code processes fine, so lastName and email work. Now, I cannot for the life of me figure out why Voornaam is not processed, and how it may be fixed.

foreach on json data

Hi i want to insert values into table which is posted from api
json data is
{"questions":{"34":"Yes", "46":"good", "48":"NA", "29":"Yes", "45":"ravi", "49":"Negative", "43":"1 BHK", "35":"Neighbour", "38":"14", "39":"9", "27":"1",
"41":"Married", "52":"vijay#123.com", "47":"good", "31":"Bunglow", "33":"Middle Class", "37":"Owned By Parents", "30":"good", "50":"easy",
"51":"comments", "32":"No", "44":"[MusicSystem,PC,Refrigerator,Airconditioner]"}}
when i convert into array it is
Array
(
[questions] => Array
(
[34] => Yes
[46] => good
[48] => NA
[29] => Yes
[45] => ravi
[49] => Negative
[43] => 1 BHK
[35] => Neighbour
[38] => 14
[39] => 9
[27] => 1
[41] => Married
[52] => ravi#gmail.com
[47] => good
[31] => Bunglow
[33] => Middle Class
[37] => Owned By Parents
[30] => good
[50] => easy
[51] => comments
[32] => No
[44] => [MusicSystem,PC,Refrigerator,Airconditioner]
)
)
how to get this values like
using foreach
$question=34;
$answer= yes;
Try like this.Convert json into array using json_decode().And make a new array with key and value.Then just print the first key and value.
<?php
$json = '{"questions":{"34":"Yes", "46":"good", "48":"NA", "29":"Yes", "45":"ravi", "49":"Negative", "43":"1 BHK", "35":"Neighbour", "38":"14", "39":"9", "27":"1",
"41":"Married", "52":"vijay#123.com", "47":"good", "31":"Bunglow", "33":"Middle Class", "37":"Owned By Parents", "30":"good", "50":"easy",
"51":"comments", "32":"No", "44":"[MusicSystem,PC,Refrigerator,Airconditioner]"}}
';
$array = json_decode($json,TRUE);
//print_r($array);
foreach($array['questions'] as $key=>$value)
{
$arr[] = array('key'=>$key,'value'=>$value);
}
//print_r($arr);
echo "Question:".$arr[0]['key'].PHP_EOL;
echo "Answer:".$arr[0]['value'];
Output:
Question:34
Answer:Yes
To access a single value while having the key, you can do $array["questions"][34] to get the answer yes.
If you want to loop through all of them, you can do this loop:
foreach($array["questions"] as $no => $ans){
echo "Question: $no";
echo "Answer: $ans";
}

Magento SOAP API V2 calls not working for Private Sales Store (Authorization plugin used)

FACTS:
I am not very good at magento but have some experience working with API's using PHP.
TARGET:
What I actually want to do is
Download magento customer orders in our order system
Process the orders
Upload tracking code back to magento
Update magento products level based on the values fetched from our local Stock System running on a different server
ENVIRONMENT:
I am using a Login Plugin to make the store private so only customers with an account can access it.
PROBLEM:
IF 'AUTHENTICATION' PLUGIN IS DISABLED EVERYTHING WORKS FINE. BUT WHEN PLUGIN IS ENABLED STEP 3 AND 4 STOP WORKING.
ERROR SAMPLE:
Magento SOAP API V2 calls result in
SoapFault Object ( [message:protected] => looks like we got no XML document, when used for Private Sales store. I am able to login using API a/c,fetch orders but calls to update stock / upload tracking etc fail.
SAMPLE OUTPUT:
Login Successful [0a254c064fa033859bc75db94]
Request :
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento"><SOAP-ENV:Body><ns1:catalogInventoryStockItemUpdateRequestParam>sessionId>0a254c064fa033859bc75db94</sessionId><productId>1</productId><data><qty>100</qty><is_in_stock>1</is_in_stock></data></ns1:catalogInventoryStockItemUpdateRequestParam></SOAP-ENV:Body></SOAP-ENV:Envelope>
Result:
SoapFault Object ( [message:protected] => looks like we got no XML document [string:Exception:private] => [code:protected] => 0 [file:protected] => /var/www/vhosts/XXX/magentoAPI/stock.php [line:protected] => 31 [trace:Exception:private] => Array ( [0] => Array ( [file] => /var/www/vhosts/XXX/magentoAPI/stock.php [line] => 31 [function] => __call [class] => SoapClient [type] => -> [args] => Array ( [0] => catalogInventoryStockItemUpdate [1] => Array ( [0] => stdClass Object ( [sessionId] => 0a254c064fa033859bc75db94 [productId] => 1587 [data] => Array ( [qty] => 1000 [is_in_stock] => 1 ) ) ) ) ) [1] => Array ( [file] => /var/www/vhosts/XXX/magentoAPI/stock.php [line] => 31 [function] => catalogInventoryStockItemUpdate [class] => SoapClient [type] => -> [args] => Array ( [0] => stdClass Object ( [sessionId] => 0a254c064fa033859bc75db94 [productId] => 1587 [data] => Array ( [qty] => 1000 [is_in_stock] => 1 ) ) ) ) ) [previous:Exception:private] => [faultstring] => looks like we got no XML document [faultcode] => Client [faultcodens] => http://schemas.xmlsoap.org/soap/envelope/ )

PHP don't generate properly array from database (UBUNTU)

I have a little problem.
There are two tables in my database: users and classes. The first one contains info about users, and the second one - about user classes and about access rights.
Now I'm extracting all data from there using this:
SELECT * FROM users NATURAL JOIN classes WHERE users.ID_User = '$id';
The mysql code works and returns a right array.
Now, when I'm doing next:
<?php
$result = mysql_query($sql_above);
$row = mysql_fetch_array($result);
print_r($row);
?>
... i'm getting this:
Array ( [0] => 1 [ID_User] => 1 [1] => John [Name] => John [2] => Doe [Surname] => Doe [3] => ... [16] => Owner [Class] => Owner [17] => [All] => [18] => [CanAuth] => [19] => [CanViewData] => [20] => [CanAddData] => [21] => [CanAlterData] => [22] => [CanDeactivateData] => [23] => [CanDeleteData] => [24] => [CanMgLocatari] => ... )
Columns from 17 till the end are access rights, noted in the database by 1 or 0. And there, they're missing.
Is there any option to enable in PHP configuration to correct this thing? Because on a Windows machine, the code executes properly and rights are working.
PHP Version 5.3.6-13ubuntu3.7 | Apache/2.2.20 (Ubuntu) | MySQL client version: 5.1.62
Please, reply. Thanks!

Joins - Merge result into single, multidimensional array

I'm performing a simple query that joins two tables together. What I get is something like this.
array(
[0] => array(
'id' => 52
'name' => 'charles',
'sale_id' => 921,
'sale_time' => 1306393996,
'sale_price' => 54.21
),
[1] => array(
'id' => 52
'name' => 'charles',
'sale_id' => 922,
'sale_time' => 1306395000,
'sale_price' => 32.41
),
...
);
...which is the expected result. However, I'd like the query to return something like this:
array(
[0] => array(
'id' => 52,
'name' => 'charles',
'sales' => array(
[0] => array(
'sale_id' => 921,
'sale_time' => 1306393996,
'sale_price' => 54.21
),
[1] => array(
'sale_id' => 922,
'sale_time' => 1306395000,
'sale_price' => 32.41
),
...
)
)
)
Now I realize I could simply perform two queries, one for the user info, and another for sales, and merge those arrays together using whatever language I'm using (PHP in this case). But I have many arrays of properties and querying and merging for those seems awfully inelegant to me (although it does work). It seems to me there'd be a way to work with a single, unified object without duplicating data.
Just wondering if there was a no-brainer query, or if that's simply not easy through MySQL alone.
I would say this is not possible with MySQL alone - you have to do some tricks at application level. That is, because even if you send a single query that will bring you all the data from MySQL to your application (PHP), they will come as a denormalized array of data - your first case.
If you want to get the data as in your second case, I'd recommend using some ORM - in Ruby there is ActiveRecord, in Perl there are Class::DBi, DBIx::Class and many more - I can not name one for PHP that is able to do this, but I am sure there are plenty.