1

I just tried to create a spinner using MySQL and PHP, but after a few a hours it displays "ArrayArrayArray" in front of the expected output.

if($_POST['artikel']){
    $artikel = nl2br($_POST['artikel']);
    $ar = explode("\n\r",$artikel);
    $hasil = "";
    foreach($ar as $ars){
        $newstring1 = explode(" ", $ars);
        foreach($newstring1 as $newstring2){
            $newstring3 = explode("\n", $newstring2);
            foreach($newstring3 as $newstring4){
                $newstring5 = explode(",", $newstring4);
                foreach($newstring5 as $value){
                    $cari = mysql_query("select * from sinonim where kata1='$value'");
                    $j = mysql_num_rows($cari);
                    if($j>0){
                        $ka = mysql_fetch_array($cari);
                        $hasil = $hasil."<span class='re'>".$ka['kata2']." ";
                        $hasil = explode(",", $hasil);
                        foreach($hasil as $vv) {
                            $hasil = $hasil.$vv."</span> "; 
                        }
                    }else{
                        $cari2 = mysql_query("select * from sinonim where kata2='$value'");
                        $j2 = mysql_num_rows($cari2);
                        if($j2>0){
                            $ka2 = mysql_fetch_array($cari2);
                            $hasil = $hasil."<span class='re'>".stripslashes($ka2['kata1'])."</span> ";
                        }else{
                            $hasil = $hasil.$value." ";
                        }
                    }
                }
            }
        }
    }
    echo $hasil;
}

The result looks like this

ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayhello all......

Why is "Array" printed? How can I avoid that?

3
  • i just think my problem here $hasil = explode(",", $hasil); foreach($hasil as $vv) { $hasil = $hasil.$vv."</span> "; } but i don't know where to fix I just want result like this hello all...... not ArrayArrayArrayhello all...... Commented Sep 23, 2014 at 21:36
  • 1
    You need to explain what you want to do? What's the input, what should be the output... It's actually nothing more than a code-soup. Commented Sep 23, 2014 at 21:37
  • The lack of indentation hurts my eyes! Commented Sep 23, 2014 at 22:05

1 Answer 1

2

Your code is a mess. The problem occurs in this part

    $hasil = $hasil."<span class='re'>".$ka['kata2']." "; // line 1
    $hasil = explode(",", $hasil);                        // line 2
    foreach($hasil as $vv) {                              // line 3
        $hasil = $hasil.$vv."</span> ";                   // line 4
    }                                                     // line 5

In line 2 you use explode to split $hasil into an array, then in line 4 you try to append it to itself, although it's an array now not a string. This causes the "Array" output.

Eventually you want to use another variable here. Also you're adding multiple </span>, which will invalidate your HTML. I guess it should look like this:

    $hasil = $hasil."<span class='re'>".$ka['kata2']." ";
    $temp = explode(",", $hasil);
    foreach($temp as $vv) {
        $hasil = $hasil.$vv;
    }
    $hasil = $hasil."</span> ";

But line 4 still doesn't make sense. You're iterating over text tokens, which are already part of the output. What do you want to append here?

In your edit you stated that changing line 4 to $hasil = $vv."</span> "; solved your problem, but it looks like you're still producing invalid HTML and your code is way too complicated for what you're trying to achieve.

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

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.