Jump to content

Recommended Posts

Is it possible to create a PHP array that contains only keys?

	array
	1
	3
	5
	6
	7
	

 

I have a query that returns row ID's that I want to put into an array to do some work.  There is no real concept of a "value" - I just need to store a list of the database table rows that i want and that's it!

 

Edited by SaranacLake
Link to comment
https://forums.phpfreaks.com/topic/312158-array-with-keys-only/
Share on other sites

Not getting anywhere today... ;-(

I have a query in my script using prepared statements.

The query returns a listing of required questions.

	if (mysqli_stmt_num_rows($stmt10)>0){
    mysqli_stmt_bind_result($stmt10, $requiredItem);
    mysqli_stmt_fetch($stmt10);
    while (mysqli_stmt_fetch($stmt10)){
        $requiredItemsArray[$requiredItem] = 'xxx';
    }
	

 

When I copy and paste my query into phpMyAdmin, it gives met {1,3,5,6,7}

When I run the above PHP code, my var_dump($requiredItemsArray), then I get {3=>'xxx', 5=>'xxx', 6=>'xxx', 7=>'xxx'}

Where is 1?

 

1.) Ideally, I want an array that just lists {1,3,5,6,7} like my query.

 

2.) I would like to figure out why I am missing "1"?!

 

Thanks.

 

No, you cannot have an array of just keys, because the very nature of a key is that it provides the location to a value.
You also cannot have an array with just values, because no keys would mean you wouldn't know where a value was in the array.

 

53 minutes ago, SaranacLake said:

Where is 1?

It was returned by that first useless call to mysqli_stmt_fetch.

7 hours ago, requinix said:

No, you cannot have an array of just keys, because the very nature of a key is that it provides the location to a value.
You also cannot have an array with just values, because no keys would mean you wouldn't know where a value was in the array.

 

It was returned by that first useless call to mysqli_stmt_fetch.

Insulting me and my code does nothing to help me understand why the above code is not working.

I have used similar code all along, so I'm not sure what is wrong - although the answer is probably right in front of me.

	mysqli_stmt_fetch($stmt10);
	

should return all of the applicable rows from my query - which works fine in phpMyAdmin.

 

And

	while (mysqli_stmt_fetch($stmt10)){         
	    $requiredItemsArray[$requiredItem] = 'xxx';   
	 }
	

should populate my array with (1, 3, 5, 6, 7) as the keys and 'xxx' as junk values since all I need are the keys.

 

So what am I doing wrong?

 

 

6 minutes ago, SaranacLake said:

Insulting me and my code does nothing to help me understand why the above code is not working.

I was insulting you?

 

Quote

I have used similar code all along, so I'm not sure what is wrong - although the answer is probably right in front of me.

It is.

 

Quote

 



	mysqli_stmt_fetch($stmt10);
	

should return all of the applicable rows from my query - which works fine in phpMyAdmin.

No. What that does is fetch the next row in the resultset, which then updates $requiredItem.

 

Quote

And



	while (mysqli_stmt_fetch($stmt10)){         
	    $requiredItemsArray[$requiredItem] = 'xxx';   
	 }
	

should populate my array with (1, 3, 5, 6, 7) as the keys and 'xxx' as junk values since all I need are the keys.

It's populating the array with the remaining rows from the resultset.

 

Okay, let's say you don't know what mysqli_stmt_fetch does.

In your while loop, you call the function: if it returns good then you put $requiredItem into the array, and if it returns bad then you stop. Clearly mysqli_stmt_fetch does something that moves you row by row through the resultset, right?
Now look at the code before the loop. You call mysqli_stmt_fetch. We now know that moves through a single row. However, where your while loop did something with $requiredItem after every call to mysqli_stmt_fetch, before the loop you do not.

Think about that.

I just grabbed another block of code that did a similar thing, pasted it into BBEdit, stripped out all of the specifics, and looked at my working code structure.

At the very end, I see that my working code has...

	while(mysqli_stmt_fetch($stmt2)){
	

 

Then I noticed that I have an EXTRA...

]code]

mysqli_stmt_fetch($stmt10)

[/code]

in my broken code.

 

NOW I see what Iw as doing wrong!

(I haven't been outside in like 3 1/2 days - so that is the real problem.

We had an ice storm, but I need to try and go to the store or something and clear my brain...

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.