1

Here is my code i tried to explode string, but get some errors, i got string but i did not explode, Here i getting some erros,i cant understand it A PHP Error was encountered

Severity: Warning Message: explode() expects parameter 2 to be string, object given Filename: views/testing.php Line Number: 65

Here is My code:

<?php

foreach ($test as $t)   
{                      
   echo $t->user_rights;
   $t=(explode(",", $t));                                   


 }

?>
8
  • what's the content of $test ??? Commented Jul 21, 2018 at 10:13
  • $test is controller variable, get data from database Commented Jul 21, 2018 at 10:14
  • it should have column name like this $t->column_name Commented Jul 21, 2018 at 10:16
  • echo $t->user_rights; $t=(explode(",", $t)); Commented Jul 21, 2018 at 10:17
  • also tried it @pradeep but it did not work Commented Jul 21, 2018 at 10:17

3 Answers 3

2

Hope this will help you :

foreach ($test as $t)   
{   
    /* this will trigger an error since $t->user_rights is an array . 
       To use explode $t->user_rights should be a string
    */                  
    $user_rights = (explode(",", $t->user_rights));
    print_r($user_rights);                                   
}

UPDATE :

If you want a comma separated values of user_rights then you have to use implode() instead of explode();

foreach ($test as $t)   
{                      
    $user_rights = implode(",", $t->user_rights);
    echo $user_rights ;                                   
}

For more : http://php.net/manual/en/function.implode.php

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

6 Comments

still not work,, error:Message: Array to string conversion
Array ( [0] => stdClass Object ( [user_rights] => Array ( [0] => 5 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 1 [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 [18] => 1 [19] => 1 [20] => 1 [21] => 1 [22] => 1 [23] => 1 [24] => 1 [25] => 1 ) ) ) yes got it in array,,,,but now how to print it
i got output like this yes its correct, but how to print it now Array ( [0] => 5 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 1 [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 [18] => 1 [19] => 1 [20] => 1 [21] => 1 [22] => 1 [23] => 1 [24] => 1 [25] => 1 )
$t->user_rights has an array and if u want comma separated values of user_rights then u have use impload instead of explode, what do u say?
yes i want seprated values,so how to implement implode function
|
0

Maybe you can convert $t using implode($t). such as: $t= explode(",", implode($t));

Comments

-1
<?php
foreach ($test as $t){
    $t=(explode(",",$t->user_rights));
}
?>

1 Comment

$t->user_rights should be string and in $t variable you will get array .so according to that array you can use array value.

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.