如何检查多维数组中的值

时间:2016-08-26 06:33:00

标签: php arrays multidimensional-array

我的数组位于

之下
$sample_arr = Array
    (
        [0] => Array
            (
                [index] => 0
                [in_id] => 309
                [date] => 2016-08-01
                [article_Id] => 4
                [qty] => 50
                [ref] => ref
            )

        [1] => Array
            (
                [index] => 1
                [in_id] => 309
                [date] => 2016-08-01
                [article_Id] => 6
                [qty] => 20
                [ref] => ref 2
            )

        [2] => Array
            (
                [index] => 2
                [in_id] => 309
                [date] => 2016-08-01
                [article_Id] => 6
                [qty] => 21
                [ref] => ref 2
            )

    )

正如您在上面的数组中所看到的,数组中有article_Id两次,值为6

我想找到第二行的详细信息,以便我可以使第二行数量为41,即我的结果数组将如下所示

我已经尝试了in_array函数,但仍然缺少一些东西

我也尝试过foreach但问题是如何获得第一次出现行数量?即在$ sample_arr中,当用户使用article_Id 6添加第三条记录时,必须更新第二行,如下所示

 $result_arr = Array
        (
            [0] => Array
                (
                    [index] => 0
                    [in_id] => 309
                    [date] => 2016-08-01
                    [article_Id] => 4
                    [qty] => 50
                    [ref] => ref
                )

            [1] => Array
                (
                    [index] => 1
                    [in_id] => 309
                    [date] => 2016-08-01
                    [article_Id] => 6
                    [qty] => 41
                    [ref] => ref goes here
                )

        )

4 个答案:

答案 0 :(得分:1)

看起来文章ID是唯一的。因此,我们可以使用密钥作为文章ID来存储此$ newArray变量中的数据。

每次都检查是否存在包含文章ID的记录。如果是,则添加数量。如果没有,则将其附加到$ newArray。

$newArray = array();

foreach($sample_arr as $arr) {
    if (isset($newArray[$arr['article_Id']])) {
        $newArray[$arr['article_Id']]['qty'] += $arr['qty'];
        $newArray[$arr['article_Id']]['ref'] = 'Ref goes here';  // If this the string that replaces the ref  
    } else {
        $newArray[$arr['article_Id']] = $arr;
    }
}

$newArray = array_values($newArray);

输出:

Array
(
 [0] => Array
    (
        [index] => 0
        [in_id] => 309
        [date] => 2016-08-01
        [article_Id] => 4
        [qty] => 50
        [ref] => ref
    )

 [1] => Array
    (
        [index] => 1
        [in_id] => 309
        [date] => 2016-08-01
        [article_Id] => 6
        [qty] => 41
        [ref] => ref 2
    )

)

答案 1 :(得分:1)

尝试以下方法:

$new_merged_array = array();

if(!empty($sample_arr )){
    foreach($sample_arr  as $sample_ar){
        if(!empty($sample_ar) && isset($sample_ar['article_Id'])){
            if(isset($new_merged_array[$sample_ar['article_Id']])){
                $new_merged_array[$sample_ar['article_Id']]['qty'] += $sample_ar['qty'];
            }else{
                $new_merged_array[$sample_ar['article_Id']] = $sample_ar;
            }
        }
    }
}
print_r($new_merged_array);

输出将是:

Array(
    [4] => Array
        (
            [index] => 0
            [in_id] => 309
            [date] => 2016-08-01
            [article_Id] => 4
            [qty] => 50
            [ref] => ref
        )

    [6] => Array
        (
            [index] => 1
            [in_id] => 309
            [date] => 2016-08-01
            [article_Id] => 6
            [qty] => 41
            [ref] => ref 2
        )
)

另一种方式:

$new_merged_array = $sample_array = array();

if(!empty($sample_arr )){
    foreach($sample_arr  as $sample_ar){
        if(!empty($sample_ar) && isset($sample_ar['article_Id'])){
            if(isset($new_merged_array[$sample_ar['article_Id']])){
                $new_merged_array[$sample_ar['article_Id']]['qty'] += $sample_ar['qty'];
            }else{
                $new_merged_array[$sample_ar['article_Id']] = $sample_ar;
            }
        }
    }
}

if(!empty($new_merged_array )){
    foreach($new_merged_array  as $new_merged_arr){
        $sample_array[] = $new_merged_arr;
    }
}
print_r($sample_array);

输出将是:

Array(
    [0] => Array
        (
            [index] => 0
            [in_id] => 309
            [date] => 2016-08-01
            [article_Id] => 4
            [qty] => 50
            [ref] => ref
        )

    [1] => Array
        (
            [index] => 1
            [in_id] => 309
            [date] => 2016-08-01
            [article_Id] => 6
            [qty] => 41
            [ref] => ref 2
        )
)

答案 2 :(得分:1)

$sample_arr = array
    (
    0 => array
    (
        'index'=> 0,
        'in_id' => 309,
        'date' => '2016-08-01',
        'article_Id' => 4,
        'qty' => 50,
        'ref' => 'ref'
    ),
    1 => array
    (
        'index' => 1,
        'in_id' => 309,
        'date' => '2016-08-01',
        'article_Id' => 6,
        'qty' => 20,
        'ref' => 'ref 2'
    ),
    2 => array
    (
        'index' => 2,
        'in_id' => 309,
        'date' => '2016-08-01',
        'article_Id' => 6,
        'qty' => 21,
        'ref' => 'ref 2'
    )
);

$articleArray = [];
foreach($sample_arr as $key=>$value){   
    $articleId = $value['article_Id'];
    if(array_key_exists($articleId,$articleArray)){
        $articleArray[$articleId]['qty'] = $articleArray[$articleId]['qty'] + $value['qty'];
    }else{
        $articleArray[$articleId] = $value;
    }
}
$articleArray = array_values($articleArray);

print_r($sample_arr);
print_r($articleArray);
  

Demo

答案 3 :(得分:-2)

in_array()不适用于多维数组。您可以编写一个递归函数来为您执行此操作:

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))) {
            return true;
        }
    }

    return false;
}

用法:

$b = array(array("Mac", "NT"), array("Irix", "Linux"));
echo in_array_r("Irix", $b) ? 'found' : 'not found';