1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| <?php
class GraphicsCard { public $brand; public $model;
public function __construct($brand, $model) { $this->brand = $brand; $this->model = $model; } }
$graphics_card = [ [ 'brand'=>'华硕', 'model'=>'GTX980(Ti)', 'status'=>1, ], [ 'brand'=>'技嘉', 'model'=>'GTX970', 'status'=>0, ], [ 'brand'=>'微星', 'model'=>'GTX960', 'status'=>1, ], [ 'brand'=>'七彩虹', 'model'=>'GTX950', 'status'=>0, ], ];
$graphics_card2 = []; foreach ($graphics_card as $value) { $graphics_card2[] = new GraphicsCard($value['brand'], $value['model']); }
$demoA1 = $graphics_card; $demoA2 = $graphics_card2; foreach ($demoA1 as $key => $value) { $demoA1[$key]['model'] = '1080'; $demoA1[$key]['brand'] = '按摩店'; } foreach ($demoA2 as $key => $item) { $demoA2[$key]->brand = '按摩店'; $demoA2[$key]->model = '1080'; } var_dump($demoA1); var_dump($demoA2);
$demoB1 = $graphics_card; $demoB2 = $graphics_card2; $demoB1 = array_map(function ($value) { $value['brand'] = '按摩店'; $value['model'] = '1080'; return $value; }, $demoB1); array_map(function ($value) { $value->brand = '按摩店'; $value->model = '1080'; }, $demoB2); var_dump($demoB1); var_dump($demoB2);
$demoC1 = $graphics_card; $demoC2 = $graphics_card2; array_map(function ($key, $value) use (&$demoC1) { $demoC1[$key]['brand'] = '按摩店'; $demoC1[$key]['model'] = '1080'; }, array_keys($demoC1), $demoC1); array_map(function ($key, $value) use ($demoC2) { $demoC2[$key]->brand = '按摩店'; $demoC2[$key]->model = '1080'; }, array_keys($demoC2), $demoC2); var_dump($demoC1); var_dump($demoC2);
$brands = []; $models = []; foreach ($graphics_card as $item) { $brands[] = $item['brand']; }
$models = array_map(function ($value) { return $value['model']; }, $graphics_card);
var_dump($brands); var_dump($models);
var_dump(array_column($graphics_card, 'brand')); var_dump(array_column($graphics_card, 'model'));
$items = [ 'item1'=>'Hello', 'item2'=>'World', 'item3'=>'Luis', 'item4'=>'Edware', 'item5'=>'Ann', 'item6'=>'Eason', ];
foreach($items as $key => $value){ if($key === 'item1'){ $items[$key] = 'World'; }elseif($key === 'item3'){ $items[$key] = 'Edware'; } } var_dump($items);
$items_to_modify = ['item1'=>'Hello','item3'=>'Luis']; array_map(function($key,$value) use(&$items) { $items[$key] = $value; },array_keys($items_to_modify),$items_to_modify); var_dump($items);
|