我遇到一个奇怪的问题for for循环我的意思是我有关联数组与make:
$makes = array(0 => '9FF',
1 => 'AC',
2 => 'AMG',
3 => 'Alfa Romeo',
4 => 'Alpina',
5 => 'Ariel',
6 => 'Ascari',
7 => 'Aston Martin',
8 => 'Audi',
9 => 'B Engineering',
10 => 'BAC',
11 => 'BMW',
12 => 'Beck',
13 => 'Bentley',
14 => 'Bertone',
15 => 'Bizzarrini',
16 => 'Brabus',
17 => 'Bristol',
18 => 'Brooke',
19 => 'Bugatti',
20 => 'Buick',
21 => 'Cadillac',
22 => 'Callaway',
23 => 'Caparo',
24 => 'Caterham',
25 => 'Chevrolet',
26 => 'Chrysler',
27 => 'Citroen',
28 => 'Cizeta Moroder',
29 => 'Connaught',
30 => 'Covini',
31 => 'Dauer',
32 => 'David Brown',
33 => 'Dax',
34 => 'De Lorean',
35 => 'De Tomaso',
36 => 'Dodge',
37 => 'Dome',
38 => 'ES Motorsports',
39 => 'Elfin',
40 => 'Ferrari',
41 => 'Fiat',
42 => 'Fioravanti',
43 => 'Fisker',
44 => 'Ford',
45 => 'GM',
46 => 'GTA',
47 => 'Gemballa',
48 => 'Ghia',
49 => 'Ginetta',
50 => 'Giugiaro',
51 => 'Grinnall',
52 => 'Gumpert',
53 => 'Hennessey',
54 => 'Heuliez',
55 => 'Honda',
56 => 'Invicta',
57 => 'Isdera',
58 => 'Iso',
59 => 'Ital Design',
60 => 'Jaguar',
61 => 'Jeep',
62 => 'Jensen',
63 => 'Joss',
64 => 'KTM',
65 => 'Keio University',
66 => 'Koenig',
67 => 'Koenigsegg',
68 => 'Lamborghini',
69 => 'Lancia',
70 => 'Land Rover',
71 => 'LeBlanc',
72 => 'Leading Edge',
73 => 'Lexus',
74 => 'Light Car Company',
75 => 'Lingenfelter',
76 => 'Lister',
77 => 'Lotec',
78 => 'Lotus',
79 => 'MB Roadcars',
80 => 'MG',
81 => 'Marcos',
82 => 'Maserati',
83 => 'Maybach',
84 => 'Mazda',
85 => 'McLaren',
86 => 'Melling',
87 => 'Mercedes',
88 => 'Mitsubishi',
89 => 'Monteverdi',
90 => 'Morgan',
91 => 'Mosler',
92 => 'Nissan',
93 => 'Noble',
94 => 'Oldsmobile',
95 => 'Pagani',
96 => 'Palmer',
97 => 'Panoz',
98 => 'Panther',
99 => 'Peugeot',
100 => 'Pininfarina',
101 => 'Plymouth',
102 => 'Pontiac',
103 => 'Porsche',
104 => 'Prodrive',
105 => 'Radical',
106 => 'Renault',
107 => 'Rimac',
108 => 'Rolls Royce',
109 => 'Roush',
110 => 'Ruf',
111 => 'SCG',
112 => 'SSC',
113 => 'Saab',
114 => 'Saleen',
115 => 'Sbarro',
116 => 'Schuppan',
117 => 'Sin',
118 => 'Spectre',
119 => 'Spyker',
120 => 'Stealth',
121 => 'Strathcarron',
122 => 'Studiotorino',
123 => 'Subaru',
124 => 'Superformance',
125 => 'TVR',
126 => 'Techart',
127 => 'Tesla',
128 => 'Tiger',
129 => 'Toroidion',
130 => 'Toyota',
131 => 'Tramontana',
132 => 'Trident',
133 => 'UVA',
134 => 'Ultima',
135 => 'Vauxhall',
136 => 'Vector',
137 => 'Vemac',
138 => 'Venturi',
139 => 'Veritas',
140 => 'Volkswagen',
141 => 'Volvo',
142 => 'Westfield',
143 => 'Wiesmann',
144 => 'Wolfrace',
145 => 'Yamaha',
146 => 'Zagato',
147 => 'Zenvo');
以及其他一些代码:
$pics = '';
$logos = "http://www.supercarworld.com/images/logos/";
$gif = ".gif";
for ($x = 0; $x < count($makes); $x++) {
$pics = array($logos.$makes[$x].$gif);
print_r($pics[$x]);
$car = new Car();
$car->setName('http://www.supercarworld.com/images/fullpics/595.jpg');
// em entity manager
$em = $this->getDoctrine()->getManager();
$em->persist($car);
$em->flush();
$car = $this->getDoctrine()
->getRepository('GrupaProjektBundle:Car')
->findOneBy(array('id' => $car->getId(), 'name' => $car->getName()));
return $this->render('GrupaProjektBundle:Sc:supercars.html.twig', array('car' => $car,
'pics' => $pics[$x],
));
}
当我想获得[$ x]时,行为很奇怪,它只显示零元素,即9FF,但当我从$得到[0]或[1]时,它会起作用 为什么? 我想让它迭代,我错过了一些增量?
答案 0 :(得分:1)
$ pics不是一个数组,或者你指定它的方式,它是一个只有一个元素和一个数组的数组。将在for循环的每次迭代中被覆盖。
你的意思是这样做,还是你想要
$pics = array();
然后在你的for循环中:
$pics[$x] = $logos.$makes[$x].$gif;
我在这里猜测,因为它不能立即清楚你想要实现的目标。但我当然可以说你在for循环的每次迭代中都覆盖了$ pics。