Asked  7 Months ago    Answers:  5   Viewed   37 times

I have a PHP array and want to convert it to a string.

I know I can use join or implode, but in my case array has only one item. Why do I have to use combine values in an array with only one item?

This array is the output of my PHP function which returns an array:

Array(18 => 'Something');

How do I convert this to a string?

 Answers

33

Is there any other way to convert that array into string ?

You don't want to convert the array to a string, you want to get the value of the array's sole element, if I read it correctly.

<?php
  $foo = array( 18 => 'Something' );
  $value = array_shift( $foo );
  echo $value; // 'Something'.

?>

Using array_shift you don't have to worry about the index.

EDIT: Mind you, array_shift is not the only function that will return a single value. array_pop( ), current( ), end( ), reset( ), they will all return that one single element. All of the posted solutions work. Using array shift though, you can be sure that you'll only ever get the first value of the array, even when there are multiple.

Wednesday, March 31, 2021
 
Novalirium
answered 7 Months ago
94

To add to Rikesh's answer:

<?php
$aryMain = array(array('hello','bye'), array('',''),array('','')); 
$aryMain = array_filter(array_map('array_filter', $aryMain));
print_r($aryMain);

?>

Sticking his code into another array_filter will get rid of the entire arrays themselves.

Array
(
    [0] => Array
        (
            [0] => hello
            [1] => bye
        )

)

Compared to:

$aryMain = array_map('array_filter', $aryMain);

Array
(
    [0] => Array
        (
            [0] => hello
            [1] => bye
        )

    [1] => Array
        (
        )

    [2] => Array
        (
        )

)
Wednesday, March 31, 2021
 
drowneath
answered 7 Months ago
74

No, it's not possible to do directly. You might try this "clever" trick instead:

public function addPages(array $pages)
{
    foreach($pages as $page) addPage($page);
}

public function addPage(My_Page $page)
{
    //
}

But I 'm not sure if it's worth all the trouble. It would be a good approach if addPage is useful on its own.

Wednesday, March 31, 2021
 
Pradip
answered 7 Months ago
27

I would turn it into CSV form, like so:

$string_version = implode(',', $original_array)

You can turn it back by doing:

$destination_array = explode(',', $string_version)
Wednesday, March 31, 2021
 
tdous
answered 7 Months ago
38

EDIT

The solution you want is this:

$user = current( User::findByEmail( $_SESSION['email'] ) );

Note that using current() with functions always returns the first element, because the array was never assigned to a variable, and does not have a point. This is NOT the case if the function returns an array by reference.

Wednesday, March 31, 2021
 
Dail
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :