My document where data is to be inserted is:
Data has to be appended to array with key as Solutions
Suppose $data has some numerical value
array(8) {
["_id"]=>
object(MongoId)#9 (1) {
["$id"]=>
string(24) "56d32613097ed36c10000049"
}
["Username"]=>
string(4) "xxx"
["Solutions"]=>
array(0) {
}
}
Suppose $j= "The entire above array"
How to push the value of $data in the array having key as "Solutions"?
Related
I have a dump of data :
var_dump($steps);
and results are :
object(Drupal\form\Manager\StepManager)#490 (1) {
["steps":protected]=>
array(1) {
[1]=>
object(Drupal\form\Step\StepOne)#494 (2) {
["step":protected]=>
int(4)
["values":protected]=>
array(1) {
["key"]=>
string(3) "123"
}
}
but I tried using :
$steps[1]->values->key but its having an error, its not available directly?
where did I miss?
The index to the first item – and only item in your case – is 0, and not 1. So this should work:
$steps[0]->...
The number 1 in the var_dump shows the size of the array:
["steps":protected]=>
array(1) { // This array contains one item
Your $steps array with in a object of Drupal\form\Manager\StepManager
and you accessed key from values object but your output shows you have a values array not object.
array(1) {
["key"]=>
string(3) "123"
}
So try this in var_dump() I think its worked
var_dump($steps->steps[1]->values["key"]);
I am trying to create a JSON representation of data stored in mySQL.
I am trying to document a RESTful API.
I am using PHP's json_encode() function.
I have a table that contains data such as
1) name
2) parent
3) data_type (object/array/string/number to match JSON data types)
4) value
I am trying to create a generalized function that will allow me to build these JSON strings by simply adding data to the mySQL database.
I am having problems with working with both objects and arrays though.
For instance the JSON should be:
{
"sessionToken":"","roleName":""
,"data":[
{
"methodTypes":[""] , "objects":[""]
}
]
}
however it is coming out as:
{
"sessionToken":"","roleName":""
,"data":[
{
"methodTypes":[""]
}
,{
"objects":[""]
}
]
}
this is indicating to me for some reason my code is adding an object for both methodType and objects, where as it should just be within the single object.
I am trying to first create an array containing methodTypes and objects.
Then I create an object in the format of $objects->$A, and I make this equal the array created in the first step.
Then I add in to the primary data array for the JSON generation.
I have been searching for examples that show usage examples of JSON when both arrays and objects are required in the same JSON without success.
Any pointers in the correct direction would be greatly appreciated.
UPDATE #1:
var_dump of the array that is being fed to json_encode() is:
array(3) { ["sessionToken"]=> string(0) "" ["roleName"]=> string(0) "" ["data"]=> array(2) { [0]=> array(1) { ["methodTypes"]=> array(1) { [0]=> string(0) "" } } [1]=> array(1) { ["objects"]=> array(1) { [0]=> string(0) "" } } } }
where as if I take a known good JSON and do a json_decode() then the var_dump looks like:
object(stdClass)#3 (3) { ["sessionToken"]=> string(0) "" ["roleName"]=> string(0) "" ["data"]=> array(1) { [0]=> object(stdClass)#4 (2) { ["methodTypes"]=> array(1) { [0]=> string(0) "" } ["objects"]=> array(1) { [0]=> string(0) "" } } } }
or
array(3) { ["sessionToken"]=> string(0) "" ["roleName"]=> string(0) "" ["data"]=> array(1) { [0]=> array(2) { ["methodTypes"]=> array(1) { [0]=> string(0) "" } ["objects"]=> array(1) { [0]=> string(0) "" } } } }
if I set it to TRUE to return array instead of object.
Edited to give the desired output
$data = ['sessionToken' => '',
'roleName' => '',
'data' => [['methodTypes' => [''], 'objects' => ['']]]
];
And it yields. This works because I wrapped the associative array in a non-associative one
{"sessionToken":"","roleName":"","data":[{"methodTypes":[""],"objects":[""]}]}
EDIT
Some more info on json_encode
Javascript arrays contain only numeric keys. Period. If you have what looks like an associative array, it's really a JS object (which can be referenced using brackets, i.e. var['name'] and var.name are equivalent).
Since json_encode can't create an associative array in JSON, it converts associative PHP arrays into objects instead. So you can see in my example above, I got an object back instead of an array, except where I has not specified keys.
echo json_encode(['name' => 'value']);
Yields
{"name":"value"}
While
echo json_encode(['value']);
Yields
["value"]
So I need to modify the array in a memcached key-value pair. I need to remove one of the arrays inside the array. An example of what it looks like:
array(2) { [0]=> array(3) { ["username"]=> string(3) "Bob" ["id"]=> string(5) "14537" ["comment"]=> string(4) "cool"} [1]=> array(3) { ["username"]=> string(3) "Tom" ["id"]=> string(5) "14538" ["comment"]=> string(3) "yes"}}
If I know the values of username, id, and comment, how can I delete it? The generic queston: How can I delete array 0?
Considering the answer of doing a foreach loop, I tried
foreach($memcachedarray as $f){
if ($f['id'] == '14537'){
echo key($f);
}
}
But it spits out username
Edit- Ok
I searched some more and found I need to do this:
foreach($memcachedarray as $key => $f){
if ($f['id'] == '14537'){
echo $key;
}
}
That works!
If the Id's are unique across the system then you could use an associative array to store you data then unset the key, otherwise you would want to use a foreach loop to get the array key, then unset that key and recommit your new array back into memcache.
My question: How can I break up and iterate through the JSON array pictured below?
I am making an AJAX web app and I need to serialize an array of objects in Javascript and put them in a url to pass to a php script. This is all going fine and the php script recieves the JSON like so..
$passed = $_GET['result'];
if(isset($passed)){
$passed = str_replace("undefined" , " " , $passed); /*had to add this to remove the undefined value*/
$json = json_decode(stripslashes($passed));
echo"<br/>";
var_dump($json ); //this is working and dumps an array
}
When I call var_dump on the decoded JSON I echo an output like so...
array(1) { [0]=> object(stdClass)#70 (2) { ["itemCount"]=> int(0) ["ItemArray"]=> array(2) { [0]=> object(stdClass)#86 (6) { ["itemPosition"]=> int(0) ["planPosition"]=> int(0) ["Name"]=> string(5) "dsfsd" ["Description"]=> string(3) "sdf" ["Price"]=> string(0) "" ["Unit"]=> string(0) "" } [1]=> object(stdClass)#85 (6) { ["itemPosition"]=> int(1) ["planPosition"]=> int(0) ["Name"]=> string(4) "fdad" ["Description"]=> string(3) "sdf" ["Price"]=> string(0) "" ["Unit"]=> string(0) "" } } } }
The JSON
This is the JSON I am receiving. It seems like some of the pairs don't have names? How can I access elements in this Array?
Thanks alot guys
Some of these elements are coming back as stdClass objects as you can see in the var_dump output. You can get at the attributes with the standard object notation, for example, with your $json variable:
echo $json[0]->itemCount; // 0
echo $json[0]->itemArray[0]->itemPostion; // 0
You can also iterate over stdClass instances just like any PHP object, you'll be looping through the public data members, so again with your $json:
foreach(echo $json[0]->itemArray[0] as $key => $value)
echo 'key: ' . $key . ', value: ' . $value . PHP_EOL;
will loop through that first object, echoing out the member names and values of the object.
You just access them by index:
data[0] // first data item
Note that that's how you would "normally" access an array in the usual sense, so I might be missing something about your question here...
I have the following already decoded json stored in $response = $result->response;:
object(stdClass)#6 (5) {
["EmailAddress"]=> string(18) "email#gmail.com"
["Name"]=> string(0) ""
["Date"]=> string(19) "2011-10-09 19:32:00"
["State"]=> string(6) "Active"
["CustomFields"]=> array(1) {
[0]=>object(stdClass)#7 (2) {
["Key"]=>string(2) "id"
["Value"]=>string(6) "Dl9lIz"
}
}
I can already access the main attributes (EmailAddress, Name, etc) with:
$email = $response->{'EmailAddress'};
print $email;
But I need to access the "Value" portion in the CustomFields object. I don't know how to dig that deep. I'm attempting to do this in PHP..
Any suggestions?
It is contained in the first element ([0]) of array CustomFields, so you can access it with an object operator (->) after the array index.
print $response->CustomFields[0]->Value;