0

I have two full arrays, of about 10 values each. I have combining those arrays and displaying them through this line of code.

foreach(array_combine($hpictures, $highschools) as $pictures => $hschool)

and echoing them out into a css format.

The array's combine and code works perfectly the only problem is that for some reason it is only displaying of the values in the array, and not going through and displaying all 10. I am wondering if the foreach or array_combine is the reason for this.

Edits:

       if(!empty($highschools)){
               echo "<h3>High School Division</h3>";
               echo "<ul>";
                foreach(array_combine($hpictures, $highschools) as $pictures => $hschool){
                        echo "<li><img src='$pictures'/><a href='./schoolpage.php?school=$hschool'><strong>$hschool</strong></a></li>";
                    }



               echo "</ul>";
               }

Thanks for any help, and hope this is helpful to others as well.

4
  • Can you post more code? Example arrays and also how you're outputting the values? Commented Feb 15, 2013 at 2:37
  • 2
    start with print_r(array_combine($hpictures, $highschools)); Commented Feb 15, 2013 at 2:37
  • 1
    Are you sure you don't want array_merge() instead? array_combine() assumes you have one array of keys and one array of values Commented Feb 15, 2013 at 2:39
  • I have made edits and added more code. @dagon I did that and it displays the when the arrays combine there are only three values but seperatly there are 10. Thanks Commented Feb 15, 2013 at 2:40

1 Answer 1

3

Without knowing the structure of your data, all I can assume is you may need array_merge() instead of array_combine()

foreach(array_merge($hpictures, $highschools) as $pictures => $hschool)

And for a complex array strucure, try array_merge_recursive()

foreach(array_merge_recursive($hpictures, $highschools) as $pictures => $hschool)

Differences

array_combine(array('dog','cat'),array('fred','felix'))

would make an array like:

array('dog'=>'fred','cat'=>'felix')

where as

array_merge(array('dog','cat'),array('fred','felix'))

would produce

array('dog','cat','fred','felix');
Sign up to request clarification or add additional context in comments.

3 Comments

I tried array merge and that worked but displayed the pictures and text each as a list item. Should I merge them and them run THEN through a foreach loop?
I think we really need to see the structure of your arrays to understand the problem better. It may even require manual iteration and assignment through the two arrays.
The array_merge worked. Can you maybe explain the difference real fast, so I can better understand?

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.