Grow your CSS skills. Land your dream job.

Trying to get array keys

  • # July 8, 2013 at 6:06 pm

    ###Code to get data:

    $query = “SELECT * FROM `read`”;
    $result = $DB->query($query);
    while ($row = $result->fetch_assoc()) {
    var_dump($row);
    }

    ###Output:

    array (size=3)
    ‘order’ => string ‘1’ (length=1)
    ‘title’ => string ‘Google Maps (2013 Update)’ (length=25)
    ‘url’ => string ‘http://fontsinuse.com/uses/4135/google-maps-2013-update’ (length=55)
    array (size=3)
    ‘order’ => string ‘2’ (length=1)
    ‘title’ => string ‘Type Battle 41! Five characters, no curves.’ (length=43)
    ‘url’ => string ‘http://typophile.com/battle41′ (length=29)
    array (size=3)
    ‘order’ => string ‘3’ (length=1)
    ‘title’ => string ‘Desk = Work; Not Desk = Not Work’ (length=32)
    ‘url’ => string ‘http://chriscoyier.net/2013/05/21/desk-work-not-desk-not-work/’ (length=62)

    ###Desired Output

    array (size=3)
    => 0
    ‘order’ => string ‘1’ (length=1)
    ‘title’ => string ‘Google Maps (2013 Update)’ (length=25)
    ‘url’ => string ‘http://fontsinuse.com/uses/4135/google-maps-2013-update’ (length=55)
    array (size=3)
    => 1
    ‘order’ => string ‘2’ (length=1)
    ‘title’ => string ‘Type Battle 41! Five characters, no curves.’ (length=43)
    ‘url’ => string ‘http://typophile.com/battle41′ (length=29)
    array (size=3)
    => 2
    ‘order’ => string ‘3’ (length=1)
    ‘title’ => string ‘Desk = Work; Not Desk = Not Work’ (length=32)
    ‘url’ => string ‘http://chriscoyier.net/2013/05/21/desk-work-not-desk-not-work/’ (length=62)

    How can I do this?

    # July 8, 2013 at 6:28 pm

    If each array is $row, then can’t you just do whatever you need to do to each of them in your while loop?

    If you do need it as a separate array, I think you can do something like this:

    $newArray = array();

    while ( $row = $result->fetch_assoc() ) {
    array_push($newArray, $row);
    }

    But probably come up with a better name than `newArray` ;)

    Note: not PHP expert.

    # July 8, 2013 at 6:33 pm

    @TheDoc That’s exactly what I’m trying to figure out.

    What’s happening with your code is that it outputs a new array in a continuous loop.

    ###Result

    array (size=1)
    0 =>
    array (size=3)
    ‘order’ => string ‘1’ (length=1)
    ‘title’ => string ‘Google Maps (2013 Update)’ (length=25)
    ‘url’ => string ‘http://fontsinuse.com/uses/4135/google-maps-2013-update’ (length=55)
    array (size=2)
    0 =>
    array (size=3)
    ‘order’ => string ‘1’ (length=1)
    ‘title’ => string ‘Google Maps (2013 Update)’ (length=25)
    ‘url’ => string ‘http://fontsinuse.com/uses/4135/google-maps-2013-update’ (length=55)
    1 =>
    array (size=3)
    ‘order’ => string ‘2’ (length=1)
    ‘title’ => string ‘Type Battle 41! Five characters, no curves.’ (length=43)
    ‘url’ => string ‘http://typophile.com/battle41′ (length=29)
    array (size=3)
    0 =>
    array (size=3)
    ‘order’ => string ‘1’ (length=1)
    ‘title’ => string ‘Google Maps (2013 Update)’ (length=25)
    ‘url’ => string ‘http://fontsinuse.com/uses/4135/google-maps-2013-update’ (length=55)
    1 =>
    array (size=3)
    ‘order’ => string ‘2’ (length=1)
    ‘title’ => string ‘Type Battle 41! Five characters, no curves.’ (length=43)
    ‘url’ => string ‘http://typophile.com/battle41′ (length=29)
    2 =>
    array (size=3)
    ‘order’ => string ‘3’ (length=1)
    ‘title’ => string ‘Desk = Work; Not Desk = Not Work’ (length=32)
    ‘url’ => string ‘http://chriscoyier.net/2013/05/21/desk-work-not-desk-not-work/’ (length=62)

    # July 8, 2013 at 6:35 pm

    That’s only because you have the var_dump inside of the `while`. Put `var_dump($newArray);` after the loop.

    # July 8, 2013 at 6:39 pm

    @TheDoc You’re so smart. Thanks a lot for the quick help.

    # July 8, 2013 at 6:50 pm

    Yahoo, I answered one of your PHP questions!

    # July 8, 2013 at 7:52 pm

    Are you still trying to figure out the random order in which items are inserted into the database?

    Why don’t you convert `pubDate` into UTC timestamp by:

    $pubDate = $items;
    $utcTime – date(strtotime($pubDate));

    Then store that into the database via Integer data type, and make it unique. This way you can sort by timestamp.

    Tested and working: https://gist.github.com/alenabdula/5913146

    # July 8, 2013 at 7:59 pm

    Wait… that is article publish date not readability.com xml add date? Right?

    Nope, I just checked timestamp “Leaked report…” title shows Monday 8th July 2013 3:12:13 PM UTC. You might need to pass time zone for date function above to get the correct time zone.

    # July 8, 2013 at 8:02 pm

    @AlenAbdula Correct. So unfortunately that wouldn’t work. However, @traq helped me out with that in my previous thread which is thankfully solved now.

    # July 8, 2013 at 8:14 pm

    Nevermind, I see you’re trying to do something else. I misinterpreted the question. Carry on… :)

    # July 8, 2013 at 8:33 pm

    @AlenAbdula thanks for trying to help out.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".