Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    112
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Outputting individual of values to a new line in a text file

    Array
    (
    [0] => stdClass Object
    (
    [symbol] => AAN
    )

    [1] => stdClass Object
    (
    [symbol] => AAP
    )



    So I have a file that runs this...

    file_put_contents('stocksymbols.txt', print_r($stocksymbols, TRUE));

    but it prints out the array in the format of the above. Is there a way to just do

    AAPL
    AAP
    B
    BAC

    etc?

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Untested but should do the job:

    PHP Code:
    foreach ($stocksymbols as $Key => $Value)
       {
       
    file_put_contents('stocksymbols.txt'$ValueFILE_APPEND);
       } 
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #3
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    112
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Now I have...

    PHP Code:
    $stocksymbols $wpdb->get_results"select meta_value as symbol from ii_postmeta
    where meta_key = 'symbol' and meta_value not like '% %' and meta_value  not like ''
    group by meta_value
    order by meta_value asc"
    ); 


    foreach (
    $stocksymbols as $k=>$v)
    {
    file_put_contents('../stocks/stocksymbols.txt'$vFILE_APPEND);

    but the file that results is blank, any ideas?

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    What happens when you do this after the query?:

    var_dump($stocksymbols);
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #5
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    112
    Thanks
    4
    Thanked 0 Times in 0 Posts
    catching the tail end but the output looks like this


    [1151]=>
    object(stdClass)#1436 (1) {
    ["symbol"]=>
    string(3) "ZLC"
    }
    [1152]=>
    object(stdClass)#1437 (1) {
    ["symbol"]=>
    string(3) "ZMH"
    }
    [1153]=>
    object(stdClass)#1438 (1) {
    ["symbol"]=>
    string(4) "ZNGA"
    }
    }

  • #6
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    112
    Thanks
    4
    Thanked 0 Times in 0 Posts
    seems like something fundamental is missing or wrong. I tried this as well...

    PHP Code:
    $stocksymbols $wpdb->get_results"select meta_value as symbol from ii_postmeta
    where meta_key = 'symbol' and meta_value not like '% %' and meta_value  not like ''  and meta_value not like 'Multi'
    group by meta_value
    order by meta_value asc"
    ); 

    file_put_contents('stocksymbols.txt'print_r($stocksymbolsTRUE));

    $fp=fopen("test.txt","w+");

    foreach (
    $stocksymbols as $k=>$v){
    fwrite($fp,$v);

    the text.txt that gets written inside the foreach is blank, but stocksymbols.txt that gets written (the one outside of the foreach) outputs as


    Array
    (
    [0] => stdClass Object
    (
    [symbol] => AAN
    )

    [1] => stdClass Object
    (
    [symbol] => AAP
    )

    [2] => stdClass Object
    (
    [symbol] => AAPL
    )


    as before

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Ok try using this:
    PHP Code:
    foreach ($stocksymbols as $Key => $Value)
       {
       
    file_put_contents('stocksymbols.txt'$Value['symbol'], FILE_APPEND);
       } 
    Note the symbol part on $Value which I forgot about last time. It might still not work but just try it.

    Edit: As per FouLu's post use ->symbol instead of ['symbol'] (sorry I don't do oop so didn't think of that!)
    Last edited by tangoforce; 12-28-2012 at 10:05 PM.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    stdclass cannot be output directly to a string.
    In the file_put_contents, use $v->symbol instead of just $v.
    Using a print_r on that example you have here should still write the output as it would with any print_r(x, true) call. That would indicate to me that you may be looking at the wrong file (you have used 2 different filepaths here). It is also possible that your function call includes an empty slot for the array, and since the file_put_contents you have here doesn't use append it will overwrite it every time. You'd be better off using var_dump instead of print_r since you could tell if its empty or null or what it is.

    Needless to say, use the $v->symbol instead of $v in the put or write calls.

  • Users who have thanked Fou-Lu for this post:

    tangoforce (12-28-2012)

  • #9
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    112
    Thanks
    4
    Thanked 0 Times in 0 Posts
    So now I have

    PHP Code:
    foreach ($stocksymbols as $Key => symbol)
       {
       
    file_put_contents('stocksymbols.txt'$Value['symbol'], FILE_APPEND);
       } 


    But I'm getting an internal server error and the script stops completely.

    In the error log I'm getting

    [02-Jan-2013 10:25:26] PHP Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /home5/instiuc5/jobs/stockcoverage.php on line 24

    Line 24 is the line with the foreach statement.

    I google that nekudotayim error and it has to do with missing braces it looks like?
    But I don't believe I have any missing braces.

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Thats because you're doing two things wrong:

    1) Leaving it days at a time before coming back
    2) Ignoring things that people say.

    Scroll up. I gave you a reply, then Fou-Lu replied correcting me. I went back and edited my post to tell you to ignore my reply and go with Fou-Lu's. Despite that, you've come back days later and still gone with my old and wrong advice, completely ignored my edit and completely ignored Fou-Lu.

    If you're going to ignore people like that then what do you expect?
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #11
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    112
    Thanks
    4
    Thanked 0 Times in 0 Posts
    the above problem looks like it was due to the "=", i changed it to -> and it was able to run, however the resulting file is still blank I do either


    foreach ($stocksymbols as $Key -> symbol)
    {
    file_put_contents('stocksymbols.txt', symbol, FILE_APPEND);
    }

    or

    foreach ($stocksymbols as $Key -> symbol)
    {
    file_put_contents('stocksymbols.txt', $Value['symbol'], FILE_APPEND);
    }

  • #12
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    112
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post

    If you're going to ignore people like that then what do you expect?

    You said to use F-lou, if you look at the code I pasted, you can see that I have replaced your ['symbol'] with ->symbol

    So I'm not sure why you are upset.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    No paamayim nekudotayim is for scope resolution. You have a class named symbol, so you cannot use it in the foreach as you have. That would require the variable $symbol (or a public static property in the symbol class), and the $Value['symbol'] would be replaced by $symbol->symbol.

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Re read what Fou said.

    Look at the code I once gave you. Now read what Fou said. This is what he meant:

    PHP Code:
    foreach ($stocksymbols as $Key => $Value)
       {
       
    file_put_contents('stocksymbols.txt'$Value->symbolFILE_APPEND);
       } 
    Note the -> is inside the loop. Fou was quite clear about stating this must be changed in the file_put_contents() line of code. I also put in my edit (you know, the red box in my reply that says edit) that you must change ['symbol'] to ->symbol. Why did you then decide to change => to -> on the foreach line instead?
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #15
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    112
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Okay, there was some miscommunication in my reading of fou-lu's original correction. I had misread.

    the script is working now with this:

    $fp=fopen("test.txt","w+");

    foreach ($stocksymbols as $k=>$v){
    fwrite($fp,$v->symbol);
    }

    Apologize for the miscommunication, I changed the "=" to "-" due to my own error in trying to diagnose the problem.

    apologies


  •  

    Posting Permissions

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