2011-07-27 8 views
0

데이터 키를 사용하여 배열 값을 받고 (?)PHP 나는이 배열 가지고

$data = 'physicalPercent:10, icePercent:10, holyPercent:-10'; 

는 지금 폭발 할 필요를 으로 $ 데이터 : 배열 키 값을 얻거나 더 좋은 방법이 있습니까?

나는이 같은 일을 wan't :

$v = explode(':', $data); 

그리고 다음을 $v[0]는 철 인 경우. physicalPercent 다음 그것은

/images/gems/physical.gif

을 선택합니다 그리고 같은에서, 나는 후 숫자 값을 조작해야합니다, 그래서 필요로하는 작품이 같은 :

if($v[1] > xx and $v[1] < yy) 

$v[0]과 일치하는 배열 값을 선택하십시오.

죄송하지만 영어가 도움이 필요합니다. :) 아마도 다음과 같은

답변

2

뭔가 :

foreach(explode(', ', $data) as $prop) { 
    list($propName, $propVal) = explode(':', $prop); 
    // $propName would be physicalPercent, 
    // $propVal would be 10 for the first iteration, etc 

    // now get the image 
    $img = $allImmunities[$propName]; 

    echo $img . '<br/>'; 
} 

전체 코드 (데이터 포함) :

<?php 
    $allImmunities = array(
    'poisonPercent' => '/images/gems/earth.gif', 
    'earthPercent' => '/images/gems/earth.gif', 
    'paralyzePercent' => '/images/gems/paralyze.gif', 
    'deathPercent' => '/images/gems/death.gif', 
    'energyPercent' => '/images/gems/energy.gif', 
    'icePercent' => '/images/gems/ice.gif', 
    'firePercent' => '/images/gems/fire.gif', 
    'physicalPercent' => '/images/gems/physical.gif', 
    'holyPercent' => '/images/gems/holly.gif', 
    'invisiblePercent' => '/images/gems/invisible.gif' 
    ); 

$data = 'physicalPercent:10, icePercent:10, holyPercent:-10'; 
foreach(explode(', ', $data) as $prop) { 
    list($propName, $propVal) = explode(':', $prop); 
    // $propName would be physicalPercent, 
    // $propVal would be 10 for the first iteration, etc 

    // now get the image 
    $img = $allImmunities[$propName]; 

    echo $img ."\n"; 
} 

출력 :

$ php game.php 
/images/gems/physical.gif 
/images/gems/ice.gif 
/images/gems/holly.gif 
+0

벌금을,하지만 지금은 어떻게 이미지를 선택하는 ? – Lucas

+0

여러 개의 이미지 (physicalPercent, icePercent, holyPercent)를 선택해야한다는 것을 기억하십시오. – Lucas

+0

내 편집 내용을 볼 수 있을지 모르지만 예, 편집 됨 –

0

은 루프를 사용하여 얻을 데이터를 폭발 정보.

+0

나는 그것을 시험해 볼 것이다 !! – Lucas

+0

어떻게 'physicalPercent'와 다른 변수를 변수로 grep 할 수 있습니까? – Lucas

0

먼저 키/값 쌍으로 폭발 할 수 있습니다 후이를 기반으로 값 검색 :

$data = 'physicalPercent:10, icePercent:10, holyPercent:-10'; 

foreach(explode(', ', $data) as $item) 
{ 
    list($key, $value) = sscanf($item, '%[a-zA-Z]:%d'); 

    echo $allImmunities[$key], "\n"; 
} 

출력 (Demo) :

/images/gems/physical.gif 
/images/gems/ice.gif 
/images/gems/holly.gif