我正在使用循环进行一些数组操作。
我想将数组键值与给定名称进行比较。
但我无法得到确切的输出。
这是我的阵列:
Array
(
[0] => Array
(
[label] =>
[value] =>
)
[1] => Array
(
[label] => 3M
[value] => 76
)
[2] => Array
(
[label] => Test
[value] => 4
)
[3] => Array
(
[label] => Test1
[value] => 5
)
[4] => Array
(
[label] => Test2
[value] => 6
)
)
这是我的变化,我需要比较:$test_name = "Test2";
以下代码我尝试过:
$details // getting array in this varriable
if($details['label'] == $test_name)
{
return $test_name;
}
else
{
return "NotFound";
}
但每次它返回NotFound。
没有得到确切的问题。
答案 0 :(得分:4)
@Manthan Dave尝试使用array_column和in_array(),如下所示:
<?php
if(in_array($test_name, array_column($details, "label"))){
return $test_name;
}
else
{
return "NotFound";
}
答案 1 :(得分:3)
$details
是一个多维数组,但你试图像一个简单的数组一样访问它
你需要循环它:
foreach ($details as $item) {
if($item['label'] == $test_name)
{
return $test_name;
}
else
{
return "NotFound";
}
}
我希望你的数组永远不能包含标签NotFound
...:)
答案 2 :(得分:2)
你在数组里面使用下面的数组,
if($details[4]['label'] == $test_name)
{
return $test_name;
}
else
{
return "NotFound";
}
虽然foreach循环应该可行,但如果没有尝试,
for($i=0; $i<count($details); $i++){
if($details[$i]['label'] == $test_name)
{
return $test_name;
}
else
{
return "NotFound";
}
}
答案 3 :(得分:2)
只使用in_array
和array_column
而不使用foreach
循环
if (in_array($test_name,array_column($details, 'label')))
{
return $test_name;
}
else
{
return "NotFound";
}
答案 4 :(得分:2)
你只需要检查下面的if条件,因为第一次遇到它会返回“notfound”,然后就不会执行。
$result = 'NotFound';
if (in_array($test_name,array_column($details, 'label')))
{
$result = $test_name;
}
return $result;
或
public class Main_Page extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FirebaseAuth mFirebaseAuth;
FirebaseAuth.AuthStateListener mAuthStateListener;
GoogleApiClient mGoogleApiClient;
//Recycler view
private RecyclerView mTodoList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main__page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Firebase Auth
mFirebaseAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null) {
finish();
startActivity(new Intent(Main_Page.this, SignIn.class));
}
}
};
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
startActivity(new Intent(Main_Page.this, Add_To_Do.class));
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Recycler View
mTodoList = (RecyclerView) findViewById(R.id.todo_list);
mTodoList.setHasFixedSize(true);
mTodoList.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
/*If i write FirebaseRecyclerAdapter it is not showing any suggestions
and if i write it manually i am getting an redline and it tells me to make a
class or a function*/
FirebaseRecy
}
//Recycler View
public static class ToDoViewHolder extends RecyclerView.ViewHolder{
View mView;
public ToDoViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setLabel(String label){
TextView post_label = (TextView)
mView.findViewById(R.id.post_label);
post_label.setText(label);
}
public void setNote(String note){
TextView post_note = (TextView) mView.findViewById(R.id.post_note);
post_note.setText(note);
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main__page, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
mFirebaseAuth.signOut();
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 5 :(得分:2)
像这样遍历你的数组,
array_walk($array, function($v) use($test_name){echo $v['label'] == $test_name ? $test_name : "NotFound";});