Total amount of values in PHP array

Asked

Viewed 936 times

1

I have this array below, and I would like to know how I can count how many equal values have in the value[0].

Array:

array(5) {
  [0]=>
  array(9) {
    [0]=>
    string(6) "500038"
    [1]=>
    string(6) "204932"
"
  }
  [1]=>
  array(9) {
    [0]=>
    string(6) "500038"
    [1]=>
    string(6) "204932"
"
  }
  [2]=>
  array(9) {
    [0]=>
    string(6) "100398"
    [1]=>
    string(6) "204932"
"
  }
  [3]=>
  array(9) {
    [0]=>
    string(6) "100398"
    [1]=>
    string(6) "204932"
"
  }
  [4]=>
  array(9) {
    [0]=>
    string(6) "100398"
    [1]=>
    string(6) "204932"
"
  }
}

For example I know that it has 2 equal values that are 500038 and 3 which are 100398. I need the total of both. The numbers on value[0] will vary not always being the same.

I would like a way to do that because I need these figures to know the total quantity and the volume number.

That would be my idea:

inserir a descrição da imagem aqui

  • Already tried using the function array_count_values. http://php.net/manual/en/function.array-count-values.php

  • @Eduardobreno from what I understand he wants to check only the value[0]

  • @Mauroalexandre, right... hehe

1 answer

2


See if he answers you.

<?php

$array[] = array(500038, 204932);
$array[] = array(100398, 204932);
$array[] = array(100398, 204932);
$array[] = array(500038, 204932);
$array[] = array(100398, 204932);


for ($i = 0; $i < count($array); $i++) {
    $volume[$i] = $array[$i][0];
}

$volumeContado = array_count_values($volume);

foreach ($volumeContado as $key => $value) {
    $novoArray[] = array("volume" => $key, "qtd" => $value);
}

var_dump($novoArray);

Exit.

 array (size=2)
  0 => 
    array (size=2)
      'volume' => int 500038
      'qtd' => int 2
  1 => 
    array (size=2)
      'volume' => int 100398
      'qtd' => int 3
  • Perfect. Has to adapt to his code. Imagine the following array $array = array(&#xA; 0 => array("500038", "204932"),&#xA; 1 => array("100398", "204932"),&#xA; 2 => array("500038", "204932"),&#xA; 3 => array("100398", "204932"),&#xA; 4 => array("500038", "204932"),&#xA; 5 => array("50003", "204932"),&#xA; 6 => array("50003845", "204932"),&#xA;);

  • How do I manipulate this data? There’s a way I can store it in a variable ?

  • @Kevin. F,you can assign the result to a variable $contados = array_count_values($contar);

  • @Kevin. F, I edited the code to make it simpler, I moved the Dice volume to make it easier to work.

  • @Eduardobreno ok, is that after that I will put this data in a txt and import to db. But vlw was just that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.