Hi guys'
I'm having trouble with this code

<?php 
    $link=sqlite_open('data/3001.sqlite ',0666,$sqliteerror);
    $comp=sqlite_query($link,"SELECT * FROM assets WHERE asset='chest'");
    while($ent=sqlite_fetch_array($comp)){
        $item=$ent['content'];
        $asset= "array(".$item.")";
        echo $asset;
    }
        $saved=array('food','knife');
        $left=array_diff($asset,$saved);
        echo $left;


    ?>

the echo $asset line displays "array(array(food,knife,scarf,torch,key)"
but I get an error
Warning: array_diff() [function.array-diff]: Argument #1 is not an array in C:\xampp\htdocs\abyss\test.php on line 10
when I run it, something simple, but i can't see it.
Any clues please?

Recommended Answers

All 9 Replies

Change:

$asset= "array(".$item.")";

To:

$asset = array($item);

Thanx for the reply, pixelsoul, but the output then becomes simply 'ArrayArray'.
To explain..
The query fetches a bunch of stuff into a list with checkboxes for players to choose to save.
That goes into '$saved' and another table.
What I then need to do is remove the chosen items from the original list, so that if the player returns
the items they have previously saved are not still available.
Using array_diff I can update the original table to leave only the not chosen items

dang.. I tried to edit before you saw it, I gave you the wrong code to change it to

$asset = array($item);

If that doesn't work, I will see if I can duplicate what you are doing on my server.

Thanks, I'll try that.
The other alternative is to restructure the db and have a separate table for each asset, but as i don't yet know how big this is gonna be,
I could end up with 100 tables of 1 column, instead of 1 table with 100rows. Broad as it's long I suppose.
Unfortunately, I have to go to work now, so it'll have to wait till later.

No prob, hit the thread back up again if you have more questions. I am probably misunderstanding what it is exactly you are trying to do. Though, I am sure there is an issue with the array there.

Also I noticed you say that it comes back as:

array(array(food,knife,scarf,torch,key)

But it seems to be missing the last ")"

array(array(food,knife,scarf,torch,key))

Oh, and another thing I just figured out. Since your $asset array is two dimensional, you would have to call it in array_diff like this for it to target the correct array:

$left=array_diff($asset[0],$saved);

Sorry, made a typo there, the 'echo $asset' line displays "array(food,knife,scarf,torch,key)"

Duh!, now I'm late for work, what a shame!

Thanks for your input, pixelsoul, but whlst at work, (not IT) I've made the decision to change the db.
Officionados will probably cry at the increased time factor, but a few milliseconds won't make much difference to me, and the resultant coding simplicity will be an advantage to me, (a simple uneducated hobyist!)
When the player makes their chioce, I can simply 'DELETE FROM assets' at the same time as inserting to the 'saved' table.
Thanks again.

No worries. I actually would do all of that in the db too most likely. Good luck on your project!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.