如何使用PHP从这个数组中得到“count”?

问题描述 投票:0回答:4

我想要结果

  • label_id 29和content_text 19 = 1
  • label_id 29和content_text 27 = 1
Array
(
    [0] => Array
        (
            [row_id] => 70
            [content_id] => 354
            [label_id] => 28
            [type_id] => 6
            [label_name] => product
            [content_text] => 14
        )

    [1] => Array
        (
            [row_id] => 70
            [content_id] => 355
            [label_id] => 29
            [type_id] => 6
            [label_name] => location
            [content_text] => 26
        )

    [2] => Array
        (
            [row_id] => 70
            [content_id] => 356
            [label_id] => 30
            [type_id] => 6
            [label_name] => business
            [content_text] => 35
        )

    [3] => Array
        (
            [row_id] => 70
            [content_id] => 362
            [label_id] => 35
            [type_id] => 6
            [label_name] => visibility
            [content_text] => 41
        )

    [4] => Array
        (
            [row_id] => 71
            [content_id] => 365
            [label_id] => 28
            [type_id] => 6
            [label_name] => product
            [content_text] => 14
        )

    [5] => Array
        (
            [row_id] => 71
            [content_id] => 366
            [label_id] => 29
            [type_id] => 6
            [label_name] => location
            [content_text] => 27
        )

    [6] => Array
        (
            [row_id] => 71
            [content_id] => 367
            [label_id] => 30
            [type_id] => 6
            [label_name] => business
            [content_text] => 35
        )

    [7] => Array
        (
            [row_id] => 71
            [content_id] => 373
            [label_id] => 35
            [type_id] => 6
            [label_name] => visibility
            [content_text] => 42
        )

    [8] => Array
        (
            [row_id] => 74
            [content_id] => 389
            [label_id] => 28
            [type_id] => 6
            [label_name] => product
            [content_text] => 14
        )

    [9] => Array
        (
            [row_id] => 74
            [content_id] => 390
            [label_id] => 29
            [type_id] => 6
            [label_name] => location
            [content_text] => 21
        )

    [10] => Array
        (
            [row_id] => 74
            [content_id] => 391
            [label_id] => 30
            [type_id] => 6
            [label_name] => business
            [content_text] => 35
        )

    [11] => Array
        (
            [row_id] => 74
            [content_id] => 397
            [label_id] => 35
            [type_id] => 6
            [label_name] => visibility
            [content_text] => 42
        )

    [12] => Array
        (
            [row_id] => 85
            [content_id] => 420
            [label_id] => 28
            [type_id] => 6
            [label_name] => product
            [content_text] => 14
        )

    [13] => Array
        (
            [row_id] => 85
            [content_id] => 421
            [label_id] => 29
            [type_id] => 6
            [label_name] => location
            [content_text] => 18
        )

    [14] => Array
        (
            [row_id] => 85
            [content_id] => 422
            [label_id] => 30
            [type_id] => 6
            [label_name] => business
            [content_text] => 35
        )

    [15] => Array
        (
            [row_id] => 85
            [content_id] => 428
            [label_id] => 35
            [type_id] => 6
            [label_name] => visibility
            [content_text] => 42
        )

    [16] => Array
        (
            [row_id] => 136
            [content_id] => 531
            [label_id] => 28
            [type_id] => 6
            [label_name] => product
            [content_text] => 14
        )

    [17] => Array
        (
            [row_id] => 136
            [content_id] => 532
            [label_id] => 29
            [type_id] => 6
            [label_name] => location
            [content_text] => 27
        )

    [18] => Array
        (
            [row_id] => 136
            [content_id] => 533
            [label_id] => 30
            [type_id] => 6
            [label_name] => business
            [content_text] => 35
        )

    [19] => Array
        (
            [row_id] => 136
            [content_id] => 539
            [label_id] => 35
            [type_id] => 6
            [label_name] => visibility
            [content_text] => 42
        )

    [20] => Array
        (
            [row_id] => 56
            [content_id] => 258
            [label_id] => 28
            [type_id] => 6
            [label_name] => product
            [content_text] => 14
        )

    [21] => Array
        (
            [row_id] => 56
            [content_id] => 259
            [label_id] => 29
            [type_id] => 6
            [label_name] => location
            [content_text] => 24
        )

    [22] => Array
        (
            [row_id] => 56
            [content_id] => 261
            [label_id] => 30
            [type_id] => 6
            [label_name] => business
            [content_text] => 35
        )

    [23] => Array
        (
            [row_id] => 56
            [content_id] => 267
            [label_id] => 35
            [type_id] => 6
            [label_name] => visibility
            [content_text] => 41
        )

)

php arrays get
4个回答
1
投票

请试试这段代码。

$query="select * From user";
$queryResult = $db->query($query);
$result = $queryResult->fetch();
echo $result['count'];

0
投票

你可以使用这个功能echo count( $arrayResult);但你的问题不明确.....


0
投票

如果你想要计算唯一的label_id&context_text字段,那么用你想要的字段重新输入输入数组,然后只需使用count函数进行计数。

我不清楚你的问题,但我想可能你想要这样的输出。


0
投票

如果要计算具有特定值的键/的数组元素,请使用以下代码。

<?php

// By Moustafa Elkady

function count_by_key_value($arr,$search)
{
    $count = 0;

    foreach ($arr as $array_elements) {
        $matched = 0;
        foreach ($search as $search_key => $search_value) {
            if (isset($array_elements[$search_key]) && $array_elements[$search_key] == $search_value) {
                $matched++;
            }
        }

        if ($matched == count($search)) {
            $count++;
        }

    }

    return $count;
}


$arr = [

    [
        'label_id' => 29,
        'content_text' => 19
    ],

    [
        'label_id' => 29,
        'content_text' => 19
    ],

    [
        'label_id' => 2,
        'content_text' => 19
    ],

];

var_dump(count_by_key_value($arr,['label_id' => 2,'content_text' => 19]));
© www.soinside.com 2019 - 2024. All rights reserved.