0

Note: I am Using jkanban.js plugin for drag and drop that's why I need to get the result in json format

This is my code

//Declare global empty array
$nodes = [];
$docs = [];

foreach($getStations as $key => $step){
   foreach($docs as $key=>$val) {
       $docs[] = array(
                    "id" =>"$val[0]",
                    "title" => "$val[2]",
                    "class" => "color$color"
                );
   }//second foreach

  $nodes[] = array(
    "id"=>"step_ID",
    "title"=>"Some Title",
    "class"=>"Some Class",
    "item" =>$docs //Pushing the array $docs
);
}//end main foreach

//Display the result
echo json_encode($nodes);

This is the screenshot of the result

The number inside parenthesis is the total items

The number inside parenthesis is the total items

What I want is to get rid of the loop result of array docs inside nodes

2 Answers 2

2

Declare your variable inside your first loop, if you attempt to push your second loop.

$arr1 = array();
foreach(){
    $arr2 = array();
    foreach(){

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

Comments

1

This works now

by declaring the variable $docs empty array inside the first foreach loop...thanks to marjameson

 //Declare global empty array
$nodes = [];

foreach($getStations as $key => $step){
  //Declare docs empty array
   $docs = [];

   foreach($docs as $key=>$val) {
       $docs[] = array(
                    "id" =>"$val[0]",
                    "title" => "$val[2]",
                    "class" => "color$color"
                );
   }//second foreach

  $nodes[] = array(
    "id"=>"step_ID",
    "title"=>"Some Title",
    "class"=>"Some Class",
    "item" =>$docs //Pushing the array $docs
  );
}//end main foreach

//Display the result
echo json_encode($nodes);

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.