I have three arrays in PHP. Example structure of arrays is as given below. I want to check if id is present in first then value with that it should not be repeated in any other arrays. fo eg in arrays below record with id 200 is present in first array then it is again in third array so I want to remove  c0 from third array there will be 9 such arrays. What will be dynamic way for it.  Array (     [a0] =&gt; Array         (              [id] =&gt; 100              [Country] =&gt; some country         )      [a1] =&gt; Array         (             [id] =&gt; 200             [Country] =&gt; some country          )  )    Array (     [b0] =&gt; Array         (             [id] =&gt;300             [Country] =&gt; some country          )      [b1] =&gt; Array         (             [id] =&gt; 100             [Country] =&gt; some country             [city] =&gt; some city          )  )    Array (     [c0] =&gt; Array         (             [id] =&gt;200             [Country] =&gt; some country             [state] =&gt; some state          )      [c1] =&gt; Array         (             [id] =&gt; 400             [Country] =&gt; some country          )  )
3020
How can I remove duplicate values from a multi-dimensional array in PHP?  Example array:  Array (     [0] =&gt; Array     (         [0] =&gt; abc         [1] =&gt; def     )      [1] =&gt; Array     (         [0] =&gt; ghi         [1] =&gt; jkl     )      [2] =&gt; Array     (         [0] =&gt; mno         [1] =&gt; pql     )      [3] =&gt; Array     (         [0] =&gt; abc         [1] =&gt; def     )      [4] =&gt; Array     (         [0] =&gt; ghi         [1] =&gt; jkl     )      [5] =&gt; Array     (         [0] =&gt; mno         [1] =&gt; pql     )  )
33664
I have an array full of random content item ids. I need to run a mysql query (id in the array goes in the WHERE clause), using each ID that's in the array, in the order that they appear in the said array. How would I do this?   This will be an UPDATE query, for each individual ID in the array.
14806
How do I remove empty elements from an array in JavaScript?   Is there a straightforward way, or do I need to loop through it and remove them manually?
107813
I'm looking at the following code snippet:  my @ret = &lt;someMethod&gt; return (undef) if( $DB_ERROR ); return (undef) unless ($#ret &gt;= 0);   Does $# just give you a count of elements in a array?
9942
Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element?  I thought that setting it to null would do it, but apparently it does not work.
19426
If I have a string "1 2 3 2 1" - or an array [1,2,3,2,1] - how can I select the unique values, i.e.  "1 2 3 2 1" produces "1 2 3"    or  [1,2,3,2,1] produces [1,2,3]   Similar to uniq but uniq seems to work on whole lines, not patterns within a line...
119674
I want 3 post with one loop iteration, so I'm using the_post() to achieve this: (html removed)  &lt;?php while ( have_posts() ) : the_post();      the_title();     the_post();     the_title();     the_post();     the_title();  endwhile; ?&gt;   The problem starts, if number of posts is not divisible by 3. For example, if I have 5 posts, than this loop will still try to display 6 of them, generating empty html. How can I work it out? Or maybe I'm trying to achieve this functionality the wrong way?  @edit: Here is HTML I wanna output to clarify my question:  &lt;div class="row"&gt; &lt;div class="span3"&gt;     &lt;h2&gt;Title&lt;/h2&gt;     &lt;p&gt;Content&lt;/p&gt; &lt;/div&gt; &lt;div class="span3"&gt;     &lt;h2&gt;Title&lt;/h2&gt;     &lt;p&gt;Content&lt;/p&gt; &lt;/div&gt; &lt;div class="span3"&gt;     &lt;h2&gt;Title&lt;/h2&gt;     &lt;p&gt;Content&lt;/p&gt; &lt;/div&gt;  &lt;/div&gt;   &lt;div class="row"&gt; &lt;div class="span3"&gt;     &lt;h2&gt;Title&lt;/h2&gt;     &lt;p&gt;Content&lt;/p&gt; &lt;/div&gt; &lt;div class="span3"&gt;     &lt;h2&gt;Title&lt;/h2&gt;     &lt;p&gt;Content&lt;/p&gt; &lt;/div&gt; &lt;div class="span3"&gt;     &lt;h2&gt;Title&lt;/h2&gt;     &lt;p&gt;Content&lt;/p&gt; &lt;/div&gt; &lt;/div&gt;   So I want to surround every 3 posts with .row.
37749
I am working on a project involving cards from the Magic: The Gathering game. To be of any use, I must load the basic information about each card into my database, but some of the fields may contain arrays of text, that is, they may contain multiple values .  For example, a card can be classified to be both "green" and "blue". Or it can have several different creature types specified, and so on.  My first thought was to simply store the values as a string, and then search using the LIKE keyword. In combination with the NOT keyword, it should be possible to search for any desired combination of words, and also exclude certain words.   However, LIKE is going to be (relatively) slow as it can't use an index and has to rely on text pattern matching, and probably has other drawbacks as well. With normalization I could improve speed by assigning an integer key to each keyword, and then create a table that connects each card with the relevant keys (see  for a clearer description).  But with normalized tables I have to deals with joins (severely increased code complexity, not only in the SQL part), transactions (to update all relevant tables or rollback everything), and possibly decreased performance due to joins (see ).  AFAIK there are roughly 20000-25000 different magic cards in existence (and more being released every year). With one row for each card, is it worth it overall to normalize the data, or is the benefit not worth the costs? Why/Why not? And how do you decide this question in general?  EDIT: I am currently using a MySQL database, but I would also appreciate answers that apply to any of the other non-commercial/open source databases out there. Who knows - I might switch to a different database at some point.  Answers should not provide a technical solution only, but explain why it would/might be the right solution in terms of best/good enough performance for least amount of effort.
33592
I have a group of strings in Javascript and I need to write a function that detects if another specific string belongs to this group or not.  What is the fastest way to achieve this? Is it alright to put the group of values into an array, and then write a function that searches through the array?  I think if I keep the values sorted and do a binary search, it should work fast enough. Or is there some other smart way of doing this, which can work faster?
98955
Suppose I have this code: var myArray = new Object(); myArray[&quot;firstname&quot;] = &quot;Bob&quot;; myArray[&quot;lastname&quot;] = &quot;Smith&quot;; myArray[&quot;age&quot;] = 25;  Now if I wanted to remove &quot;lastname&quot;?....is there some equivalent of myArray[&quot;lastname&quot;].remove()? (I need the element gone because the number of elements is important and I want to keep things clean.)
73249
Is there any method for creating a dynamic array in C#?
10572
Given an array of n Objects, let's say it is an array of strings, and it has the following values:  foo[0] = "a"; foo[1] = "cc"; foo[2] = "a"; foo[3] = "dd";   What do I have to do to delete/remove all the strings/objects equal to "a" in the array?
7271
Is it possible in PHP to do something like this? How would you go about writing a function? Here is an example. The order is the most important thing.  $customer['address'] = '123 fake st'; $customer['name'] = 'Tim'; $customer['dob'] = '12/08/1986'; $customer['dontSortMe'] = 'this value doesnt need to be sorted';   And I'd like to do something like   $properOrderedArray = sortArrayByArray($customer, array('name', 'dob', 'address'));   Because at the end I use a foreach() and they're not in the right order (because I append the values to a string which needs to be in the correct order and I don't know in advance all of the array keys/values).  I've looked through PHP's internal array functions but it seems you can only sort alphabetically or numerically.
26165
I have a class that contains a dynamically allocated array, say  class A {     int* myArray;     A()     {         myArray = 0;     }     A(int size)     {         myArray = new int[size];     }     ~A()     {         // Note that as per MikeB's helpful style critique, no need to check against 0.         delete [] myArray;     } }   But now I want to create a dynamically allocated array of these classes. Here's my current code:  A* arrayOfAs = new A[5]; for (int i = 0; i &lt; 5; ++i) {     arrayOfAs[i] = A(3); }   But this blows up terribly. Because the new A object created (with the A(3) call) gets destructed when the for loop iteration finishes, and this means that the internal myArray of that A instance gets delete []-ed.  So I think my syntax must be terribly wrong? I guess there are a few fixes that seem like overkill, which I'm hoping to avoid:   Creating a copy constructor for A. Using vector&lt;int&gt; and vector&lt;A&gt; so I don't have to worry about all this. Instead of having arrayOfAs be an array of A objects, have it be an array of A* pointers.   I would think this is just some beginners thing where there's a syntax that actually works when attempting to dynamically allocate an array of things that have internal dynamic allocation.  (Also, style critiques appreciated, since it's been a while since I did C++.)  Update for future viewers: All of the answers below are really helpful. Martin's is accepted because of the example code and the useful "rule of 4," but I really suggest reading them all. Some are good, succinct statements of what's wrong, and some point out correctly how and why vectors are a good way to go.
2911
I have an array of Foo objects. How do I remove the second element of the array?  I need something similar to RemoveAt() but for a regular array.
106137
If we have 2 tables  **user** +----+------+------+-----------+ | id | user | pass | gender_id | +----+------+------+-----------+ | 1  | lisa | good |     11    | | 2  | jack | pass |     10    | | 3  | paul | 5555 |     10    | | 4  | rich | none |     10    | +----+------+------+-----------+  **gender** +----+--------+ | id | gender | +----+--------+ | 11 | female | | 10 | male   | +----+--------+   If we join  them like so  SELECT user.id, user.user, user.pass, user.gender_id, gender.gender FROM `user` JOIN `gender` on user.gender_id   The result of query  array(4) {   [0]=&gt;   array(4) {     ["id"]=&gt;     string(3) "2"     ["type"]=&gt;     string(6) "jack"     ["pass"]=&gt;     string(11) "pass"     ["gender"]=&gt;     string(1) "male"     }   [1]=&gt;   array(4) {     ["id"]=&gt;     string(3) "3"     ["type"]=&gt;     string(6) "paul"     ["pass"]=&gt;     string(11) "5555"     ["gender"]=&gt;     string(1) "male"     }   [2]=&gt;   array(4) {     ["id"]=&gt;     string(3) "4"     ["type"]=&gt;     string(6) "rich"     ["pass"]=&gt;     string(11) "none"     ["gender"]=&gt;     string(1) "male"     }   [3]=&gt;   array(4) {     ["id"]=&gt;     string(3) "1"     ["type"]=&gt;     string(6) "lisa"     ["pass"]=&gt;     string(11) "good"     ["gender"]=&gt;     string(1) "female"     }   The order has now changed even though we did not specify this.(user lisa is last) If we change lisa gender_id to 11, and run the same query, the result will be  array(4) {   [0]=&gt;   array(4) {     ["id"]=&gt;     string(3) "1"     ["type"]=&gt;     string(6) "lisa"     ["pass"]=&gt;     string(11) "good"     ["gender"]=&gt;     string(1) "male"     }   [1]=&gt;   array(4) {     ["id"]=&gt;     string(3) "2"     ["type"]=&gt;     string(6) "jack"     ["pass"]=&gt;     string(11) "pass"     ["gender"]=&gt;     string(1) "male"     }   [2]=&gt;   array(4) {     ["id"]=&gt;     string(3) "3"     ["type"]=&gt;     string(6) "paul"     ["pass"]=&gt;     string(11) "5555"     ["gender"]=&gt;     string(1) "male"     }   [3]=&gt;   array(4) {     ["id"]=&gt;     string(3) "4"     ["type"]=&gt;     string(6) "rich"     ["pass"]=&gt;     string(11) "none"     ["gender"]=&gt;     string(1) "male"     }   This result is how the rows are positioned in the table(user lisa first), which is represented correctly in the results as we did not specify any order.  Why is the first result set being ordered when we are not specifying this. It does not seem correct, I want the results to be pulled exactly how they are positioned in the table.  Thanks
44995
I had this question earlier and it was concluded it was a bug in 5.2.5.  Well, it's still broken in 5.2.6, at least for me:  Please let me know if it is broken or works for you:  $obj = new stdClass(); $obj-&gt;{"foo"} = "bar"; $obj-&gt;{"0"} = "zero"; $arr = (array)$obj;  //foo -- bar //0 --    {error: undefined index} foreach ($arr as $key=&gt;$value){     echo "$key -- " . $arr[$key] . "\n"; }   Any ways to "fix" the array after it has been cast from a stdClass?