-3

I want to use foreach in an array and this foreach uses an array. The code is here:

$records["data"][] = array(
  $id,
$value['name'],
foreach($kichen_organ as $x => $x_value){  if($value['organ']==$x) '<span class="badge badge-success">'echo $x_value;'</span>' },
$value['type'] ,
$value['name_responsible'] ,
$value['family_responsible'] ,
$value['office'],
$value['fax'],
$value['mobile']);

$kichen_organ is another array that I parse it's key and value by renaming $x and $s_value but I received this error:

syntax error, unexpected 'foreach' (T_FOREACH), expecting ')' in /var/www/html/...

What's the solution for using the loop in array?

9
  • You can add the elements to array after array declaration right? Commented Jul 11, 2016 at 6:52
  • what is this 'echo $x_value;' , in foreach , concatenation and foreach loop in array element ? Commented Jul 11, 2016 at 6:53
  • @Thamilan I add them in array declaration not after that Commented Jul 11, 2016 at 6:53
  • You're trying to echo a string as an element in an array ...? You've mixed up the code - and there's must be a better way of achieving your goal. Let's start by understanding what's your end goal? Commented Jul 11, 2016 at 6:54
  • @OfirBaruch The goal is clear. I want to print some thing by using if for an element Commented Jul 11, 2016 at 6:55

1 Answer 1

0
$temp = [];

foreach($kichen_organ as $x => $x_value) {
    if ($value['organ'] == $x) {
        $temp[] = '<span class="badge badge-success">'.$x_value.'</span>';
    };
}

$records["data"][] = array_merge($temp,
    [
        $id,
        $value['name'],
        $value['type'] ,
        $value['name_responsible'] ,
        $value['family_responsible'] ,
        $value['office'],
        $value['fax'],
        $value['mobile']
    ]);
Sign up to request clarification or add additional context in comments.

1 Comment

I received the error: syntax error, unexpected '$x_value' (T_VARIABLE)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.