如何在ArrayObject类中获取项目索引

时间:2019-04-14 08:04:10

标签: php arrayobject

我正在通过继承PHP的ArrayObject类来实现一个类。但是我没有找到如何在迭代时获取数组的索引。我想同时显示键和值

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
public class UserControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    public void findAll() throws Exception {
        MvcResult result = mockMvc
                .perform(get("/api/user").contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk()).andReturn();

        MockHttpServletResponse response = result.getResponse();
        RestResponse restResponse = mapper.readValue(response.getContentAsString(), RestResponse.class);
        Assert.assertEquals(restResponse.getHttpStatus().name(), HttpStatus.OK.name() );
    }
}

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
public class ProductControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    public void findAll() throws Exception {
        MvcResult result = mockMvc
                .perform(get("/api/product").contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk()).andReturn();

        MockHttpServletResponse response = result.getResponse();
        RestResponse restResponse = mapper.readValue(response.getContentAsString(), RestResponse.class);
        Assert.assertEquals(restResponse.getHttpStatus().name(), HttpStatus.OK.name() );
    }

}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我修改了以下行,并且工作正常

'<td>'.$iterator->key().'</td><td>'.$iterator->current().'</td>'

有关更多信息,see here