0
$arrayDif = array();
$arrayDif[] = array('employer' => $employer,
 'comment' => $comment,
 'value' => $resultValue);

Filling in from a loop.

Below is the array that I have filled up. I need to be able to find a match by the 'employer' and 'comment' and extract the value, so I can re-update this value.

Array
(
 [0] => Array
 (
 [employer] => Albury-Wodonga
 [comment] => allOtherMembers
 [value] => 7
 )
 [1] => Array
 (
 [employer] => Albury-Wodonga
 [comment] => associateMembers
 [value] => 1
 )
Pang
10.2k146 gold badges87 silver badges126 bronze badges
asked Aug 9, 2017 at 2:14
2
  • I deleted my entire answer now that I understand your question better. You need to learn to loop through an array. There are tons of tutorials out there for that. Better you learn that over just being shown. You need a foreach() loop. Commented Aug 9, 2017 at 2:27
  • Thanks I was hoping to just do one command to get the result. Commented Aug 9, 2017 at 2:31

2 Answers 2

2

One command to extract and re-update the value, I suggest to use foreach loop

<?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) {
 	if($v['employer'] == $employer && $v['comment'] == $comment) {
 	 if(empty($value)) {
 	 return $v['value'];
 	 } else {
 	 $v['value'] = $value;
 	 }
 	}
 }
 return "Not Found.";
 }
 
 //update
 func($arrayDif, 'AAA', 'comment 1', "123123");
 
 //search
 echo func($arrayDif, 'AAA', 'comment 1');
?>

answered Aug 9, 2017 at 3:25
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if this is what you are looking for but here you go. I would use a function and simple loop.

<?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");
?>

answered Aug 9, 2017 at 2:49

2 Comments

This is something close to what I had tried. Your solution has an 'Undefined offset: 0' and 1 and 2 It occurs on line: $array[$i][$j]["employer"]
Sorry to see you get that error. I am running the exact same code above on my page and it is working without errors.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.