我有一个$result
数组,如:
Array ( [0] => Array ( [group_id] => 8 ) [1] => Array ( [group_id] => 9 ) [2] => Array ( [group_id] => 13 ) [3] => Array ( [group_id] => 14 ) [4] => Array ( [group_id] => 16 ) [5] => Array ( [group_id] => 21 ) )
我需要检查此9
中group_id
是if(in_array(9, $result)){
echo "It is there";
} else{
echo "It's not there";
}
的一部分:
URL jspUrl = new URL("...../Test.jsp");
URLConnection servletConnection = jspUrl.openConnection();
servletConnection.setDoOutput(true);
servletConnection.setDoInput(true);
servletConnection.setUseCaches(true);
servletConnection.setDefaultUseCaches(true);
servletConnection.setRequestProperty("Content-Type", "application/x-java-serialized-object");
OutputStream outputStream = servletConnection.getOutputStream();
ObjectOutputStream outputToServlet = new ObjectOutputStream(outputStream);
outputToServlet.writeObject(object);
outputToServlet.flush();
InputStream inputStream = servletConnection.getInputStream();
ObjectInputStream outputFromServlet = new ObjectInputStream(inputStream);
readObject = outputFromServlet.readObject();
outputFromServlet.close();
outputToServlet.close();
但这总是会回归:
它不存在。
答案 0 :(得分:3)
如果您的阵列结构保持不变,请使用
$a = Array ( [0] => Array ( [group_id] => 8 ) [1] => Array ( [group_id] => 9 ) [2] => Array ( [group_id] => 13 ) [3] => Array ( [group_id] => 14 ) [4] => Array ( [group_id] => 16 ) [5] => Array ( [group_id] => 21 ) ) ;
$result = array_column($a, 'group_id');
if(in_array(9, $result)){
echo "It is there";
} else{
echo "It's not there";
}
答案 1 :(得分:0)
您也可以使用array_map
代替array_column
。此解决方案不依赖于' group_id'键。
if(in_array(9, array_map('current', $array))){
echo "It is there";
} else{
echo "It's not there";
}