在数组值内部搜索以获取php中的主键

时间:2019-07-07 05:11:41

标签: php arrays

我具有以下数组格式:

$array = [
'fruits' => ['apple','orange','banana'],
'vegetables' => ['spinach','broccoli','lettuce']
];

我希望能够在水果和蔬菜的值内进行搜索,并使其返回键(水果或蔬菜)。

我用过这个:

$result = array_search('orange', $array);

但是$ result不返回任何内容。当它想返回“水果”时。我该怎么做?

2 个答案:

答案 0 :(得分:1)

可以使用foreach

$array = [
'fruits' => ['apple','orange','banana'],
'vegetables' => ['spinach','broccoli','lettuce']
];

foreach ($array as $key => $value) {
    if (in_array('orange', $value)) {
        echo $key;
    }
}

答案 1 :(得分:1)

array_search返回搜索值的索引(如果存在)。所以我认为您可以进行foreach搜索,如果array_search不为空,则获取密钥。 像这样的东西:


class CartoonBloc extends BlocBase<CartoonStreams, CartoonEvents> {
  final NoticeRepository repository;
  DatabaseHelper helper = DatabaseHelper();
  int _page = 0;
  int _limit = 10;

  List _cartoonInner = List();
  List _cartoonAll = List();
  bool _carregando = false;
  bool _fnish = true;
  bool _first = true;

  CartoonBloc(this.repository) {

  }

  @override
  void initView() {
    _load(false);
  }

  @override
  void eventReceiver(event) {
    if (event is LoadCartoon) {
      _load(false);
    }

    if (event is LoadMoreCartoon) {
      if (_fnish == true) {
        streams.progress.set(false);
        _fnish = false;
        _first = false;
        _load(true);
      }
    }

  }

  _load(bool isMore) async {
    _carregando = true;

    if (isMore) {
      _page += _limit;
    } else {
      _page = 0;
    }

    streams.errorConection.set(false);

    String toDay = new DateTime.now().toUtc().toIso8601String();

    var allCton = await helper.getCartoonList();

    if (_first == true && allCton.length > 0) {
      _showCartoon(allCton, false);
    }

    streams.progress.set(true);

    var cartoonSche = await repository.getAllCartoonSche(
        toDay, _page.toString(), _limit.toString());

    if (cartoonSche.length > 0) {
        await helper.deleteCartoon(_limit);
      }

    if (cartoonSche.length > 0) {

      for (var i = 0; i < cartoonSche.length; i++) {
         if (!_cartoonInner.contains(cartoonSche[i])) {
              await helper.insertCartoon(cartoonSche[i]);
            }

      }

      var allNwsB = await helper.getCartoonList();

      if(_first == true){
            _showCartoon(allNwsB, false);
          } else{
            _showCartoon(allNwsB, true);
          }

    } else {
      streams.progress.set(false);
    }

    _fnish = true;
    _first = false;

  }

  _showCartoon(List cartoon, bool isMore) {
    streams.progress.set(false);

    if (isMore) {
      _cartoonInner.addAll(cartoon);
    } else {
      _cartoonInner = cartoon;
    }

    streams.cartoons.set(_cartoonInner);

    _carregando = false;
  }

  _showImplError(onError) {
    print(onError);
    if (onError is FetchDataException) {
      print("codigo: ${onError.code()}");
    }
    streams.errorConection.set(true);
    streams.progress.set(false);
    _carregando = false;
  }

  void cleanList() {
    _cartoonInner = List();
    streams.cartoons.set(_cartoonInner);
  }
}