K
K
KhalBay2021-08-26 11:40:06
PHP
KhalBay, 2021-08-26 11:40:06

How to sum values ​​from PHP arrays?

I have a huge array:

array

["data"]=>
  array(133) {
    [0]=>
    array(2) {
      ["dimensions"]=>
      array(3) {
        [0]=>
        array(3) {
          ["name"]=>
          string(6) "Google"
          ["id"]=>
          string(6) "google"
          ["favicon"]=>
          string(10) "google.com"
        }
        [1]=>
        array(3) {
          ["name"]=>
          string(22) "Google, search results"
          ["id"]=>
          string(13) "google_search"
          ["favicon"]=>
          string(10) "google.com"
        }
        [2]=>
        array(3) {
          ["name"]=>
          NULL
          ["favicon"]=>
          NULL
          ["url"]=>
          NULL
        }
      }
      ["metrics"]=>
      array(1) {
        [0]=>
        float(328)
      }
    }
    [1]=>
    array(2) {
      ["dimensions"]=>
      array(3) {
        [0]=>
        array(3) {
          ["name"]=>
          string(6) "Yandex"
          ["id"]=>
          string(6) "yandex"
          ["favicon"]=>
          string(13) "www.yandex.ru"
        }
        [1]=>
        array(3) {
          ["name"]=>
          string(22) "Yandex, search results"
          ["id"]=>
          string(13) "yandex_search"
          ["favicon"]=>
          string(13) "www.yandex.ru"
        }
        [2]=>
        array(3) {
          ["name"]=>
          NULL
          ["favicon"]=>
          NULL
          ["url"]=>
          NULL
        }
      }
      ["metrics"]=>
      array(1) {
        [0]=>
        float(54)
      }
    }
    [2]=>
    array(2) {
      ["dimensions"]=>
      array(3) {
        [0]=>
        array(3) {
          ["name"]=>
          string(6) "Yandex"
          ["id"]=>
          string(6) "yandex"
          ["favicon"]=>
          string(13) "www.yandex.ru"
        }
        [1]=>
        array(3) {
          ["name"]=>
          string(22) "Yandex, search results"
          ["id"]=>
          string(13) "yandex_search"
          ["favicon"]=>
          string(13) "www.yandex.ru"
        }
        [2]=>
        array(3) {
          ["name"]=>
          string(21) "тюмень софт"
          ["favicon"]=>
          string(13) "www.yandex.ru"
          ["url"]=>
          string(94) "https://yandex.ru/search/?text=%D1%82%D1%8E%D0%BC%D0%B5%D0%BD%D1%8C%20%D1%81%D0%BE%D1%84%D1%82"
        }
      }
      ["metrics"]=>
      array(1) {
        [0]=>
        float(41)
      }
    }
    [3]=>
    array(2) {
      ["dimensions"]=>
      array(3) {
        [0]=>
        array(3) {
          ["name"]=>
          string(6) "Yandex"
          ["id"]=>
          string(6) "yandex"
          ["favicon"]=>
          string(13) "www.yandex.ru"
        }
        [1]=>
        array(3) {
          ["name"]=>
          string(22) "Yandex, search results"
          ["id"]=>
          string(13) "yandex_search"
          ["favicon"]=>
          string(13) "www.yandex.ru"
        }
        [2]=>
        array(3) {
          ["name"]=>
          string(11) "tyumen soft"
          ["favicon"]=>
          string(13) "www.yandex.ru"
          ["url"]=>
          string(44) "https://yandex.ru/search/?text=tyumen%20soft"
        }
      }
      ["metrics"]=>
      array(1) {
        [0]=>
        float(22)
      }
    }
    [4]=>....


And so on... For some reason, the Google data was summed up, but the Yandex data was not. I did something like this:
<?foreach ($shablon['data'] as $arShablon):?>
     <?if($arShablon['dimensions']['0']['name'] == 'Yandex'):?>
             <?=$yandex = array_sum($arShablon['metrics'])?>
     <?endif;?>
<?endforeach;?>

And received
result

54



41



22




18



17




6



4



4



4

At the same time, the yandex variable is not an array, I cannot sum up all the values ​​from it. I obviously did something wrong, but I can’t figure out where I made a mistake for 2 hours.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question