-1

I need to merge data into an array, then count the array values.

$str = '"Cat","A","A","A","A"';
$abc = [
    $str,
    "A",
    "Cat",
    "Dog",
    "A",
    "Dog"
];
print_r(array_count_values($abc));

Result came out:

 Array ( ["Cat","A","A","A","A"] => 1 [A] => 2 [Cat] => 1 [Dog] => 2 )

But I need like this way:

 Array ( [A] => 6 [Cat] => 2 [Dog] => 2 )
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ashish
  • 7
  • 2

7 Answers7

4

This is because $str is a string and not an array. So this is added into the $abc array as one element.

You can convert in into an array with the explode function:

$str = '"Cat","A","A","A","A"';
$arr = explode(',', $str);
// So $arr is Array([0] => "Cat", [1] => "A", [2] => "A", [3] => "A", [4] => "A")

Then you need to remove the double quotes around each element. I suggest to use the trim function, and the array_map, to apply it to each element:

$arr = array_map(function ($item) { return trim($item, '"'); }, $arr);
// $arr is Array([0] => Cat, [1] => A, [2] => A, [3] => A, [4] => A)

Then you can merge it with the rest of values:

$abc = array_merge($arr, array("A","Cat","Dog","A","Dog"));
print_r(array_count_values($abc));
// Should be Array ( [A] => 6 [Cat] => 2 [Dog] => 2 )
Nina Lisitsinskaya
  • 1,818
  • 11
  • 18
  • Why not just lose the array_map function and just replace the `"` when exploding the string? Lots less costly, and saves a whole step. – junkfoodjunkie Aug 30 '18 at 13:21
  • @junkfoodjunkie For me the removing of all quotes before exploding the string looks much less logical, since there are quotes, that clearly surround individual elements separated by commas. And with five elements, it is not "lots less costly", it is more like micro-optimisation. – Nina Lisitsinskaya Aug 30 '18 at 14:57
  • That is of course true (that it is a micro-optimisation), but I always count on shit not staying the same, and that more often than not happens. If you want to be a bit safer (so as not to remove quotes being used in the string itself, you could do a replace for `'","',','` and then just replace the last two `"` afterwards. Also, there is no reason why you can't run the string-replace after exploding, if that makes you feel better: `$splitstr = str_replace('"','',explode(',',$str));` – junkfoodjunkie Aug 30 '18 at 15:36
1

Well, then, don't put a string into an array and expect it to be treated as array values. Either modify the string to be an array, and merge the two arrays, or parse the string and add each value to the array.

For the latter approach, you can do something like:

$str = '"Cat","A","A","A","A"';
$abc = array("A","Cat","Dog","A","Dog");
$splitstr = explode(',',str_replace('"','',$str));

$finalarray = array_merge($abc,$splitstr);

print_r(array_count_values($finalarray));
junkfoodjunkie
  • 3,168
  • 1
  • 19
  • 33
1

Your logic is flawed, you should first create array properly

$str = '"Cat","A","A","A","A"';
$abc = array("A","Cat","Dog","A","Dog"); 
$splitstr = explode(',',str_replace('"','',$str)); 
$finalarray = array_merge($abc,$splitstr);  

print_r(array_count_values($finalarray));

Now you will get the desired result.

munsifali
  • 1,732
  • 2
  • 24
  • 43
rt.jar
  • 168
  • 10
1

Because your quote-wrapped, comma-delimited string closely resembles a json string, I reckon it will be most direct/performant to just wrap the whole string in square braces, then json_decode it and spread it into the array (so that elements are injected into the array one at a time).

Code: (Demo)

$str = '"Cat","A","A","A","A"';

$abc = [
    ...json_decode("[$str]"),
    "A",
    "Cat",
    "Dog",
    "A",
    "Dog"
];
print_r(array_count_values($abc));
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

you put first element as string completely. make it separate

  $str = '"Cat","A","A","A","A"';
  $abc=array(  "Cat","A","A","A","A", "A","Cat","Dog","A","Dog"); 
  print_r(array_count_values($abc));

Or merge array

  $arr1 = ["Cat","A","A","A","A"];
  $abc=array_merge(  $arr1,["A","Cat","Dog","A","Dog"]); 
  print_r(array_count_values($abc));

Demo

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • Ah, the ol' "don't change your code, change the input" trick. But what if they don't have any control over the input data? This is not an awesome answer. – mickmackusa Sep 25 '22 at 10:03
0

You are using array_count_values to count all the values of the $abc array. However, $str is a string and not an array, but an element of the $abc array.

The simplest solutions is to convert $str into an array by removing " double quotes (with str_replace method) and splitting substrings using , as a delimiter (with explode method). This can be done just by adding a single line as shown in this snippet:

$str = '"Cat","A","A","A","A"';
// Converts $str into an array of strings
$str_array = explode(',',str_replace('"','',$str)); 

$abc = array_merge($str_array, array("A","Cat","Dog","A","Dog"));  
print_r(array_count_values($abc));

You can see the execution of this script in this link.

Teocci
  • 7,189
  • 1
  • 50
  • 48
0

According to the PHP manual, you can use array_merge ( array $array1 [, array $... ] )

example:

$str = '"Cat","A","A","A","A"';
$abc=array_merge($str, array("A","Cat","Dog","A","Dog"));
print_r(array_count_values($abc));