Jump to content

Array help


owner

Recommended Posts

I have an array with indexes in it, but I cannot seem to keep the indexes from changing to the default 0, 1, 2, 3.

 

So here is what I am doing:

 

while($x = mysql_fetch_array($q)){

//Do some stuff
$string = 'some random string generated above';
$inserted_id = mysql_insert_id();

if($increment > 0){
$end = array($inserted_id => $string);

$array= array_merge((array)$array, (array)$end);

}else{
	$array= array($inserted_id => $string);
}

//This is the end of the foreach
$increment++;
}
}

 

Right now, I will get back an array that looks like:

Array ( [0] => boo [1] => apple [2] => peapod )

 

Instead of that, I want whatever those indexes are from the mySQL database to be the index of the keys here like

 

Array ( [845] => boo [846] => apple [847] => peapod )

 

How do you setup an array like this?

Thanks in advance!

owner

Link to comment
https://forums.phpfreaks.com/topic/190058-array-help/
Share on other sites

Why dont you try something like this:

$array[$inserted_id]=$string;//that way you dont need logic and merge

and at the same time you loop through output $inserted_id. I think it not there($inserted_id) so as the array gets built it gets default indexing keys.

 

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/190058-array-help/#findComment-1002729
Share on other sites

You are apparently doing queries within a foreach loop - vary bad idea. You can get all your values in a single query. Show the foreach and queries used and I can provide more details on that.

 

But, as to the specific problem you are asking about, I think you are overcomplicating the process by appending arrays. Try this:

 

while($x = mysql_fetch_array($q))
{
    //Do some stuff
    $string = 'some random string generated above';
    $inserted_id = mysql_insert_id();

    $array[$inserted_id] = $string;

    //This is the end of the foreach
    $increment++;
}
}

 

Edit: teamatomic beat me to it, but I call foul since I was unearthing other problems not shown. :)

Link to comment
https://forums.phpfreaks.com/topic/190058-array-help/#findComment-1002730
Share on other sites

And I'm sitting 15 feet from a nice roaring fireplace drinking black dog, playing with my mini110 and flirting with a tablefull of hot young things and thinking it was a good day to ski.

 

But then, everyday is a good day to ski.

 

I win!

 

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/190058-array-help/#findComment-1002739
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.