Jump to content

How to convert this array to string


1bigbear

Recommended Posts

I am having problems converting this array to a string

using print_r($val[1]); I get:

Array
(
    [0] => Array
        (
            [name] => John
        )

    [1] => Array
        (
            [name] => Dan
        )

    [2] => Array
        (
            [name] => Jim
        )
)

I have tried several methods but they don't work

Link to comment
https://forums.phpfreaks.com/topic/178767-how-to-convert-this-array-to-string/
Share on other sites

The array $val has this data

Array
(
    [0] => Array
        (
            [name] => John
        )

    [1] => Array
        (
            [name] => Dan
        )

    [2] => Array
        (
            [name] => Jim
        )
)

And I want to use the content of $val[1][1] that is Dan, but the script that I use needs a string not a array

And I don't know how to convert the array $val[1][1] to string

 

The array $val has this data

Array
(
    [0] => Array
        (
            [name] => John
        )

    [1] => Array
        (
            [name] => Dan
        )

    [2] => Array
        (
            [name] => Jim
        )
)

And I want to use the content of $val[1][1] that is Dan, but the script that I use needs a string not a array

And I don't know how to convert the array $val[1][1] to string

 

 

Well, I think you have a mistake !

 

so,

<?
$users = array('John', 'Dan', 'someone');
echo $users[1][1]; // will give you the litter a of the user d*A*n!
echo $users[1]; // will give you dan!

// So you don't need extra index [1] for $val !
// just use $val[1], then it will give you the string of the index 1 !

// what you actually doing is adding an index to the index ! so it's starting to pick letter as index of the index name!

?>

<?php

$array[0]['name']='John';
$array[1]['name']='Dan';
$array[2]['name']='Jim';

$string='';
foreach($array as $subarray){
    $string.=$subarray['name'].',';
}
$string=rtrim($string,',');
echo $string;
?>

OR this way...

<?php
$array[0]['name']='John';
$array[1]['name']='Dan';
$array[2]['name']='Jim';

for($i=0;$i<count($array);$i++){
    $names[]=$array[$i]['name'];
}
$string2=implode(',',$names);
echo $string2;
?>

 

im sure there are many ways

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.