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

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";
}

Related

unset std object whole key based condition check

trying to filter data from std object to get result only with status = Active
here is my data =
$newresults =
array {
[1] => stdClass Object
(
[id] => 30508
[status] => Active
)
[2] => stdClass Object
(
[id] => 30509
[status] => InActive
)
[3] => stdClass Object
(
[id] => 30510
[status] => Active
)
}
in foreach loop i need to get new array of std object with status = active only
so far i am trying to do this with
foreach ($newresults as $key => $value) {
if($value->status == 'Inactive')
unset($newresults[$key]);
}
$newresults[]=$value;
}
return $newresults;
thanks in advance i am sure i can do it this way but i might be doing mistake somewhere
expected output =
array {
[1] => stdClass Object
(
[id] => 30508
[status] => Active
)
[2] => stdClass Object
(
[id] => 30510
[status] => Active
)
}
You could just use array_filter:
$newresults = array_filter($newresults, function ($v) { return $v->status == 'Active'; });
print_r($newresults);
Output:
Array
(
[1] => stdClass Object
(
[id] => 30508
[status] => Active
)
[3] => stdClass Object
(
[id] => 30510
[status] => Active
)
)
Demo on 3v4l.org
If you want the array to be re-indexed starting at 0, just use array_values on the result.
That should work where you just remove those "Inactive" ones.
foreach ($newresults as $key => $value) {
if ($value->status == 'Inactive') {
unset($newresults[$key]);
}
}

Foreach - determine if key exists in each iteration

I have the following Array.
I am trying to figure out how to create an if statement in the foreach that will set true or false based on if any of the individual indexes has [Customer_Facing_Comments__r]
I am using this code - however, this just tells me if [Customer_Facing_Comments__r] is in the array - which it is, every time. I need to see if it is in each index [0], [1]..etc...and set true or false base on that.
CODE:
foreach($queryResult->records as $record){
if (array_key_exists("Customer_Facing_Comments__r",$record)) {
$test = 'true'; }
else { $test = 'false'; } }
QueryResult Object
(
[queryLocator] =>
[done] => 1
[records] => Array
(
[0] => stdClass Object
(
[Id] => 5003xx0255mhcAAA
[Account] => stdClass Object
(
[Id] => 0010cxx026IwsTAAS
[Name] => xxx
)
[AccountId] => 0010c0xxwsTAAS
[Customer_Facing_Comments__r] => stdClass Object
(
[done] => 1
[queryLocator] =>
[records] => Array
(
[0] => stdClass Object
(
[Id] =>
[Comment__c] => test string
[CreatedDate] => 2021-02-13T00:25:50.000Z
[Trouble_Ticket__c] => 5003x0000255mhcAAA
)
)
[size] => 1
)
[Type] => Service Down
)
[1] => stdClass Object
(
[Id] => 5003x000024Y6TpAAK
[Account] => stdClass Object
(
[Id] => 0010c0cccc6IwsTAAS
[Name] => test
)
[AccountId] => 0010c00cccIwsTAAS
)
Just expose the key in the foreach and use that in your result array $test:
foreach($queryResult->records as $key => $record){
if (isset($record->Customer_Facing_Comments__r)) {
$test[$key] = 'true';
} else {
$test[$key] = 'false';
}
}

PHP Sorting Multidimensional Associative Array by Key and Value

I'm trying to sort the following data by the date in the key and the value of Name.
The aim is to a get nice date ordered array with all the Names from the inner array in alphabetical order.
Array
(
[2017-07-27] => Array
(
[0] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Orange
)
)
[4] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Apple
)
)
)
[2017-07-22] => Array
(
[6] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Apple
)
)
[7] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Orange
)
)
)
[2017-07-29] => Array
(
[9] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Orange
)
)
[11] => stdClass Object
(
[Job] => stdClass Object
(
[Name] => Plumb
)
)
)
)
I'm pretty sure I should be using array_multisort but can't quite get the desired results.
You must split the code if you want to order on object properties, use the usort function.
Where $arr is your array:
uksort($arr, 'dateCmp');
foreach($arr as &$sub){
usort($sub, 'propCmp');
}
function dateCmp($a, $b){
return (strtotime($a) < strtotime($b) ? -1 : 1);
}
function propCmp($a, $b){
return ($a->Job->Name < $b->Job->Name ? -1 : 1);
}
Please try below code,
$sorted_vals = array();
ksort($multiArrs);
foreach($multiArrs as $key => $value) { // $multiArrs = your data array
$columns = null;
foreach ($value as $index => $element) {
$columns[] = $element->Job;
}
$temp = $value;
array_multisort($columns, SORT_ASC, $temp);
$sorted_vals[$key] = $temp;
}

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 ->

