LeeBronwin LeeBronwin - 6 months ago 11
PHP Question

PHP Array Check the attribute, return value based on attribute

I am trying to return pull a value based on an attribute from an array, and it seems straight forward enough but I can't seem to nail down the correct way to accomplish this.

Here is the array I am trying to pull from:

[1] => InfoOptions Object
(
[description] => INFO
[optSequence] => 2
[eqpObject] => CUSTOMER NTWK ENG
[attribute] =>
[eqpValue] =>
[dlrSequence] => 10
)

[2] => InfoOptions Object
(
[description] =>
[optSequence] => 3
[eqpObject] => CUSTOMER TEST
[attribute] => CUSTOMER
[eqpValue] => Jon Doe
[dlrSequence] => 10
)


Here is what I have so far:

if (is_array($provisionCVResult->path->infoOptions-_InfoOptions)) {
foreach ($provisionCVResult->path->infoOptions ->InfoOptions as $cv_obj) {
$CVA = array();
$result = null;

foreach ($CV_obj as $value) {
if($value['attribute'] == 'CUSTOMER') {
$CVA["eqpValue"] = $cv_obj->eqpValue;
break;
}
}

$this->cvArrayDataList[] = $CVA;
}
}


Where am I going wrong?

Answer
  1. If $provisionCVResult->path->InfoOptions is an array, it does not make sense to write $provisionCVResult->path->InfoOptions ->InfoOptions in the foreach EDIT: I red in the comments that the array is $provisionCVResult->path->InfoOptions->InfoOptions
  2. PHP is case sensitive so $cv_obj and $CV_obj are two different variables
  3. The second foreach is not needed

So, assuming $provisionCVResult->path->InfoOptions->InfoOptions is returning an array of InfoOptions Object, I think you should do something like this:

if (is_array($provisionCVResult->path->InfoOptions->InfoOptions)) 
{

    $result = null;

    foreach($provisionCVResult->path->InfoOptions->InfoOptions as $cv_obj)
    {

        if($cv_obj->attribute == 'CUSTOMER')
        {
            $this->cvArrayDataList[] = array("eqpValue" => $cv_obj->eqpValue);
        }

    }

}