Search in multidimensional Stdclass PHP - 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');

Related

array_unique outputs null while sorting array

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.

how to show multidimentional array result using foreach loop in php

Array
(
[result] => Array
(
[0] => stdClass Object
(
[uniq_id] => 00fdc23c151ad0044d60
)
[1] => stdClass Object
(
[uniq_id] => 590dde424e
)
[2] => stdClass Object
(
[uniq_id] => 6f0eb3bb34
)
[3] => stdClass Object
(
[uniq_id] => eeb6c63929
)
[4] => stdClass Object
(
[uniq_id] => a72034387e
)
}
}
But I want only multiple uniq_id one by one in foreach loop
like
00fdc23c151ad0044d60
590dde424e
6f0eb3bb34
eeb6c63929
a72034387e
Its a stdClass Object array, so try this:
foreach($yourArray['result'] as $data)
{
echo $date->uniq_id.'<br>';
}
Note: stdClass Object properties can be accessed using ->

Adding JSON stored in database to JSON string

I have some JSON stored in a database. Please assume I have a good reason for doing so. I then retrieved it from the DB and wish to include it in another JSON string. The following treats the JSON from the DB as standard strings instead of JSON as I desire. Will I need to loop over $json_from_db and first convert the cmd values to arrays/objects, or is there a better way?
<?php
$json_from_db=array(
array('id'=>1,'cmd'=>'{"a":{}}'),
array('id'=>3,'cmd'=>'{"b":{"name":"x","value":"xxx"}}'),
array('id'=>5,'cmd'=>'{"b":{"name":"y","value":"yyy"}}'),
array('id'=>9,'cmd'=>'{"c":{"name":"z","extra":"hello","more"=>1}}'),
);
$arr=array('a'=>"hello",'json'=>array('a'=>"hello"),'json_from_db'=>$json_from_db);
$json=json_encode($arr);
echo($json."\n\n");
print_r(json_decode($json));
OUTPUT
{"a":"hello","json":{"a":"hello"},"json_from_db":[{"id":1,"cmd":"\"a\":{}"},{"id":3,"cmd":"\"b\":{\"name\":\"x\",\"value\":\"xxx\"}"},{"id":5,"cmd":"\"b\":{\"name\":\"y\",\"value\":\"yyy\"}"},{"id":9,"cmd":"\"c\":{\"name\":\"z\",\"extra\":\"hello\",\"more\"=>1}"}]}
stdClass Object
(
[a] => hello
[json] => stdClass Object
(
[a] => hello
)
[json_from_db] => Array
(
[0] => stdClass Object
(
[id] => 1
[cmd] => "a":{}
)
[1] => stdClass Object
(
[id] => 3
[cmd] => "b":{"name":"x","value":"xxx"}
)
[2] => stdClass Object
(
[id] => 5
[cmd] => "b":{"name":"y","value":"yyy"}
)
[3] => stdClass Object
(
[id] => 9
[cmd] => "c":{"name":"z","extra":"hello","more"=>1}
)
)
)
Note that "b":{"name":"x","value":"xxx"} isn't valid json.
You need to decode all cmd's json-string from $json_from_db,
array_walk_recursive($json_from_db, function (&$item, $key) {
if ($key === 'cmd') {
$item = json_decode($item, true);
}
});
Then you can do
$arr = array(
'a' => "hello",
'json' => array('a'=>"hello"),
'json_from_db' => $json_from_db
);
$json = json_encode($arr);
Demo.

PHP | how to check if some data is present in array

Im using codeigniter, Yes i have searched internet and i have in_array() but it seems that it is not working.
See here is the array i am getting from database.
Array ( [0] => stdClass Object ( [FormCIPath] => admin/dashboard/System ) [1] => stdClass Object ( [FormCIPath] => admin/dashboard/Users ) [2] => stdClass Object ( [FormCIPath] => admin/residentials/# ) [3] => stdClass Object ( [FormCIPath] => admin/configurations/# ) [4] => stdClass Object ( [FormCIPath] => admin/configurations/ManageForms ) [5] => stdClass Object ( [FormCIPath] => admin/residentials/Houses ) [6] => stdClass Object ( [FormCIPath] => admin/residentials/Flats ) [7] => stdClass Object ( [FormCIPath] => admin/configurations/ManageTabs ) [8] => stdClass Object ( [FormCIPath] => admin/configurations/SitePreferences ) [9] => stdClass Object ( [FormCIPath] => admin/usersManageUsers/# ) [10] => stdClass Object ( [FormCIPath] => admin/usersManageUsers/CreateUser ) [11] => stdClass Object ( [FormCIPath] => admin/usersManageUsers/ListUsers ) )
i want to find if configurations/ManageForms is present inside the array, so i tried like this.
$partialURI = $class."/".$method;
if(in_array($partialURI,$result)){
return "True";
}
else{
return "FALSE";
}
but i always get False in return. i haved checked the variable $partialURI it is returning configurations/ManageForms.
But still i am getting FALSE in return and where as you can see this text is present inside array above??
Since in_array() works only on flat, you could just use a simple foreach loop:
$partialURI = $class."/".$method;
foreach($result as $r) {
if(stripos($r->FormCIPath, $partialURI) !== false) {
return 'true';
}
}
return 'false';
Try this:
$data= new array ();
foreach ($result as $r) {
$data[]=$r;
}
Now try your code by replacing $result array with $data
$partialURI = $class."/".$method;
if(in_array($partialURI,$data)){
return "True";
}
else{
return "FALSE";
}

PHP Array remove items if missing key

I have the following array format:
Array
(
[0] => stdClass Object
(
[tid] => 3
[children] => Array ()
)
[1] => stdClass Object
(
[tid] => 15
)
[2] => stdClass Object
(
[tid] => 12
[children] => Array ()
)
[3] => stdClass Object
(
[tid] => 9
[children] => Array ()
)
)
I would like to remove items that do not have [children] and am having some difficulty doing so.
Help is appreciated, thank you.
Try this, where $array is the array you want to process
foreach($array as $key => $value)
{
if(!isset($value['children'])
unset($array[$key]);
}
This will remove all items in your array where children is not set or is null. Since in your example you set children to empty arrays, you will need to use isset() and not empty().
Can you give this a shot and let me know what happens?
$myArray = array_filter($myArray, "hasChildren");
function hasChildren($node) {
if (isset($node->children)) return true;
return false;
}

Categories