MyBB Community Forums

Full Version: How to rename Array Keys?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I queried the database, selecting the column 'filename' where uid = X, then I output the results using the $db->fetch_array in a while loop, so it outputs all rows.

The problem is:

1. The arrays automatically use the key 'filename' because that is what the column name is.
2. Because the keys 'filename' aren't unique, it automatically makes it a Multi Dimensional array.

Look below at the output of print_r();:

Array ( 

[0] => Array ( 
	[filename] => uploads/testing1.txt 
	) 
[1] => Array ( 
	[filename] => uploads/random.txt 
	) 
[2] => Array ( 
	[filename] => uploads/mytest.docx
	) 
[3] => Array ( 
	[filename] => uploads/testing2.php
	) 
[4] => Array ( 
	[filename] => uploads/atest.txt
	) 
)

Is there a way to make [filename] to be [0], [1], [2], etc? That is what is causing a Second Dimension Array. Array keys need to be unique, and the key [filename] is automatically generated from the db->fetch_array() code.

I need it to look like this:


Array ( 

[0] => uploads/testing1.txt

[1] => uploads/random.txt
 
[2] => uploads/mytest.docx

[3] => uploads/testing2.php

[4] => uploads/atest.txt

)
I am not sure, but I think you mean this:

<?php
$old_array = array(
    array("filename","uploads/testing1.txt"), 
    array("filename","uploads/random.txt"),
    array("filename","uploads/mytest.docx"),
    array("filename","uploads/testing2.php"),
    array("filename","uploads/atest.txt"),);
print_r($old_array); 
$new_array = array();
foreach($old_array as $i) { 
    $new_array[] = $i[1];
}
echo "<br /><br />";
print_r($new_array);
?>

If so, try it.
There must be a function that handles this in 1 statement, but the ones I found didn't work for me.
There is a function, but I'm not sure what. Here is something I found out though:



$numbergenerator = rand(0,5); //TESTING. This will be changed to the proper way of counting from 0 to X, for the Array keys.
$array[$numbergenerator] = $array['filename'];

That reassigns the columns name 'filename' to $numbergenerator, which I now need to count from 0 to the number of rows that it found.

Then, that will give me unique Array keys from [0] =>, [1] =>, [2] =>, etc.

I tried it using the rand() function, and it works. It gave me random Array key numbers Smile

Now I just need to not make them random Array Key Numbers, but make them each from 0, 1, 2, etc...
But the above code I suggested does the job, doesn't it?
Not quite, the number of array keys will all be 'filename', as I am querying the database. So rand wouldn't work, I used it for testing purposes.

So I will need to use num_rows to count how many rows there are, then...I'm not sure how to output from 0, until the num_rows count.

Lets say num_rows = 5, I'm not sure how to count from 0 until 5, and output every number inbetween.

EDIT: I'll try using a for loop to count from 0 to num_rows. Yes, it works! Thanks for your help. I successfully renamed the array keys from [filename] to [0], [1], [2], etc...

I never knew Programming was so fun...until I started.
I do not understand, this is wat I get after running the test:

old array
Array ( [0] => Array ( [0] => filename [1] => uploads/testing1.txt ) [1] => Array ( [0] => filename [1] => uploads/random.txt ) [2] => Array ( [0] => filename [1] => uploads/mytest.docx ) [3] => Array ( [0] => filename [1] => uploads/testing2.php ) [4] => Array ( [0] => filename [1] => uploads/atest.txt ) )

new array
Array ( [0] => uploads/testing1.txt [1] => uploads/random.txt [2] => uploads/mytest.docx [3] => uploads/testing2.php [4] => uploads/atest.txt ) 

The new array is exactly what you asked for in your opening post.

But when you simply want to create a new array, then this will do?


$array = array("uploads/testing1.txt","uploads/random.txt","uploads/mytest.docx","uploads/testing2.php","uploads/atest.txt")
Yes, that's correct. What code did you use to achieve that? I used a for loop to count up until num_rows.
The code in post #2

$new_array = array();
foreach($old_array as $i) { 
    $new_array[] = $i[1];
}
Ok. But 'filename' was the [key] for all the arrays, it wasn't part of the array itself. In your code, you put 'filename' as part of the array. Eg.
[0] => filename

When mine was like:

[fielname] => test.txt

Nevertheless, I solved it. I changed mine from a for loop to be in the while loop, which queries the database.
This is the outcome of the loop, that is it, doesn't it?

new array
Array ( [0] => uploads/testing1.txt [1] => uploads/random.txt [2] => uploads/mytest.docx [3] => uploads/testing2.php [4] => uploads/atest.txt ) 
Pages: 1 2