I am gathering error messages for records based on their ids, e.g. in the following code the id “2342” has two errors. After collecting these, how can I get “all errors for a specific id” without looping through all array items and testing for that id?
$errorMessages = [];
$errorMessages[] = ['2342','error1'];
$errorMessages[] = ['1236','error2'];
$errorMessages[] = ['2342','error3'];
$errorMessages[] = ['6535','error4'];
//get all error messages that belong to e.g. id=2342 in an array
An alternative would be to save the array as, e.g.:
$errorMessages['2342'] = 'error1';
But then the second time I add a message to the id=’2342′, the value is replaced instead of added, so this is not an option.
What is the best way to save multiple strings like this to an array, and then be able to retrieve all strings for a specific array quickly?
simply use an multi dimensional array and store an array of messages of the same id