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]);
}
}
Related
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';
}
}
I want to merge the 2 arrays of objects based on the 'id' field of Array1 and the 'itemVendorCode' of Array2. I also wanted to remove from the resulting arrays of object anything that didn't match.
Array1:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
)
[1] => stdClass Object
(
[id] => 89-575-2354
[qty] => 24
[price] => 230.35
)
[2] => stdClass Object
(
[id] => 89-605-1250
[qty] => 2
[price] => 230.35
)
)
Array2:
Array
(
[0] => Item Object
(
[internalId] => 14062
[itemVendorCode] => 89-605-1250
)
[1] => Item Object
(
[internalId] => 33806
[itemVendorCode] => 89-575-2354
)
[2] => Item Object
(
[internalId] => 64126
[itemVendorCode] => 26-295-1006
)
)
I was able to solve this by this code:
$indexed = array();
foreach($itemsArray as $value) {
$indexed[$value->itemVendorCode] = $value;
}
$results = array();
foreach($vendorItems as $obj) {
$value = $indexed[$obj->id];
if (isset($value)) {
foreach($value as $name => $val) {
$obj->$name = $val;
array_push($results, $obj);
}
}
}
print_r($results);
credits to the original poster. I just modified it a bit,
I was able to get the result like this:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[1] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[2] => stdClass Object
(
[id] => 14-102-1010
[qty] => 16
[price] => 3.2
[internalId] => 57033
[itemVendorCode] => 14-102-1010
)
)
I think you will have to use array_map function which provides you a callback function to execute on array(s).
In the callback function:
- declare your array1
- foreach the second array
- set an if statement to check that the current iteration with the id value matches the itemVendorCode of the array2 and return it
something like this:
// You have to specify to PHP to use a local copy of your $array2 to works with it into your callback
$cb = function ($obj1) use ($array2)
{
// you foreach this array
foreach ($array2 as $obj2) {
// if the value of id matches itemVendorCode
if ($obj1->id === $obj2->itemVendorCode) {
// you return the id
return $obj->id;
}
}
};
// this function will fill a new array with all returned data
$mergedArray = array_map($cb, $array1);
This code is a sample but doesn't provide you, your needled solution, try to update it to do what you exactly want ;)
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";
}
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);
}
In my function im storing in $var1 a query from the database of posts. And im storing in $var2 a query from the database of images from the posts. (Each has a key post_id to connect them.)
$var1 will return something like this.
array (
[0] => stdClass Object
(
[post_id] => 210
[post_title] => title
)
[1] => stdClass Object
(
[post_id] => 212
[post_title] => title
)
)
and $var2 will return something like this.
array (
[0] => stdClass Object
(
[post_id] => 210
[post_meta_key] => image
[post_meta_value] => image_value
)
[1] => stdClass Object
(
[post_id] => 212
[post_meta_key] => flag
[post_meta_value] => flag_value
)
[2] => stdClass Object
(
[post_id] => 210
[post_meta_key] => image
[post_meta_value] => image_value
)
[3] => stdClass Object
(
[post_id] => 102
[post_meta_key] => image
[post_meta_value] => image_value
)
)
I would like to create a foreach from $var1 and if $var1[post_id] = $var2[post_id] than $var1 will be edited to something like this
array (
[0] => stdClass Object
(
[post_id] => 210
[post_title] => title
[image] => stdClass Object
(
[0] => image_value
[1] => image_value
)
)
[1] => stdClass Object
(
[post_id] => 212
[post_title] => title
)
)
How can i do this?
foreach ($var1 as &$post1)
{
foreach($var2 as $post2)
{
if ($post1->post_id == $post2->post_id)
{
$post1->image = (object)array(
$post2->post_meta_value
);
}
}
}
Better use arrays instead of objects here:
foreach ($var2 as $key2=>$var2){
if (!empty($var1[$key2])){
$var1[$key2]['image']->$var2;
}
}
You could cast arrays and objects back and forth with
$array = (array)$object;
$object = (object)$array;
This is what you want:
foreach ( $var1 as $v1 )
{
foreach ( $var2 as $v2 )
{
if ( $v1['post_id'] == $v2['post_id'] )
$v1['image'][] = $v2['post_meta_value'];
}
}