检查多维数组中是否存在特定值

时间:2017-12-16 12:49:47

标签: php multidimensional-array

我有像这样的多维数组

Array
(
    [jack] => Array
        (
            [0] => 12
            [1] => 45
            [2] => 78
            [3] => 19
            [4] => 94
            [5] => 668
        )

    [john] => Array
        (
            [0] => 641
            [1] => 741
            [2] => 683
            [3] => 603
        )

)

如何搜索 641 ,如果找到则返回其父键。 即。如果找到 641 ,我想获得 john

这就是我试过的

    if(in_array_r("641" , $array)){
    // if found return john!
}

OR

if(in_array_r("12" , $array)){
    // if found return jack!
}

我正在寻找一种不会包含foreach loop的方法。复杂程度较低的解决方案会很好。

4 个答案:

答案 0 :(得分:1)

试试这个:

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Backend\Model\Session\Quote $sessionQuote,
    \Magento\Sales\Model\AdminOrder\Create $orderCreate,
    PriceCurrencyInterface $priceCurrency,
    \Magento\Wishlist\Model\WishlistFactory $wishlistFactory,
    \Magento\GiftMessage\Model\Save $giftMessageSave,
    \Magento\Tax\Model\Config $taxConfig,
    \Magento\Tax\Helper\Data $taxData,
    \Magento\GiftMessage\Helper\Message $messageHelper,
    StockRegistryInterface $stockRegistry,
    StockStateInterface $stockState,
    array $data = []
) {
    $this->_messageHelper = $messageHelper;
    $this->_wishlistFactory = $wishlistFactory;
    $this->_giftMessageSave = $giftMessageSave;
    $this->_taxConfig = $taxConfig;
    $this->_taxData = $taxData;
    $this->stockRegistry = $stockRegistry;
    $this->stockState = $stockState;
    //Pass All Arguments To Parent
    parent::__construct($context, $sessionQuote, $orderCreate, $priceCurrency, $wishlistFactory, $giftMessageSave, $taxConfig, $taxData, $messageHelper, $stockRegistry, $stockState, $data);
}

eval.in demo

代码只是在foreach中运行$arr = [ 'jack' => [ 12, 45, 78, 19, 94, 668, ], 'john' => [ 641, 741, 683, 603, ], ]; function in_array_r ($needle, $haystack) { foreach ($haystack as $key => $subArr) { if (in_array($needle, $subArr)) { return $key; } } return false; } echo in_array_r(641, $arr); // => john echo "\n"; echo in_array_r(12, $arr); // => jack 。这将在2D阵列上工作,但不在具有更多尺寸的阵列上工作。如果您的问题实际上不仅仅是2D数组,请告诉我。

答案 1 :(得分:0)

最佳查找方式如下

<?php

$myarr = Array
(
'Jack' => Array
    (
        'uid' => '100',
        'name' => 'Sandra Shush',
        'url' => 'urlof100'
    ),

'John' => Array
    (
        'uid' => '40489',
        'name' => 'Michael',
        'pic_square' => 'urlof40489'
    )
);

echo checkData($myarr,100);

 function checkData( $myarr,$myvalue ) 
 {
        foreach( $myarr as $key => $value ){
            if( in_array(  $myvalue, $value ) ){
                return $key;
            }
        }
        return false;
 }

检查http://phptester.net/

答案 2 :(得分:0)

我假设您使用了递归的in_array函数,如下所示: https://gist.github.com/Billy-/bc6865066981e80e097f

我已根据您的要求修改了它(因此它可以根据需要返回数组的键):

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict)!==FALSE)) {
            return key($haystack);
        }
    }

    return false;
}

您应该能够将其用作:

echo in_array_r('641',$array); // Prints john
echo in_array_r('12',$array); // Prints jack

我假设的数组是您的问题:

Array
(
    [jack] => Array
        (
            [0] => 12
            [1] => 45
            [2] => 78
            [3] => 19
            [4] => 94
            [5] => 668
        )

    [john] => Array
        (
            [0] => 641
            [1] => 741
            [2] => 683
            [3] => 603
        )

)

答案 3 :(得分:0)

$arr = Array(
    "jack" => Array(12, 45, 78, 19, 94, 668),
    "john" => Array("641", 741, 683, 603),
);
$tofind = 641;

function getKey($arr, $tofind) {
    foreach($arr as $key => $val){
        if(in_array($tofind, $val)){
            return $key;
        }
    }
    return "not found";
}

echo getKey($arr, $tofind);
相关问题