2017-08-09 119 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

谢谢我希望能够做一个命令来获得结果。 –

回答

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