How to iterate through a nested stdClass? [duplicate]

This question already has answers here:
stdClass object and foreach loops
(5 answers)
Closed 4 months ago.
I have an object like this:
stdClass Object
(
[_count] => 10
[_start] => 0
[_total] => 37
[values] => Array
(
[0] => stdClass Object
(
[_key] => 50180
[group] => stdClass Object
(
[id] => 50180
[name] => CriticalChain
)
)
[1] => stdClass Object
(
[_key] => 2357895
[group] => stdClass Object
(
[id] => 2357895
[name] => Data Modeling
)
)
[2] => stdClass Object
(
[_key] => 1992105
[group] => stdClass Object
(
[id] => 1992105
[name] => SQL Server Users in Israel
)
)
[3] => stdClass Object
(
[_key] => 37988
[group] => stdClass Object
(
[id] => 37988
[name] => CDO/CIO/CTO Leadership Council
)
)
[4] => stdClass Object
(
[_key] => 4024801
[group] => stdClass Object
(
[id] => 4024801
[name] => BiT-HR, BI & IT Placement Agency
)
)
[5] => stdClass Object
(
[_key] => 37845
[group] => stdClass Object
(
[id] => 37845
[name] => Israel Technology Group
)
)
[6] => stdClass Object
(
[_key] => 51464
[group] => stdClass Object
(
[id] => 51464
[name] => Israel DBA's
)
)
[7] => stdClass Object
(
[_key] => 66097
[group] => stdClass Object
(
[id] => 66097
[name] => SQLDBA
)
)
[8] => stdClass Object
(
[_key] => 4462353
[group] => stdClass Object
(
[id] => 4462353
[name] => Israel High-Tech Group
)
)
[9] => stdClass Object
(
[_key] => 4203807
[group] => stdClass Object
(
[id] => 4203807
[name] => Microsoft Team Foundation Server
)
)
)
)
I need to get the id and name in an HTML table, but I seem to have a hard time iterating through this object. TIA. I understand that I need to get to the Values Array, and then to the group object, but I trip over the transitions between object and array and foreach vs index based iteration.
For example I tried this:
foreach ($res as $values) { print "\n"; print_r ($values); }
It iterates trough the object, but it also gives me useless
10 0 37
echo "<table>"
foreach ($object->values as $arr) {
foreach ($arr as $obj) {
$id = $obj->group->id;
$name = $obj->group->name;
$html = "<tr>";
$html .= "<td>Name : $name</td>";
$html .= "<td>Id : $id</td>";
$html .= "</tr>";
}
}
echo "</table>";
Since this is the top result in Google if you search for iterate over stdclass it may be helpful to answer the question in the title:
You can iterate overa stdclass simply by using foreach:
$user = new \stdClass();
$user->flag = 'red';
foreach ($user as $key => $value) {
// $key is `flag`
// $value is `red`
}
function objectToArray( $data )
{
if ( is_object( $data ) )
$d = get_object_vars( $data );
}
Convert the Object to array first like:
$results = objectToArray( $results );
and use
foreach( $results as result ){... ...}
I know it's an old post , but for sake of others:
when working with stdClass you should use Reflections:
$obj = new ReflectionObject($object);
$propeties = $obj->getProperties();
foreach($properties as $property) {
$name = $property->getName(); <-- this is the reflection class
$value = $object->$name; <--- $object is your original $object
here you need to handle the result (store in array etc)
}
foreach($res->values as $value) {
print_r($value);
}

Categories