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.

Turn into this.

Here is my PHP 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>

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,

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:

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?

Is the “Array” that displayed in your pictures? If so,it can’t be changed,because it was outputed by the method ‘print_r()’.

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.

@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.

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.

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

Don’t tell me you didn’t read the FAQ :eek: http://www.sitepoint.com/forums/faq.php?faq=etiquette#faq_editingposts :wink:

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.

That can be simplified to:


$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.

The data from FaceBook is JSON.

you can use json_encode and json_decode in php to work with that datatype.