dcsimg
www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] Convert array element to string

  1. #1
    Join Date
    Jun 2013
    Posts
    20

    resolved [RESOLVED] Convert array element to string

    hi guys.

    my problem is that i want to convert an element of an array to string, but i can't figure out how.
    i have tried this

    $a =(string)$news_array[0].['NAME'];

    but php returned "Array to string conversion in........"

    which is the correct method???

  2. #2
    Join Date
    Jun 2013
    Posts
    20
    i solved the problem.
    it was

    $a=(string)$news_array[0]['NAME']


    sory guys!

  3. #3
    Join Date
    Sep 2013
    Posts
    221
    well..you can also use:
    $string_version = implode(',', $original_array);

    This is exactly what the PHP implode() function is for.
    grt to knw that you have solved it.
    strad solutionswww.stradsolutions.com

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,306
    Quote Originally Posted by priyankagound View Post
    well..you can also use:
    $string_version = implode(',', $original_array);

    This is exactly what the PHP implode() function is for.
    grt to knw that you have solved it.
    No, the OP wanted to convert a single, scalar array element to a string, not the entire array. (In fact, I'm guessing that casting it to string is redundant, but it certainly doesn't hurt anything to do so.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Join Date
    Aug 2013
    Location
    CA
    Posts
    15
    Well you can use this :

    function my_implode($separator,$array){
    $temp = '';


    foreach($array as $key=>$item){
    $temp .= $item;
    if($key != sizeof($array)-1){
    $temp .= $separator ;
    }
    }//end of the foreach loop

    return $temp;
    }//end of the function

    $array = array("One", "Two", "Three","Four");


    $str = my_implode('-',$array);
    echo $str;

    Hope it helps you

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,306
    Quote Originally Posted by jackspparow View Post
    ...
    Hope it helps you
    Why would you make a user-defined function that replicates a standard built-in PHP function, making your source code longer and your program slower?

    Besides, imploding has nothing to do with the OP's question.

    *sigh*
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  7. #7
    Join Date
    Oct 2013
    Posts
    1
    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'.

  8. #8
    Join Date
    Jun 2013
    Posts
    20
    i already solved the problem.
    it is

    $a=(string)$news_array[0]['NAME']

    thx anyway!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles