0

I have a PHP application with a function that was built to expect information from an API call. However, I'm trying to use this function by passing in information that mimics the API data.

I struggle a bit with arrays and this seems to be an object within an array.

I can access the array that the api provides, so when I use the following code ($triggers is the array the api call returns):

print("<pre>".print_r($triggers,true)."</pre>");

I get the following output:

Array
(
    [0] => stdClass Object
        (
            [triggerid] => 18186
            [status] => 0
            [value] => 0
        )

This is the beginning of the function:

function iterate_triggers($triggers){
    $trigger_id_values = array();
    foreach($triggers as $trigger) {
        //Necessary to show human readable status messages.
        $check_status = array(0=>"Up", 1=>"Down", 2=>"Degraded", 3=>"Maintenance");
        array_push ($trigger_id_values, [$trigger->triggerid, $trigger->value]);

So if I wanted to pass this function a [triggerid] => 18186 and [value] => 1 how would i do that?

Currently I'm trying:

iterate_triggers(array(0 => array("triggerid" => 18186,"status" => 0,"value" => 1,)));

but this gives me a "Trying to get property of non-object" error. Please be kind to me, I've done my best to research and structure this on my own to no avail.

2
  • Not sure if my comment would be of help but just want to point out that the error states it literally, you're trying to access an object when it's actually an array hence, Trying to get property of non-object. Commented Oct 16, 2015 at 0:50
  • well it is helpful, but that is part of my problem. I'm not sure how to pass it to the function as an object. Commented Oct 16, 2015 at 0:54

2 Answers 2

1

The easiest way is to cast the assoc array just to an object

In your case this would be

iterate_triggers(array(0 => (object)array("triggerid" => 18186,"status" => 0,"value" => 1,)));
Sign up to request clarification or add additional context in comments.

Comments

1

You are currently creating and passing an array that contains an array, while your function expects an array of objects.

You should create your object beforehand, then construct your parameter array, and pass it to your function.

$obj = new \stdClass();
$obj->triggerid = 18186;
$obj->status = 0;
$obj->value = 1;
$arr = array($obj);
iterate_triggers($arr);

This comment on php.net, and the rest of that object documentation, may be useful to you.

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: <?php $genericObject = new stdClass(); ?>

The error message you are seeing with your own code is caused by $trigger->triggerid inside the function, when $trigger is an array instead of an object as the function expects. Object properties are accessed using $someObject->propertyName notation, while array elements are accessed using $someArray['keyName']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.