0

In this example, the function validateJsonNotEmptyImage does not work as expected?

Just to clarify.

'src' => $image->src, // WORKS
'height' => $this->validateJsonNotEmptyImage('type', 'width'), // DOES NOT WORK
public function validateJsonNotEmptyImage($type, $array1) {
    if ($type, $array1) {
        if (isset($type->$array1)) {
            return $type . '->' . $array1;
        } else {
            return null;
        }
    }
}
$productsImagesToSiteArray = array();
    foreach ($this->resource->images as $image) {
        $productsImagesToSiteArray[] = array(
            'height' => $this->validateJsonNotEmptyImage('$image', 'width'),
            'src'    => $image->src,
        );
    }
1
  • your should remove single quote on $image Commented Nov 14, 2019 at 1:38

1 Answer 1

1

I see two mistakes

1) your method validateJsonNotEmptyImage, in body you have wrong IF it should be check with null[here1] and you should return value not string[here2].
And also you should return everytime any value or null[here3].

public function validateJsonNotEmptyImage($type, $array1) {
    if ($type != null && $array1 != null) {            <----------- here1
        if (isset($type->$array1)) {
            return $type->$array1;                     <----------- here2
        } else {
            return null;
        }
    }
    return null;                                       <----------- here3
 }

2) you should pass variable not string in your call (remove quotes from $image)

$this->validateJsonNotEmptyImage($image, 'width')
Sign up to request clarification or add additional context in comments.

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.