Skip to content
Advertisement

PHP Return Array from function [closed]

I have a very simple web app that is capturing RFID tag reads and then submits it into the Database.

I have a function that was to pass the information through a filter and remove the duplicates and then return an array of unique tag reads.

The function looks like this

$txtarea = $_POST["rfid"];

rfid($txtarea);

function rfid($txtarea){
  $array = explode("rn", $txtarea);

  $rfid_array1 = array_unique($array);

  return $rfid_array1; 
}

I then use Print_r to check the contents of the array to make sure it works.

When I run the code inside the function I do not get a result returned but when I run the following outside the function

$txtarea = $_POST["rfid"];

rfid($txtarea);

$array = explode("rn", $txtarea);

$rfid_array1 = array_unique($array);

It returns the values correctly ?

I am very new to PHP so I apologize if this question seems a little basic.

Advertisement

Answer

The function rfid returns a value which you could capture in a variable.

$rfid_array1 = rfid($txtarea);

Note that you could shorten the function a bit:

function rfid($txtarea){
    return array_unique(explode("rn", $txtarea));
}

Demo on https://3v4l.org/DY8Ts

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement