0

I query a database to obtain an array of results.

$usersArray = $db->getAllUsers();  // db-Query

If I print out the array's var_dump, its content is structured in form of other arrays:

array(9) { [0]=> **array**(1) { ["column"]=> string(20) "..." } [1]=> **array**(1) { ["column"] (remaining 8 are the same).

Now I need these values (which are correct, so far) to be casted as strings, so that:

array(9) { [0]=> **string**(1) { ["column"]=> string(20) "..." } [1]=> **string**(1) { ["column"] ....

There are several answers to this here and elsewhere, such as

-array_map: here I can actually cast the content as string, but- it prints "Array" instead the value. It tried then getting the content via

$users = array_map('strval',implode( $usersArray));
$users = array_map('strval', print_r($usersArray));

Neither of those worked.

Is there a method through which I could cast the content as string and get the content ? Or should I rewrite the query to format the result as strings ?

9
  • 1
    Are you trying to cast array to string? serialize($usersArray); Commented Jan 27, 2015 at 15:39
  • Why can't you just get the values like this? $usersArray[0]['column'] ? Why are you trying to cast as string? Commented Jan 27, 2015 at 15:40
  • And what do you mean print_r() and implode() doesn't work? Show us how you've tried using these functions. Commented Jan 27, 2015 at 15:40
  • $users = implode($usersArray); (didn't work). $users = print_r($usersArray); didn't work either.. Commented Jan 27, 2015 at 15:42
  • 1
    @Romana You using them both wrong. Implode requires two parameters. And you can't assign print_r to a variable. Commented Jan 27, 2015 at 15:45

1 Answer 1

1

You have a wrong understanding of types or at least this:

array(9) { [0]=> **string**(1) { ["column"]=> string(20) "..." } [1]=> **string**(1) { ["column"] ....

doesn't make any sense. You believe you want elements to be of type string but yet contain array data which really doesn't work. What you actually want is a different array structure but you are heading in the wrong direction for that.

You basically have two options:

  1. Modify the getAllUsers() method in a way that returns your data in a structure you actually need.

  2. Modify the data after you have received it. Obviously there's no builtin function convert_data_to_how_i_want_them() - so a basic understanding of arrays is required. Basically you create a new array and copy those values you need to the position you need them at.

Something like this should do the trick in this case:

$out = array();
foreach($in as => $value) {
    $out[] = $value['column'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Good explanation- you also pointed correctly out the options. I implemented the second version. Thanks!

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.