SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Tried everything - simply can't echo array values, just keep getting 'Array' instead.

    Hello there,

    I've spent the best part of a week trying to get this to work, and scoured the Internet for a solution, and got nowhere, so I'm now desperately hoping someone here can save the day!

    What I've done is use mysqli_multi_query to SELECT three records from two different MySQL tables. Then I've combined the two outputs $output1 and $output2 into one array called $final, by using array_merge.

    Everything works fine up to that point.

    The last step is to display the contents of the $final array, but nothing I've tried works, every variation of code I've tried just displays the word Array instead of the actual value. Yet I know the values are sitting in the array, through checking the output of the print_r function (as below).

    The only suggestion I found on Google that looked like it might work, was to assign the value of each array element to a string variable first, and then echo that ... but all that did was assign the value Array to the string ... again!

    I'm completely stumped by this, so any ideas on how to output the actual values would be vastly appreciated! The relevent section of code is below (everything up to this point works fine, just this final step is the unsolvable problem for me).

    Code PHP:
    <?php
     
       $final=array_merge($output1,$output2);
       $total_rows = $count_rows_1 + $count_rows_2;
       $i = 0;
       while ($i <= $total_rows) {
          print_r($final[$i]);
          echo '<li>',$final[$i],'</li>';
          $i++;}
     
    ?>

    From what I've read, the blank line in the output below is due to a gremlin with PHP's mysqli_multi_query, that generates an unwanted extra record.

    /* OUTPUT

    Array ( [0] => blue dalek [1] => (reviewer) )
    Array
    Array ( [0] => bluemoon [1] => (reviewer) )
    Array
    Array ( [0] => bubbleboy [1] => (reviewer) )
    Array

    Array ( [0] => beatles IV [1] => (album) )
    Array
    Array ( [0] => blue aeroplanes [1] => (album) )
    Array
    Array ( [0] => bluemoon [1] => (album) )
    Array

    */


    Not sure if it's relevent, but the background to this is that it's part of an Autocomplete Search function, which involves jQuery (that's why all the records begin with 'b', to test Autocomplete). Normally, Autocomplete can only query one table, so what I've done is use mySQLi's multi-query function to query two tables, then merged the two results into one array, then applied Autocomplete to that ... it's working fine, except I don't see the values in my search results, just this darned Array! Maybe I broke jQuery somehow ...?

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What you have is a multi-dimensional array. $final is an array, and every element of that is an array itself. I've changed the echo line to give you some output. Not sure exactly what you need in the end but maybe this will help you understand what to do.


    PHP Code:
    $final=array_merge($output1,$output2);
    $total_rows $count_rows_1 $count_rows_2;
    $i 0;
    while (
    $i <= $total_rows) {
       
    print_r($final[$i]);
       echo 
    "<li> {$final$i ][ ]} {$final$i ][ ]}</li>";
       
    $i++;

    Side note: I personally would use foreach() instead of the while and count but it should work either way.
    - Robert

  3. #3
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks QMonkey! --- I'll try that idea of yours out, and post back here tomorrow.

    Though after a LOT of mucking about (it's now 5:30am here - been up all darned night!) - I finally found a fix ...

    Code PHP:
       $final=array_merge($output1,$output2);
       foreach ($final as $key => $values):
       echo "<li>",$values[0],$values[1],"</li>";
       endforeach;

    ... that produces the output I was after:


    blue dalek (reviewer)
    bluemoon (reviewer)
    bubbleboy (reviewer)

    beatles IV (album)
    blue aeroplanes (album)
    bluemoon (album)


    EDIT: Just reading through your post Robert (with my by-now rather bleary eyes) ... yes, I tried the multi-dimensional suggestion, but that just gave me Array Array. But I see you suggest the foreach method too, and that's what I stumbled on in a post here at StackOverflow, and that's done the trick - at long last.

    Many thanks for your advice Robert.
    Last edited by kiwiplayer; Oct 4, 2012 at 09:38. Reason: update


Tags for this Thread

Bookmarks

Posting Permissions

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