0

I have a foreach loop that goes through a list of items. For each of these items, I have a while loop that grabs data out of a database.

$output = array();

//$reference is a multidimensional array has been passed to this page
where the element `color` contains the color I want.

foreach ($reference as $c) {

  $color = $c['color'];

$query = "SELECT DISTINCT name FROM $table where colorPreference = $color";
$exquery = mysqli_query($con, $customerQuery);

while ($row = mysqli_fetch_array($exquery)) {
$person = $row['person'];
array_push($output[$color], $person);

  }                
}

So this loops through, the first time searching 'red', and finding 5 people in the fake table who like red. Next, 'blue', where it finds 1 person, and then 'green' where it finds 3.

If I look at the individual results, my first array has "red, blue, green" and my second array has these lists of names.... I just don't know how to add them into an array together.

I'm trying to build an array like this:

Array
(
  [Red] => Array
      (
          [0] => John
          [1] => Sally
          [2] => Bob
          ...
      )

  [Blue] => Array
      (
          [0] => Luke
      )
  [Green] => Array
      (
          ..etc...       
      )

I'm not using array_push correctly though - I'm getting an Warning: Illegal offset type error. What am I doing wrong?

2
  • 1
    $output[$color][] = $person? Commented Mar 23, 2015 at 20:04
  • Also check the value of $color. If it says illegal offset type, it's probably because it's an object or a null value. Commented Mar 23, 2015 at 20:06

2 Answers 2

2

It's been a while since I've worked with PHP, but I think you need to initialize each "color" array that you're going to push into. So...

$output = array();

//$reference is a multidimentional array has been passed to this page
where the element `color` contains the color I want.

foreach ($reference as $c) {

  $color = $c['color'];

  $query = "SELECT DISTINCT name FROM $table where colorPreference = $color";
  $exquery = mysqli_query($con, $customerQuery);

  while ($row = mysqli_fetch_array($exquery)) {
    $person = $row['person'];
    if (!array_key_exists($color, $output)) {
      $output[$color] = array();
    }
    array_push($output[$color], $person);

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

1 Comment

yup, this did it. Thank you so much! You need to add a closing ) to the if (!array_key_exists) statement though :)
0

Try changing:

array_push($output[$color], $person);

Into:

$output[$color][] = $person;

From the manual on array_push:

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.

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.