PHP多维数组-如果存在来自第一个键的值,则从第二个键返回值

时间:2019-03-18 10:40:14

标签: php arrays

所以即使对于PHP或数组也不是陌生的人,但从来不必处理多维数组及其操作。

我将PHP输出到服务器API,需要从( dst_mac )键中提取所有mac地址值,但仅在类别( catname < / strong>),每个元素的键值都为新兴-p2p

数组的格式如下(为简洁起见,删除了中间键和值)

    [1] => stdClass Object
    (
        [_id] => 5c8ed5b2b2302604a9b9c78a
        [dst_mac] => 78:8a:20:47:60:1d
        [srcipGeo] => 
        [dstipGeo] => stdClass Object
            (
            )

        [usgipGeo] => stdClass Object
            (
            )
        [catname] => emerging-p2p
    )

任何帮助我都感激不尽,我知道我什么时候进入我的生活!

6 个答案:

答案 0 :(得分:1)

package com.cavad.firebaseproject;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;


public class Home extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    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.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) 
 findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    Fragment category = new CategoryFragment();
    this.setDefaultFragment(category);
 }

 private void setDefaultFragment(Fragment defaultFragment)
 {
    this.replaceFragment(defaultFragment);
 }

 public void replaceFragment(Fragment destFragment)
 {
    FragmentManager fragmentManager = this.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = 
 fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.category, destFragment);
    fragmentTransaction.commit();
 }

@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.home, 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) {
        return true;
    }

    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;
  }
 }

答案 1 :(得分:1)

在您的示例中,该数组是带有std类的数组。您可以使用空功能。

//checks if the first key
if (!empty($array[1]->_id)) {
  echo $array[1]->dst_mac;
  // or do what you want.
}

此示例仅适用于一个数组。使用循环可以动态完成此操作。

编辑:我的回答基于您的问题。没意识到您必须在获取Mac地址之前将猫名检查为“ emerging-p2p”吗?

// loop through the array
foreach ($array as $item) {
  // checks for the catname
  if ($item->catname === 'emerging-p2p') {
    // do what you want if the cat name is correct
    echo $item->dst_mac;
  }
}

这是您想要的吗?

答案 2 :(得分:0)

如果您只需要基于mac address的{​​{1}},则可以使用array_column

catname

工作demo

答案 3 :(得分:0)

如果只有一个对象的猫名为“ emerging-p2p”:

foreach ($your_list_of_objects as $obj) {
    if ($obj->catname == 'emerging-p2p') {
        return $obj->dst_mac;
    }
}

如果有很多:

$result = [];
foreach ($your_list_of_objects as $obj) {
    if ($obj->catname == 'emerging-p2p') {
        $result[]= $obj->dst_mac;
    }
}
return $result;

答案 4 :(得分:0)

用于循环。

for($i =0; $i<=count($arr); $i++){ 
   if(arr[$i]['catname']=='emerging-p2p'){
        echo arr[$i]['dst_mac'];
    }
}

答案 5 :(得分:0)

要获取catname为emerging-p2p的所有Mac

//Assuming $arr has array of objects
$result_array = array_filter($arr, function($obj){
    if ($obj->catname == 'emerging-p2p') {
        return true;
    }
    return false;
});

foreach ($result_array as $value) {
    echo $value->dst_mac;
}