2017-08-09 13 views
0
$arrayDif = array(); 
$arrayDif[] = array('employer' => $employer, 
        'comment' => $comment, 
        'value' => $resultValue); 

ループからの入力。多次元配列からの値の抽出

以下は、私が記入した配列です。私は '雇用者'と 'コメント'によって一致を見つけて価値を引き出すことができる必要があるので、私はこの価値を再更新することができます。

Array 
(
    [0] => Array 
     (
      [employer] => Albury-Wodonga 
      [comment] => allOtherMembers 
      [value] => 7 
     ) 

    [1] => Array 
     (
      [employer] => Albury-Wodonga 
      [comment] => associateMembers 
      [value] => 1 
     ) 
+0

を使用することをお勧めあなたの質問をよりよく理解してください。配列をループすることを学ぶ必要があります。そこにはたくさんのチュートリアルがあります。あなただけが表示されている以上にそれを学ぶことをお勧めします。 'foreach()'ループが必要です。 – Difster

+0

私は結果を得るために1つのコマンドを実行したいと考えていました。 –

答えて

0

これがあなたが探しているものであるかどうかはわかりませんが、ここにはありますか。私は関数と単純なループを使用します。

<?php 
 
    $arrayDif = array(); 
 

 
    $arrayDif[] = array(
 
        array('employer' => "Albury-Wodonga", 'comment' => "allOtherMembers", 'value' => "1 star"), 
 
        array('employer' => "Employer2", 'comment' => "Good Job", 'value' => "2 stars"), 
 
        array('employer' => "Employer3", 'comment' => "Smart", 'value' => "3 stars") 
 
       ); 
 
    
 
    // Function for searching the array for the matches and returning the value of the match. 
 
    function SearchMe($array, $searchEmployer, $searchComment){ 
 
     for($i = 0; $i < count($array); $i++){ 
 
      for($j = 0; $j < count($array[$i]); $j++){ 
 
       
 
       if(
 
        $array[$i][$j]["employer"] == $searchEmployer && 
 
        $array[$i][$j]["comment"] == $searchComment 
 
       ){      
 
        return $array[$i][$j]["value"]; 
 
       } 
 
      } 
 
     } 
 
     return "No Match"; 
 
    } 
 
    
 
    echo SearchMe($arrayDif, "Albury-Wodonga", "allOtherMembers"); 
 
?>

+0

これは私が試したものに近いものです。あなたの解は「未定義オフセット:0」と1と2を持っています。 $ array [$ i] [$ j] ["employer"] –

+0

上記のコードと同じコードをページ上で実行していますが、エラーなしで動作しています。 – Icewine

2

値を抽出し、再更新するための一つのコマンドで、私は今、私というのが私の全体の答えを削除したforeachループ

<?php 
 
    $arrayDif = array(); 
 

 
    $arrayDif[] = array('employer' => "AAA", 'comment' => "comment 1", 'value' => "1"); 
 
    $arrayDif[] = array('employer' => "BBB", 'comment' => "comment 2", 'value' => "2"); 
 
    $arrayDif[] = array('employer' => "CCC", 'comment' => "comment 3", 'value' => "3"); 
 
    
 
    // function for setting the value or returning the value 
 
    // notice the $array here is a reference to the real array 
 
    function func(&$array, $employer, $comment, $value = ''){ 
 
     // $v is also a reference 
 
     foreach ($array as $k => &$v) { 
 
     \t if($v['employer'] == $employer && $v['comment'] == $comment) { 
 
     \t  if(empty($value)) { 
 
     \t   return $v['value']; 
 
     \t  } else { 
 
     \t   $v['value'] = $value; 
 
     \t  } 
 
     \t } 
 
     } 
 
     return "Not Found."; 
 
    } 
 
    
 
    //update 
 
    func($arrayDif, 'AAA', 'comment 1', "123123"); 
 
    
 
    //search 
 
    echo func($arrayDif, 'AAA', 'comment 1'); 
 
?>

+0

ありがとうございます。正しい値を提供します。 –

+0

あなたはそれを "受け入れ"とすることができます^^/ – Zhwt