Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a piece of code that loops and displays values for job vacancies:

<?php foreach($arrXML as $inner_arr)
foreach($inner_arr as $value) { ?>
        <p>Job Ref: <?php echo $value['jobref']; ?></p>
        <p>Date: <?php echo $value['date']; ?></p>
        <p>Title: <?php echo $value['title']; ?></p>
        <p>Company: <?php echo $value['company']; ?></p>
        <p>Minimum Salary: <?php echo $value['salarymin']; ?></p>
        <p>Maximum Salary: <?php echo $value['salarymax']; ?></p>
        <p>Benefits: <?php echo $value['benefits']; ?></p>
        <p>Salary: <?php echo $value['salary']; ?></p>
        <p>Job Type: <?php echo $value['jobtype']; ?></p>
        <p>Location: <?php echo $value['location']; ?></p>
        <p>Country: <?php echo $value['country']; ?></p>
        <p>Description: <?php echo $value['description']; ?></p>
        <p>Category: <?php echo $value['category']; ?></p>
        <?php } ?>

This works great except with some results, the value for Benefits, Salary, Minimum Salary and Maximum Salary maximum return as 'array'.

Is there a way of showing the value if it isn't an array and the array values if it is?

Many thanks

Pete

share|improve this question
    
You are actually doing the same thing already, including one foreach into another. Why not to just follow the pattern? –  Your Common Sense May 21 '13 at 9:29
1  
@YourCommonSense: I think the issue here is that OP didn't know how to handle the 'can be value, can be array' variable. Though this has already been covered with is_array(). –  Aquillo May 21 '13 at 9:31

1 Answer 1

up vote 3 down vote accepted
if (is_array($value['benefits'])) echo "<p>Benefits: ".implode(",",$value['benefits'])."</p>";
else echo "<p>Benefits: ".$value['benefits']."</p>";

function is_array() explains itself, implode(c,a) does convert an array into a string and puts the character c between all values of the array. if the array will contain only 1 value, the character c will be omitted.

share|improve this answer
    
+1, but please explain at OP why this works and what was the error :) –  DonCallisto May 21 '13 at 9:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.