I would like to convert a strings array into an array that counts the occurrences of each string and store it in the new array in the string index.
[a][a][b] will turn into an `array("a" => "2", "b" => "1")
EDIT: I don't know the original array values. I didn't wrote any code yet since I don't know how to approach this problem.
Try this: use array_count_values.for more visit here:http://php.net/manual/en/function.array-count-values.php
$arr=array("a","a","b");
print_r(array_count_values($arr));
output:
Array
(
[a] => 2
[b] => 1
)
array_count_values() best suits your need ! It does what you need, meaning counting occurences of element in your array, creating another array in result containing for keys the element, and for values the number of this element.
(Exemple taken from PhP documentation)
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
will result in an array containing :
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
Related
I want to need multiple array combaine in one array
I have array
array(
0=> test 1
)
array(
0=> test 2
)
array(
0=> test 3
)
I need expected output
`array(
0=>Test1
1=>Test2
2=>test3
)`
You can use the array_merge() for this. The array_merge() function merges one or more arrays into one array.If two or more array elements have the same key, the last one overrides the others.
Syntax:
array_merge(array ...$arrays): array
Example:
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
Result:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
You can check more here.
$a = array('test_1');
$b = array('test_2');
$c = array('test_3');
print_r(array_merge($a,$b,$c));
O/P - Array ( [0] => test_1 [1] => test_2 [2] => test_3 )
Hope you are doing well and good.
So, as per your requirement i found solution to get result.
$array1 = [0 => "Test 1"]; $array2 = [0 => "Test 2"]; $array3 = [0 => "Test 3"];
print_r(array_merge($array1,$array2,$array3));
In the above example you have to merge the n number of array with single array, so for that you need to use array function which is array_merge(array ...$array).
What is array_merge()?
The array_merge() function merges one or more arrays into one array.
Tip: You can assign one array to the function, or as many as you like.
Note: If two or more array elements have the same key, the last one overrides the others.
Everyone else usually asking how to convert string array with commas to an array of key value pairs.
But my question is opposite. I want to extract the keys of the array and place them in a separate array using PHP
I have an array of this form:
Array
(
[Lights] => 4
[Tool Kit] => 4
[Steering Wheel] => 4
[Side Mirrors] => 3.5
)
and I want output to be in this form:
{"Lights", "Tool Kit", "Steering Wheel", "Side Mirrors" }
Using array_keys :
array_keys — Return all the keys or a subset of the keys of an array
So you can just extract each keys simply by using this method
$keys = array_keys($array);
Otherwise, you can loop through each values and only get the keys :
$keyArray=array_keys($array);
$keyArray=[];
foreach($array as $key => $value){
$keyArray[]=$key;
}
Use array_keys. It will return array keys as array values
$array=array('Lights' => 4,
'Tool Kit' => 4,
'Steering Wheel' => 4,
'Side Mirrors' => 3.5);
$key_array=array_keys($array);
print_r($key_array);
It will result
Array ( [0] => Lights [1] => Tool Kit [2] => Steering Wheel [3] => Side Mirrors )
Looks like you are expecting JSON output. You can just use json_encode function in a pair with array_keys.
$result = json_encode(array_keys($array));
However, your result will be ["Lights","Tool Kit","Steering Wheel","Side Mirrors"]
I want to merge many different assoc arrays in one array but in the form of assoc array. Like i have different arrays like this
Array ( [0] => abc [1] => def [2] => ghi )
Array ( [0] => jkl [1] => mno [2] => pqr )
.
.
.
and want to make an array like
array
0 =>
array
0 => string 'abc'
1 => string 'def'
2 => string 'ghi'
1 =>
array
0 => string 'jkl'
1 => string 'mno'
2 => string 'pqr'
.
.
.`
.
i am getting these arrays from a csv file. Please help. Thanks
If I understand correctly, you don't want to merge the arrays... you just want to make a multidimensional array i.e. an array of arrays. See the difference here.
You are creating the initial arrays from the csv file, but I'll create them here for completeness:
$array1 = array ( "0" => "abc", "1" => "def", "2" => "ghi" );
$array2 = array ( "0" => "jkl", "1" => "mno", "2" => "pqr" );
Then all you need to do is create an array with those arrays as the values, depending on what works with the rest of your code, e.g.
$multiarray = array();
$multiarray["0"] = $array1;
$multiarray["1"] = $array2;
or
$multiarray = array ( "0" => $array1, "1" => $array2 );
If you print_r ($multiarray);, it will look like the example in your question.
By the way, the examples you have given are not associative arrays, but I've treated them as if they are in case you do actually need them.
If your arrays are just standard indexed arrays, then you don't need to specify the key, e.g.
$array1 = new array("abc", "def", "ghi");
etc
$multiarray[] = $array1;
$multiarray[] = $array2;
I provide another point of view, which is lest optimized for the task in itself as I see it but might be useful in some context.
$array = array('abc', 'def', 'ghi');
$array2 = array('jkl', 'mno', 'pqr');
function gather(... $array){return $array;}
my_print_r(gather($array, $array2));
That function uses the splat operator, which natively gathers whatever arguments are sent to the function as entries in an array, called array in that example. We can do whatever we want with array in that function, but just by retuning it, it does what you asked for.
This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed 6 years ago.
I wonder why I don't get the 3 elements of my array.
$array1 = array(
"One" => 1,
"Two" => 2,
"One" => 1
);
When I print it:
echo 'array1:<pre>'; print_r($array1); echo '</pre>';
I get this:
array1:
Array
(
[One] => 1
[Two] => 2
)
This is not what I want. I need to show the following:
array1:
Array
(
[One] => 1
[Two] => 2
[One] => 1
)
Any help wil be appreciated.
Thanks in advance
Your array is a set of key/value pairs. Think of it as a dictionary:
array( "elephant" => "Big grey animal with tusks",
"canary" => "Little Yellow Bird",
"elephant" => "Candy that tastes like Skittles"
)
When you print this one, you will always get the second definition of "elephant", just like in your code. Try changing the second "one" => 1 to "one" => 77.
Because PHP reads from top-down, the last one will always be the final answer, just like in variables.
You can't have duplicate keys in an array. So by declaring the array['one'] you might replace the old value set before for key array['one'].
Use array of array..I think you are trying to achieve that..
But this looks weird
array( ['one'] => array(1,3), ['two']=> 2);
I have two assoc array i want to creat one array out of that
E.g
a(a=>1
b=>3
f=>5
)
b(a=>4
e=>7
f=>9
)
output must be
c(
a=>1
b=>3
f=>5
a=>4
e=>7
f=>9
)
i am new in php
Use array_merge(). Your resulting array CAN NOT have more than one entry for the same key, so the second a => something will overwrite the first.
Use the + operator to return the union of two arrays.
The new array is constructed from the left argument first, so $a + $b takes the elements of $a and then merges the elements of $b with them without overwriting duplicated keys. If the keys are numeric, then the second array is just appended.
This ey difference of the + operator and the function, array_merge is that array merge overwrites duplicated keys if the latter arguments contain that key. The documentation puts it better:
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If the keys are different, then use array_merge()
<?php
$a1=array("a"=>"Horse","b"=>"Cat");
$a2=array("c"=>"Cow");
print_r(array_merge($a1,$a2));
?>
OUTPUT:
Array ( [a] => Horse [b] => Cat [c] => Cow )
If the keys are the same, then use array_merge_recursive()
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>
OUTPUT:
Array
(
[color] => Array
(
[favorite] => Array
(
[0] => red
[1] => green
)
[0] => blue
)
[0] => 5
[1] => 10
)