0

I am working on parsing HTML and get multidimensional output array as json. I am parsing html like I want but I couldn't create JSON array.

The example output of foreach loop:

PS: every json object has different string value.

0:"blahblah"

1:"blahblah"

2:"blahblah"

3:"blahblah"

4:" " // only space

5:"blahblah"

6:"blahblah"

7:"blahblah"

8:"blahblah"

9:" " // only space

...

I want create json array like this:enter image description here

 $output = array();
    $html = str_get_html($ret);

    $lessons["lesson"] =array();
    foreach($html->find('table//tbody//tr') as $element) {

        $temp = strip_tags($element->innertext);

        array_push($lessons['lesson'], $temp); // the objects (I wrote as 'blahblah' every object but I getting different values always)

        if($temp == " ") // if there is only space push array the output and create new array
        {

            array_push($output , $lessons["lesson"]);
            unset($lessons);
            $lessons["lesson"] = array();
        }
    }
echo (json_encode($output ,JSON_UNESCAPED_UNICODE)); // $output show nothing

Thanks in advice.

0

1 Answer 1

2

If your issue is getting all the blah into the array then the below will get you there. I am not following the code too well but attempt to explain my thoughts in comments.

$json = ["blahblah"
,"blahblah"
,"blahblah"
,"blahblah"
," "
,"blahblah"
,"blahblah"
,"blahblah"
,"blahblah"
," "];


$lessons["lesson"] = []; // I think this is the array you are using
$tmp = []; // Something tmp to hold things
foreach($json as $elm){ //Loop what I assume $html->find('table//tbody//tr') is returning
    if($elm != ' '){//Wait for a ' ' and add to tmp
        $tmp[] = $elm;
    } else {
         $lessons["lesson"][] = $tmp; // This array is done so keep it and restart
         $tmp = [];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! bu i changed your if clause as "if($elm != ' ') then it worked! If you update your post like this, I am gonna accept your answer

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.