I'm working with a multidimensional object/array and want to retrieve the value of the key labeled confirm_enabled and store it within a variable. Here is the object:
object(stdClass)#361 (2) { ["id"]=> string(1) "1" ["meta"]=> string(475) "{"feed_name":"Default Feed","auto_respond":"0","push_Salesforce":"0","lookup_enabled":"1","confirm_enabled":"1","voterdata_mapped_fields*":"9","voterdata_mapped_fields*":"10","voterdata_mapped_fields_*":"11","voterdata_mapped_fields_*":"","voterdata_mapped_fields_*":"","voterdata_mapped_fields_*":"13","voterdata_mapped_fields_*":"6"}" }
Does anyone know how I can do this?
Try this:
$retrieve = (theVariable)->meta->confirm_enabled;
print_r($retrieve);
Or if you want to make all that an array use this:
$a = json_encode((the_fetch_data)) // insert where you get the data
$b = json_decode($a) // then decode to make this all an array
print_r($b['meta']['confirm_enabled']); //show data
I've figured it out! I'd forgotten to json_decode() before attempting to parse. I queried my database to retrieve the object, which is stored in $meta. From there, I just did the following:
$meta = json_decode( $meta[0]->meta );
$confirm_enabled = intval( $meta->confirm_enabled );
I hope this helps someone!
Related
i have a json file, that has to be parsed in a loop.
i cant seem to succeed
JSON:
{"IMD":{"url":"http:\/\/www.google.com","timeOut":1515155361},"cvH":{"url":"http:\/\/www.google.com","timeOut":1515155364}}
PHP:
<?php
$linkyValues="./linky.json";
if (file_exists($linkyValues)) {
$fileStream = fopen($linkyValues, 'r');
$fileValue=json_decode(fread($fileStream, filesize($linkyValues)));
fclose($fileStream);
echo count($fileValue);//Always 1!
for($i=0;$i<count($fileValue);$i++){
$timeout=$fileValue->item($i)->timeOut;
if(time()>=$timeout){
unset($fileValue[$i]);
}
}
$fileStream = fopen($linkyValues, 'w');
fwrite($fileStream, json_encode($fileValue));
fclose($fileStream);
}
?>
my problem is that count($fileValue) is always 1.
Output of var_dump($fileValue):
object(stdClass)#2 (2) {
["IMD"]=>
object(stdClass)#1 (2) {
["url"]=>
string(21) "http://www.google.com"
["timeOut"]=>
int(1515155361)
}
["cvH"]=>
object(stdClass)#3 (2) {
["url"]=>
string(21) "http://www.google.com"
["timeOut"]=>
int(1515155364)
}
}
it looks like an array to me...
JSON does not support the concept of an associative array, but stores such maps as objects instead.
Your JSON file contains such an object. For PHP this means, that it can either import it as an stdClass ( object ) or as an associatiave array.
This is decided by the json_decode's second parameter, that is either TRUE to read the object as an associative array, or FALSE to read it as an object.
Therefore this will fix your problem:
$fileValue = json_decode(fread($fileStream, filesize($linkyValues)), TRUE);
json_decode documentation
In addition to that, your code has problems with iterating the array. You use $fileValue->item($i) as well as $fileValue[$i], while you have an associative array.
You work with it as if it was an indexed array, while it is an associative array, which means it has keys instead of indices, that identify the values in your array.
The propper way to iterate an associative array is with foreach, like deomstrated belo:
foreach($fileValue as $key => $value) {
if (time() >= $value['timeOut']) {
unset($fileValue[$key]);
}
}
Yet, since you only want to remove specific values, you can use array_filter as well:
$fileValue = array_filter($fileValue, function($value){
return time() < $value['timeOut'];
});
array_filter will then take care of removing the specified fields from your array, so you do not have to unset them manually.
You can use :
count((array)$fileValue);
You're mixing up arrays and objects here.
count() can only be used on arrays, however you can cast an object to array to achieve the same thing.
$fileValue[$i] is a method to access an array, which won't work with your json object.
I see a solution already is to just change your object to an array so I'd like to offer the solution if you wanted to stick with objects.
$linkyValues="./linky.json";
if (file_exists($linkyValues)) {
$fileStream = fopen($linkyValues, 'r');
$fileValue=json_decode($jsonString);
fclose($fileStream);
//Cast the object to an array to get the count, but count isn't really requierd
echo count((array)$fileValue);
//loop through the object
foreach($fileValue as $key=>$fv){
//pull the timeout
$timeout=$fv->timeOut;
//do the check
if(time()>=$timeout){
//remove the timeout from the object
unset($fileValue->$key);
}
}
$fileStream = fopen($linkyValues, 'w');
fwrite($fileStream, json_encode($fileValue));
fclose($fileStream);
}
?>
i have an array after convert it from xml data and i have var_dump the data in array like :
object(SimpleXMLElement)#651 (1) {
["authenticationSuccess"]=>
object(SimpleXMLElement)#652 (1) {
["user"]=>
string(9) "yassine"
}
}
i want to get the value the attribut user that equal "yassine" in this cas .
i trying
$xml["authenticationSuccess"]["user"]
but not working , it return null value , is there any solution to get this data from the array .
some help please
It seems your variable is not array but object, so you need to use $xml->authenticationSuccess->user;
As the var_dump says, you have an object instead of an associative array. You can access object fields like this:
$xml->authenticationSuccess->user;
or this:
$xml->{"authenticationSuccess"}->{"user"};
I am getting the contents of a facebook api call that will return all the posts made to a facebook group. So all the posts/comments on that post.
$data = json_decode(file_get_contents($url2));
foreach($data as $post){
foreach $post as $subpost)
{
...etc
}
}
The problem I have here is that I will need to use 3 nested foreach loops in order to actually get the data I want. How do I get the first element of $data without having to use a foreach?
e.g. something like $data[0] (which doesn't work). How does a foreach loop iterate through an object so I can just manually write it since I only want 1 single object thats nested inside arrays.
edit
object(stdClass)#1 (2) { ["data"]=> array(25) { [0]=> object(stdClass)#2 (10)...
i want to access the final object that contains 10 pieces of data.
$data = json_decode(file_get_contents($url2), true);
will return an array and you can access it via an index like $data[0].
Try to use like below - just pass true in second argument , it will convert object into array so you can use it as array.
$data = json_decode(file_get_contents($url2), true);
foreach($data as $post){
foreach $post as $subpost)
{
...etc
}
}
You should do
$data = json_decode(file_get_contents($url2),true);
To convert it to a array then try
$data[0]
You can use $post instead of $data..
indexing of $post depends on what kind of data you are receiving through json.
If it is a 2-dimensional array,You can use $post[0]['index'] or if it is a single-dimensional array,you can use $post['index'], which will give you the exact indexed item.
In my php file im using the following,
$obj = ($_POST['data']);
var_dump(json_decode($obj,true));
And i see this result. Is this the correct format? and how do i access the array.
eg, set a new variable $newID the same as row1 id
array(4) {
["row0"]=>
string(92) "{"id":"157","name":"123","basic":"123123123","submitter":"Keith","status":"review"}"
["row1"]=>
string(169) "{"id":"158","name":"TEST RESOURCE","basic":"Please state the type of work.","submitter":"Keith","status":"review"}"
["row2"]=>
string(107) "{"id":"159","name":"TEST OTHER","basic":"testing for other","submitter":"Keith","status":"review"}"
["row3"]=>
string(160) "{"id":"160","name":"Name","basic":"type of work","submitter":"Keith","status":"review"}"
}
heres whats in POST in firebug
data {"row0":"{\"id\":\"157\",\"name\":\"123\",\"basic\":\"123123123\",\"submitter\":\"Keith\",\"status\":\"review\"}","row1":"{\"id\":\"158\",\"name\":\"TEST RESOURCE\",\"basic\":\"Please state the type of work.\",\"submitter\":\"Keith\",\"status\":\"review\"}","row2":"{\"id\":\"159\",\"name\":\"TEST OTHER\",\"basic\":\"testing for other\",\"submitter\":\"Keith\",\"status\":\"review\"}","row3":"{\"id\":\"160\",\"name\":\"Name\",\"basic\":\"type of work\",\"submitter\":\"Keith\",\"status\":\"review\"}"}
Each "row" of the array is another JSON string. It seems like the data was double-encoded, like:
$array = json_encode(
array(
'row0' => json_encode(array('id' => '157', ...)),
...
)
)
This is incorrectly encoded data, unless you wanted JSON objects inside JSON objects. To work with it, you need to json_decode each individual item again. Better though: fix the encoding step.
So I thought this would be easy, but try as I might various methods of appending values to an array in PHP, I always get NULL.
$sites = array();
$sites[0] = $_POST['site0'];
foreach($sites as $site) {
var_dump($site);
}
$_POST['site0'] is an HTML form array, containing 11 keys and values. I get a invalid argument error for line 3. Any reason why this would occur?
Probably, $_POST['site0'] is null or empty.
You could push the value to the $sites array doing so:
$sites[] = $_POST['site0'];
The value will be pushed to the end of the array.
It's really work. Just check your
$_POST['site0'];
and try add
$sites[1] = 'spam';
Your code works for me (see codepad):
Code
$_POST['site0'] = 'foobar';
$sites = array();
$sites[0] = $_POST['site0'];
var_dump($sites); // returns NULL
Result
array(1) {
[0]=>
string(6) "foobar"
}
Try to var_dump($_POST); to see what's actually contained in $_POST.