Removing certain array element in a for-loop in PHP -


I have inserted some elements (name of the fruit) from mySQL into an array. Now, I would like to remove some objects from the array. If 'Array' exists, then I want to remove 'Apple' and 'Orange'. This is what I have tried, but I am getting an error message. Array example: Array ([1] => orange [2] = & gt; applet) foreach ($ word as $ = = $ v) {if ( Key ($ v) == "apple") {without set ($ word [$ k]); } Otherwise (key ($ v) == "orange") {without set ($ word [$ k]); }}> Gt; & Gt; & Gt; WARNING: The key (1) is expected to be an array of parameter 1, string is given / the same error has been repeated 4 times

I have referenced this link here: if any I can tell what I did wrong

explanation:

$ fr is the actual array of all your fruit and your $ rm is another array that contains a list of items to be removed from your $ fr array.

Use a foreach cycle through the array $ rm and see whether the element exists on the $ fr array Is, if found, unset () .

Code ...
  & lt ;? Php $ fr = array ('apple', 'orange', 'pineapple'); // & lt; - Your real array $ rm = array ('apple', 'orange'); // & lt; --- Elements to be removed foreach ($ vm $ v) {if_array ($ v, $ fr)) {unset ($ fr [array_search ($ v, $ fr)]); }} Print_r ($ fr);   

Output:

  Array ([2] => Pineapple)  array_diff ()   
  print_r (array_diff ($ fr, $ rm)) using code>  

;



Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -