0

I'm trying to use isset to check if the $filtered_data returns a set of data.

I'm using this code rather than foreach because datasetID is a unique id.

My novice understanding is that if (isset($filtered_data)) returns true or false? So the if mrans that if records are returned do x. So, currently when I put in $lastPart = '5fd4058e5c8d2' I'm getting the expected result. When I change the 2 to a 3 (a non-existing ID) $lastPart = '5fd4058e5c8d3' I get Undefined index: 5fd4058e5c8d3`.

I expect the isset is doing its job and the error is thrown on this line as indicated in the error message $filtered_data = array_column(array_merge(...$data), null, 'datasetID')[$lastPart]; am I missing the obvious? I guess this needs a get out clause if the value $lastPart does not exist in the array?

$filtered_data = array_column(array_merge(...$data), null, 'datasetID')[$lastPart];

if (isset($filtered_data)){
  echo 'qwertyuio';
  $datasetID = $filtered_data['datasetID'];
  $collectionCode = $filtered_data['collectionCode'];
  $datasetName = $filtered_data['datasetName'];
  $ownerInstitutionCode = $filtered_data['ownerInstitutionCode'];
  $vernacularName = $filtered_data['vernacularName'];
  $elementName = strtolower($filtered_data['elementName']);
} else {
  echo 'not set';
}
2
  • array_column() always returns an array, so $filtered_data will always be set. Commented Dec 16, 2020 at 21:16
  • @Barmar True, but the OP is attempting to assign an element in that returned array. Commented Dec 16, 2020 at 21:43

2 Answers 2

1

A quick fix would be to use null coalescing (??) and setting the value to a dummy value if not found, then your isset() can be reworked to check for the dummy value...

$filtered_data = array_column(array_merge(...$data), null, 'datasetID')[$lastPart] 
                  ?? null;

if ($filtered_data){
Sign up to request clarification or add additional context in comments.

2 Comments

great, that works! But you say quick fix is it the best fix, the best way to do this?
TBH, without knowing how the data is created, it is a simple fix. IF you could alter the $data array, you could index it by the 'datasetID' column as you load it, then use isset($data[$lastPart])
1

You're missing the obvious: you're trying to access a non-existent element when you assign it to $filtered_data, so the error occurs there. You're not testing for its existence until the next line.

Try this:

$filtered_data = array_column(array_merge(...$data), null, 'datasetID');
if (isset($filtered_data[$lastPart]) {
  $filtered_data = $filtered_data[$lastPart];
  echo 'qwertyuio';
  $datasetID = $filtered_data['datasetID'];
  $collectionCode = $filtered_data['collectionCode'];
  $datasetName = $filtered_data['datasetName'];
  $ownerInstitutionCode = $filtered_data['ownerInstitutionCode'];
  $vernacularName = $filtered_data['vernacularName'];
  $elementName = strtolower($filtered_data['elementName']);
} else {
  echo 'not set';
}

Comments

Your Answer

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.