3

I think there is a problem when I echo $whereArray and orderByArray. If I type in a word such as "Question" and then submit it, I expect it to display in the echos "%".Question."%"; for both arrays. But instead in both echos it just displays "Array" for both echos. Does this mean that both arrays are not working when it comes to storing in the values?

 $searchquestion = $_GET['questioncontent'];
    $terms = explode(" ", $searchquestion);

$whereArray = array();
$orderByArray = array();


    //loop through each term
    foreach ($terms as $each) {
        $i++;
        $whereArray[] = "%".$each."%";
        $orderByArray[] = "%".$each."%"; 


    }

        echo $whereArray;
        echo $orderByArray;
0

4 Answers 4

2

echo() only works for strings. PHP converts your array to "Array" as a fallback.

When you're debugging, you should use var_dump(). It will tell you the type of the object and its content.

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

Comments

1

Use var_dump or print_r instead of echo (they are functions, not constructs like echo is).

4 Comments

It has nothing to do with being a function or a construct. echo just doesn't deep toString arrays like the other functions do.
@mellamokb what I meant was that functions need parentheses.
You should be able to do var_dump $a; as well as echo($a); depending on your configuration. I think mellamokb meant that your reasoning is wrong, even if the answer is right.
@ExplosionPills: Gotcha, I thought that was your explanation :)
1

An array needs to be printed out using a special function such as print_r. If you want to print out a value in your array try:

echo $whereArray[0];

To get the first element. Be careful because if the array is empty you will get an error.

Comments

1

you can also loop through them

foreach($arrayname as $value)
     echo $value;

or

  echo implode("",$arrayname);

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.