I'm trying to add a new set of key and values into a two dimensional associative array. Here is my array
myArray = array(1 => array("John", 500, "2012-09-02"),
2 => array("Mike", 105, "2012-07-01"),
3 => array("Joe", 24, "2011-12-23"),
4 => array("Alex", 65, "2012-08-30"));
What I would like to do is have user input an ID and a name than either increase value[1] by 5 if they entered data is in myArray, but if it's not than I would like to add their information as new data into the array so it should output/print a 5th $key and $values. The if statement works fine and adds 5 to value[1], but I can't seem to get the else if statement to add the new entry.
$nameExists = FALSE;
$name = htmlspecialchars($_POST['name']);
$id = htmlspecialchars($_POST['id']);
foreach($myArray as $key => $value){
if($key == $id && $value[0] == $name){
$value[1] = $value[1] + 5;
$nameExists = TRUE;
}
else if ($nameExists === FALSE){
$newData = array($name, 1, date("Y-m-d"));
$myArray[] = $newData;
}
echo "<tr><td>".$key."</td>
<td>".$value[0]."</td>
<td>".$value[1]."</td>
<td>".$value[2]."</td></tr>";
}
any help appreciated, thank you.
It looks like you have a logic flow problem with the check for $nameExists === FALSE
. That should be tested after the entire foreach
loop has completed, rather than inside the loop body. Otherwise, the loop tests the first array element for a match and if not found right then, proceeds to append to the array.
Instead, move the $nameExists === FALSE
to after the loop. If no match was found, that flag will not have been set TRUE
and the array append should work.
$nameExists = FALSE;
// Note: use htmlspecialchars() on *output to HTML* not on input filtering!
// If you print these values later in the page, you should enclose them
// in htmlspecialchars() at that time. Placing it here may cause
// your values not to match in the loop if the stored vals were unescaped.
$name = $_POST['name'];
$id = $_POST['id'];
// Note the &$value is needed here to modify the array inside the loop by reference
// If left out, you'll get through the loop and changes will be discarded.
foreach($myArray as $key => &$value){
if($key == $id && $value[0] == $name){
$value[1] = $value[1] + 5;
$nameExists = TRUE;
// No need to continue looping, you can exit the loop now
break;
}
// Finish loop - flag was set TRUE if any match was found
}
// Now, if nothing was modified in the loop and the flag is still FALSE, append a new one
if ($nameExists === FALSE){
$newData = array($name, 1, date("Y-m-d"));
$myArray[] = $newData;
}
// Use a separate loop to output your complete array after modification
foreach ($myArray as $key => $value) {
// use htmlspecialchars() here instead...
// And display output values indexed $myArray[$id]
echo "<tr><td>".$key."</td>
<td>".htmlspecialchars($value[0])."</td>
<td>".htmlspecialchars($value[1])."</td>
<td>".htmlspecialchars($value[2])."</td></tr>";
}
Check the notes in the PHP foreach
documentation about modifying the looped array with &
references.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.