array_unique outputs null while sorting array - php

I have this array (decoded from JSON, output with print_r):
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[item] => te
[date] => 13.10
)
[1] => stdClass Object
(
[item] => te
[date] => 13.10
)
[2] => stdClass Object
(
[item] => tr
[date] => 13.10
)
)
)
But now I have to remove all the duplicates.
If I try $result = array_unique($array, SORT_REGULAR);
$result is null.
Can someone spot my mistake?

This is a stdClass object, not an array. When you decode by using the function json_decode, you need to pass the parameter "true" to have an array:
$array = json_decode($json, true);
Edit: As people noticed in the comments, the actual array exists in $array['data'], so the array_unique must be applied on $array['data'] instead of $array.

Related

array_diff converst array to stdClass Object

$array
Array
(
[0] => one
[1] => two
[2] => three
)
$array2
Array
(
[0] => three
)
array_diff($array,$array2)
stdClass Object
(
[0] => one
[1] => two
)
i need final result to be array instead of stdClass Object
Array
(
[0] => one
[1] => two
)
Refer to doc, its already returning an array.
Compares array against one or more other arrays and returns the values in array that are not present in any of the other arrays.
To cast stdClass to array, can do
$casted_result = (array) $result;

Search in multidimensional Stdclass PHP

I want to search in PHP within a specific object. For instance [module] => play in a StdClass object, how can I search for values within the class itself?
Below you can find the whole array:
[flow] => stdClass Object
(
[data] => stdClass Object
(
[action] => start
)
[module] => record_call
[children] => stdClass Object
(
[_] => stdClass Object
(
[data] => stdClass Object
(
[id] => 7696364e90822fdcba4feee30574a7e2
[endless_playback] =>
[terminators] => Array
(
[0] => 1
)
)
[module] => play
[children] => stdClass Object
(
[_] => stdClass Object
….
I want to get the output like:
[data] => stdClass Object
(
[id] => 7696364e90822fdcba4feee30574a7e2
[endless_playback] =>
[terminators] => Array
(
[0] => 1
)
)
[module] => play
[children] => stdClass Object
(
[_] => stdClass Object
….
Can someone help me with that? Thank you in advance!
You have a couple of simpler options here.
The first is if you're receiving this data via a json_decode() call, add true as a second parameter:
$decoded = json_decode($data, true);
The second is to convert this to a simple associative array structure by taking advantage of the above and the use of json_encode():
$decoded = json_decode(json_encode($data), true);
With your simple array structure, it's now possible to recursively traverse the array to find the matching key/value pair desired:
function findModule($data, $module_name) {
if(!is_array($data)) return null;
if(array_key_exists('module', $data)) {
if($data['module'] === $module_name) {
return $data;
}
}
foreach($data as $key=>$val) {
$result = findModule($val, $module_name);
if(!is_null($result)) {
return $result;
}
}
return null;
}
// Will either return the array of array('data'=>..., 'module'=>'play', 'children': ...) or null if no matching module found.
$module = findModule($decoded, 'play');

Parse JSON from file using PHP from objects/arrays

I get the following output using print_r in php of my decoded JSON:
stdClass Object
(
[assignments] => Array
(
[0] => stdClass Object
(
[assignmentid] => 1
[grades] => Array
(
[0] => stdClass Object
(
[id] => 1
[userid] => 3
[attemptnumber] => 0
[timecreated] => 1484244192
[timemodified] => 1484244203
[grader] => 2
[grade] => 85.00000
)
)
)
)
[warnings] => Array
(
)
)
I want to get the value for [grade] => 85.00000 and store it in a variable. How would I go about doing this?
What about:
$var = $obj->assignments[0]->grades[0]->grade;
Use true as second parameter of json_decode() to decode it to array and then you have to loop over your result.
$data = json_decode($json, true);
foreach ($data['assignments'] as $row) {
foreach ($row['grades'] as $grade) {
echo $grade['grade'];
}
}

An array within an array of object

I have this object returned:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Desc] => Amount should be numeric.
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Desc] => Please enter your Reference Number.
)
)
)
How can I get the desc values? I need to get both Desc Values('Amount should be numeric.' and 'Please enter your Reference Number. ')
I have tried:
$res = $str[0];
it returned:
SimpleXMLElement Object
(
[#attributes] => Array
(
[Desc] => Amount should be numeric.
)
)
Your object is a SimpleXML object.
You can learn how to use it here:
http://php.net/manual/fr/book.simplexml.php
To solve your issue, you can use this:
$res0 = $str[0]->attributes()->Desc;
$res1 = $str[1]->attributes()->Desc;
Call attributes() and then access them as properties.
$node->attributes()->Desc

Getting an objects property with slashes in it

I'm getting a json result back from an api request to the freebase database.
This is part of the object returned called $json. A var dump of $json:
stdClass Object
(
[name] => Abomey
[/location/statistical_region/population_growth_rate] =>
[/common/topic/article] => Array
(
[0] => stdClass Object
(
[id] => /m/0jk2c
)
)
How can I subtract the /m/0jk2c part?
$json->/common/topic/article[0]->id (obviously) doesn't work.
This should do it:
$json->{"/common/topic/article"}[0]->id
This is what you should use
$class->{'/location/statistical_region/population_growth_rate'}['/common/topic/article'][0]->id
Reason your Object looks like this
$std = new stdClass();
$std->id = '/m/0jk2c' ;
$json = new stdClass();
$json->name = "Abomey" ;
$json->{'/location/statistical_region/population_growth_rate'} = array('/common/topic/article'=>array($std));
If you run
var_dump($json->{'/location/statistical_region/population_growth_rate'}['/common/topic/article'][0]->id);
Output
string '/m/0jk2c' (length=8)
Run
echo "<pre>";
print_r($json);
Output
stdClass Object
(
[name] => Abomey
[/location/statistical_region/population_growth_rate] => Array
(
[/common/topic/article] => Array
(
[0] => stdClass Object
(
[id] => /m/0jk2c
)
)
)
)

Categories