2014-02-18 2 views
0

배열이있는 경우 열 이름이 일치하는지 어떻게 알 수 있습니까? 저는 루프가 있습니다. 칼럼의 이름은 1에서 6까지입니다. 클릭시 이미지를 삭제하려고합니다.yii에 지정된 배열과 열 이름이 일치하는지 확인하는 방법

이것은 작동하지 않습니다. 내가 $product_id= "product_gallery_"+[$i];1$i을 변경하는 경우에도

for ($i=1; $i<=6; $i++) 
     { 
     $product_img= "product_gallery_"+[$i]; 
      if($model->getAttributeLabel($product_id)==$image_attr) // not sure about $image_attr either, it was passed from view 
      { 
      $filename = $model->$product_img; 
      unlink($path.$filename); 
      $model->product_gallery_[$i] = "default.png"; 
      $model->save(); 
      echo "removed"; 
      } 
     } 
+1

$key 요소에 액세스 할 $variable[$key]를 예에 액세스 할 수없는 키에 의한 배열 요소를 해결하기 위해 사용 $ i? +는 PHP 연결 연산자가 아닙니다 – Anigel

+0

"product_gallery_"와 [$ i]를 연결하려면 '.'을 사용해야합니다. '+'나는 생각하지 않습니다. – MisterJ

+0

아하하 .. 알았어! 감사 – JamAndJammies

답변

2

PHP를 사용하여 작동하지 않습니다. (점)을 연결 연산자로 + 기호가 아닙니다. 또한

에서 [] (대괄호) 개별 변수는 대괄호받은 이유 $variable 배열

for ($i=1; $i<=6; $i++) 
{ 
    $product_img= "product_gallery_".$i; 
    if($model->getAttributeLabel($product_id)==$image_attr) // not sure about $image_attr either, it was passed from view 
    { 
     $filename = $model->$product_img; 
     unlink($path.$filename); 
     $model->$product_img = "default.png"; 
     $model->save(); 
     echo "removed"; 
    } 
} 
관련 문제