0
$universe = $_SESSION["company"];
$sql = "SELECT manufacturer FROM spaceships WHERE `$universe` = 1";
$companyListQuery = $db->prepare($sql);
$companyListQuery->execute();
$companyList = $companyListQuery->fetchAll();
$companyList = array_unique($companyList);
foreach($companyList as $row){
$selected = $row["manufacturer"];
echo '<tr>';
echo '<td class="table">'.$row["manufacturer"].'</td>';
}

So this is giving me a notice saying there is an array to string conversion. But as far as I'm aware fetchAll() returns an array so I'm not sure why it's saying this? Help is appreciated.

6
  • Which line is the warning actually coming from? You've got at least two places where an array could be being used in a string context. Commented Feb 18, 2014 at 21:54
  • Line 40, so where $companyList = array_unique($companyList); Commented Feb 18, 2014 at 21:58
  • the error is correct. array_unique does a string comparison on each element, so it expects an array of strings (or things that can be converted to strings), but you are giving it an array of arrays. it is trying to convert the arrays into strings Commented Feb 18, 2014 at 21:59
  • @Luis Masuelli 's answer is correct, so what are you trying to do, have an array of unique rows? Commented Feb 18, 2014 at 21:59
  • I noticed his answer is correct, yes. So that would explain why the error is happening. But how would I get the duplicate rows to be omitted? Commented Feb 18, 2014 at 22:02

2 Answers 2

4

array_unique compares (string)$a == (string)$b. your array is an array of arrays, so each element is converted to array. there's the notice (array_unique is good for scalars)

official doc: Note: Note that array_unique() is not intended to work on multi dimensional arrays.

http://php.net/array_unique here

Sign up to request clarification or add additional context in comments.

Comments

1

Luis Masuelli's answer is correct for the question, but as a follow on, if you want unique rows use DISTINCT:

$sql = "SELECT DISTINCT manufacturer FROM spaceships WHERE `$universe` = 1";

Or in the future to do it in PHP if you're not using a database:

$companyList = array_map('unserialize', array_unique(array_map('serialize', $companyList)));

4 Comments

Thank you very much. I wasn't aware of "SELF DISTINCT" and I'm very glad I know of its existence now. Thanks again.
it's not SELF. it's SEELCT
Whoops, typo.. Sorry about that
@Luis Masuelli: Ha, its SELECT not SEELCT

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.