SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2013
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Rename Array keys

    Hello, I've used arrays in the past, but I've used them in a different way. I used to use them to replace strings that I want, but now I want to print arrays. My problem is below.
    I would like to change the [0], [1], [2], [3], .etc to an actual name. So [0] being something like [id], [1] being [username], .etc.

    Is there a way to make this.
    px1.png

    Turn into this.
    px2.png

    Here is my PHP code.

    Code:
    <?php
    $mysqli = new mysqli("localhost", "username", "password", "database");
    if($mysqli->connect_errno) {
    	echo "Error";
    }
    
    $id = $_GET['id'];
    
    $result = $mysqli->query("SELECT * FROM users WHERE id = 'id'");
    
    $accounts = array();
    
    while(($row =  $result->fetch_array(MYSQLI_BOTH))) {
        $accounts[] = $row['id'];
        $accounts[] = $row['username'];
        $accounts[] = $row['country'];
        $accounts[] = $row['gender'];
    }
    ?>
    <pre>
    <?php
    print_r($accounts);
    ?>
    </pre>

  2. #2
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,
    I'm not sure what your means and I can't open your attachments.But I guess you want to build a multidimensional array,yes?
    If so,I think this is what you want,
    Code:
    while(($row =  $result->fetch_array(MYSQLI_BOTH))) {
        $accounts[$row['id']]['id'] = $row['id'];
        $accounts[$row['id']]['username'] = $row['username'];
        $accounts[$row['id']]['country'] = $row['country'];
        $accounts[$row['id']]['gender'] = $row['gender'];
    }
    then you can output them like:
    Code:
    foreach($accounts as $id => $row)
    {
       foreach($row as $name=>$val)
       {
            echo $name,'=',$val,'<br/>';
       }
    }
    Hope that helps.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2013
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blue_sky View Post
    Hi there,
    I'm not sure what your means and I can't open your attachments.But I guess you want to build a multidimensional array,yes?
    If so,I think this is what you want,
    Code:
    while(($row =  $result->fetch_array(MYSQLI_BOTH))) {
        $accounts[$row['id']]['id'] = $row['id'];
        $accounts[$row['id']]['username'] = $row['username'];
        $accounts[$row['id']]['country'] = $row['country'];
        $accounts[$row['id']]['gender'] = $row['gender'];
    }
    then you can output them like:
    Code:
    foreach($accounts as $id => $row)
    {
       foreach($row as $name=>$val)
       {
            echo $name,'=',$val,'<br/>';
       }
    }
    Hope that helps.
    Thanks that was exactly what I was looking for. And by the way. Is it possible to change the "Array" word in the beginning to something else? Or is that required?

  4. #4
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the "Array" that displayed in your pictures? If so,it can't be changed,because it was outputed by the method 'print_r()'.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    Hi therockers, welcome to the forums.

    I may be off base, but my take was you wanted to replace the default numeric keys with more meaningful associative keys, not create a multi-dimensional array.

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2013
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @blue_sky Thanks. That's all I wanted to know. If it can't be change, I'll just work with it. Thanks a lot.

    @Mittineague Thanks for the warm welcome and I know. I only needed a small part of the information that blue_sky gave me. I found out that all I needed was to do something like this.

    Code:
    while(($row =  $result->fetch_array(MYSQLI_BOTH))) {
        $accounts['id'] = $row['id'];
        $accounts['username'] = $row['username'];
        $accounts['country'] = $row['country'];
        $accounts['gender'] = $row['gender'];
    }
    And it prints out what I was anticipating. Thanks a ton lot to both of your replies.

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2013
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't know if I am able to doubt post or if it violates the Terms Of Service, but I can't edit my post.

    EDIT: How would I go about printing arrays that look something like this?

    https://graph.facebook.com/56381779049

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    Don't tell me you didn't read the FAQ http://www.sitepoint.com/forums/faq....q_editingposts

    Actually, that's not an array (backets []) but an object (braces {}) and most likely JSON

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2013
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Don't tell me you didn't read the FAQ http://www.sitepoint.com/forums/faq....q_editingposts

    Actually, that's not an array (backets []) but an object (braces {}) and most likely JSON
    Thanks for the help. Now I know the difference between which creates brackets and which creates curly braces. Thanks so much. And no, I didn't read the FAQ. As you can see I'm new so I didn't know if the FAQ had what I was asking.

  10. #10
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by therockers View Post
    @blue_sky Thanks. That's all I wanted to know. If it can't be change, I'll just work with it. Thanks a lot.

    @Mittineague Thanks for the warm welcome and I know. I only needed a small part of the information that blue_sky gave me. I found out that all I needed was to do something like this.

    Code:
    while(($row =  $result->fetch_array(MYSQLI_BOTH))) {
        $accounts['id'] = $row['id'];
        $accounts['username'] = $row['username'];
        $accounts['country'] = $row['country'];
        $accounts['gender'] = $row['gender'];
    }
    And it prints out what I was anticipating. Thanks a ton lot to both of your replies.
    That can be simplified to:
    PHP Code:
    $accounts=array();
    while((
    $row =  $result->fetch_array(MYSQLI_ASSOC))) {
        
    $accounts[] = $row;

    The first line sets up $accounts as any empty array, so that it it's being used in a foreach loop, the foreach loop won't complain about not being given an array to work with.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  11. #11
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,094
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The data from FaceBook is JSON.

    you can use json_encode and json_decode in php to work with that datatype.
    What I lack in acuracy I make up for in misteaks


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
  •