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
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

 

Link to comment
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

 

 

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!

?>

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.