Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 2.32 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Re-indexing an array by matching value
  2. $array_fruit[] = array('fruit' => 'apple', 'color' => 'red');  
  3. $array_fruit[] = array('fruit' => 'banana', 'color' => 'yellow');  
  4. $array_fruit[] = array('fruit' => 'kiwi', 'color' => 'green');  
  5. $array_fruit[] = array('fruit' => 'orange', 'color' => 'orange');
  6. $array_fruit[] = array('fruit' => 'strawberry', 'color' => 'red');
  7. $array_fruit[] = array('fruit' => 'lemon', 'color' => 'yellow');  
  8.  
  9. $i = 0;  
  10.  
  11. $array_inStock = array();  
  12.  
  13. foreach($array_fruit as $fruit)  
  14. {  
  15.     if($fruit['fruit'] == 'apple')  
  16.     {  
  17.         $array_inStock['3'] = array('fruit' => $fruit['fruit'], 'color' => $fruit['color']);  
  18.     }  
  19.     else  
  20.     {  
  21.         $array_inStock[$i] = array('fruit' => $fruit['fruit'], 'color' => $fruit['color']);  
  22.     }  
  23.     $i++;  
  24. }  
  25.  
  26. asort($array_inStock);
  27.  
  28. print_r($array_inStock);
  29.        
  30. $array_stock =array();$i=0;
  31. foreach($array_fruit as $k=>$v)
  32. {
  33. if($v['fruit'] =='apple')
  34.     {
  35.         $array_stock[3]=$v;
  36.         if($k>3)
  37.             array_push($array_stock,$array_fruit[3]);
  38.         continue;
  39.     }
  40. if(array_key_exists($i,$array_stock))
  41. {
  42.     array_push($array_stock,$v);
  43. }
  44. else
  45. {
  46.     $array_stock[$i]=$v;
  47.     $i++;
  48. }
  49. }
  50.  
  51. ksort($array_stock);
  52. echo "<pre>";
  53. print_r($array_stock);
  54. echo "</pre>";
  55.        
  56. $array_fruit[] = array('fruit' => 'apple', 'color' => 'red');  
  57. $array_fruit[] = array('fruit' => 'banana', 'color' => 'yellow');
  58. etc.
  59.        
  60. $array_fruit[0] = array('fruit' => 'apple', 'color' => 'red');  
  61. $array_fruit[1] = array('fruit' => 'banana', 'color' => 'yellow');
  62. etc.
  63.        
  64. $array_fruit = array();  
  65. $array_fruit[] = array('fruit' => 'apple', 'color' => 'red');  
  66. $array_fruit[] = array('fruit' => 'banana', 'color' => 'yellow');  
  67. $array_fruit[] = array('fruit' => 'kiwi', 'color' => 'green');  
  68. $array_fruit[] = array('fruit' => 'orange', 'color' => 'orange');  
  69.  
  70. print_r($array_fruit);
  71.  
  72. $i = 0;  
  73.  
  74. $array_inStock = array();  
  75.  
  76. foreach($array_fruit as $fruit)  
  77. {  
  78.     if($fruit['fruit'] == 'apple')  
  79.     {  
  80.         $array_inStock['3'] = $fruit;  
  81.     }  
  82.     else  
  83.     {  
  84.         $array_inStock[$i] = $fruit;
  85.         $i++;  
  86.     }      
  87. }  
  88.  
  89. asort($array_inStock);
  90. print_r($array_inStock);
  91.        
  92. foreach($array_fruit as $fruit) {  
  93.     if($fruit['fruit'] == 'apple') {  
  94.         $array_inStock[3] = $fruit;
  95.     } else if($i != 3) {
  96.         $array_inStock[$i] = $fruit;
  97.         $i++;
  98.     }
  99. }