I have an array of strings and I am looking for a way to find the most common string in the array.
$stuff = array('orange','banana', 'apples','orange');
I would want to see orange.
$c = array_count_values($stuff);
$val = array_search(max($c), $c);
Use array_count_values and get the key of the item:
<?php
$stuff = array('orange','banana', 'apples','orange', 'xxxxxxx');
$result = array_count_values($stuff);
asort($result);
end($result);
$answer = key($result);
echo $answer;
?>
Output:
orange
Related
How to split array with , in 2 parts so that that can be used as 2 strings in php?
I am getting this kind of array you can view image with this list.
I want to split this array to post id and title in separate columns.
<li id="recordsArray_'.$value['id'].','.$value['title'].'"></li>
Give a try with below code if it works for you
$arr = array('1,test1','2,test2','3,test3');
foreach($arr as $ar){
$res = explode(',', $ar);
$id = $res[0];
$title = $res[1];
echo 'id-'.$id;
echo ' title-'.$title;
echo '<br>';
}
If you want to separate array into two string, you can use explode like this short code:
$item = '4,test';
$var = explode(',', $item);
echo $var;
and for loop array, you can use
$array = array(YOUR_ARRAY);
foreach($array as $item){
$var = explode(',', $item);
echo $var;
}
Update 1:
You can use a simple array like below code:
$array = array(4, 'salar');
and use a code like this:
$array = array(4, 'salar');
foreach ($array as $item) {
dump(explode(',', $item));
}
I have an array.First i want to search a specific value in that array and want to return that value with multiple by a value.I was trying below way but in this way its multiple with array keys but i want it will multiple with arrays value. It can be very simple but i don't know the solution.
<?php
$arr = ['10','20','30','40'];
$searching = array_search("30", $arr);
if ($searching) {
$result = $searching*30;
echo $result;
}
?>
Output is : 60
But i want : 900
The array_search() function search an array for a value and returns
the key.
Try this:
array_search give you the index, so use $arr[$searching].
$arr = ['10','20','30','40'];
$searching = array_search("30", $arr);
if ($searching) {
$result = $arr[$searching] * 30;
echo $result;
}
in_array function will help you to determine if value exists in your array.
If it exists - return this value, multiplied by some other value.
$arr = ['10','20','30','40'];
$search_val = 20;
if (in_array($search_val, $arr)) {
echo 30 * $search_val;
}
Try it:-
<?php
$arr = ['10','20','30','40'];
$val = 20;
echo (in_array($val, $arr)) ? (30 * $val) : "";
?>
If I want to add multiple values in array having same index in PHP, then can it be possible to create this type of an array? For e.g.,
fruits[a]="apple";
fruits[a]="banana";
fruits[a]="cherry";
fruits[b]="pineapple";
fruits[b]="grappes";
I want array to look like as below:-
fruits = {[a]=>"apple",[a]=>"banana",[a]=>"cherry",[b]=>"pineapple",[b]=>"grappes"};
You cannot define multiple value under same key or index.
In your case -
fruits[a]="apple";
fruits[a]="banana";
Here apple will be replaced by banana.
Instead, you may define array as -
fruits[a][] = "apple";
fruits[a][] = "banana";
Edit: i updated my answer with php code, but i don't code php usually, this might not be the most optimal solution, i tried this code in a php sandbox
$subarray1[0] = "apple";
$subarray1[1] = "banana";
$subarray1[2] = "cherry";
$subarray2[0] = "pineapple";
$subarray2[1] = "grappes";
$fruits[0] = $subarray1;
$fruits[1] = $subarray2;
foreach( $fruits as $key => $value ){
foreach( $value as $key2 => $value2 ){
echo $key2."\t=>\t".$value2."\n";
}
}
use implode and explode .
subarray1[0] = "apple"
subarray1[1] = "banana"
subarray1[2] = "cherry"
subarray2[0] = "pineapple"
subarray2[1] = "grappes"
It is store data with ,(comma)
$ar="";
for($i=0;$i<=count(subarray1);$i++)
{
$ar[]=subarray1[$i];
}
$rt=implode(',',$ar);
echo $rt;
It is Remove ,(comma) form array
$ex=explode(",",$ar);
print_r($ex);
I have an associative array, $teams_name_points. The length of the array is unknown.
How do I access the first and forth value without knowing the key of this array in the easiest way?
The array is filled like this:
$name1 = "some_name1";
$name2 = "some_name2";
$teams_name_points[$name1] = 1;
$teams_name_points[$name2] = 2;
etc.
I want to do something like I do with an indexed array:
for($x=0; $x<count($teams_name_points); $x++){
echo $teams_name_points[$x];
}
How do I do this?
use array_keys?
$keys = array_keys($your_array);
echo $your_array[$keys[0]]; // 1st key
echo $your_array[$keys[3]]; // 4th key
You can use array_values which will give you a numerically indexed array.
$val = array_values($arr);
$first = $val[0];
$fourth = $val[3]
In addition to the array_values, to loop through as you show:
foreach($teams_name_points as $key => $value) {
echo "$key = $value";
}
You can get use the array_keys function such as
//Get all array keys in array
$keys = array_keys($teams_name_points);
//Now get the value for 4th key
//4 = (4-1) --> 3
$value = $teams_name_points[$keys[3]];
You can get all values now as exists
$cnt = count($keys);
if($cnt>0)
{
for($i=0;$i<$cnt;$i++)
{
//Get the value
$value = $team_name_points[$keys[$i]];
}
}
I am wondering how I can add a string variable to the current array. For example, I have an array called $finalarray. Then I have a loop that adds a value on every run. Basically:
$finalarray = $results_array + string;
A very basic structure. I am using this for MySQL so that I can retrieve the final array of the column.
$query = "SELECT * FROM table";
$showresult = mysql_query($query);
while($results_array = mysql_fetch_assoc($showresult))
{
$finalarray = $finalarray + $results_array["column"];
}
Edit:
Currently using this code (still not working):
$query = “SELECT * FROM table”;
$showresult = mysql_query($query);
while($results_array = mysql_fetch_assoc($showresult))
{
$finalarray[] = $results_array["name"];
}
echo $finalarray;
The problem is that it just says "Array"
Thanks,
Kevin
Answer to your edited question:
You cannot use just a single echo to print the entire array contents. Instead
Use
var_dump($finalarray);
or
print_r($finalarray);
to print the array contents..
Use the [] notation. + is for unioning two arrays.
$array = array('foo');
$array[] = 'bar'.
// $array == array('foo', 'bar')
You can use the function array_push or [] notation:
array_push($array, 'hi');
$array[] = 'hi';
Take a look at this
$query = "SELECT * FROM table";
$showresult = mysql_query($query);
while($results_array = mysql_fetch_assoc($showresult))
{
$finalarray[] = $results_array["column"];
}
// Add X to the end of the array
$finalarray[] = "X";
Codaddict is correct. You are looking to output the last element that was added to the array.
echo end($final_array);
That will move the array's pointer to the last element added to it and then output it. Since you're adding the elements to the array in this manner...
$finalarray[] = $results_array["name"];
The key for each of your elements will be sequential, 0,1,2,3...so on and so forth. Another less elegant solution to get the last element would be...
echo $final_array[count($final_array) - 1];
echo implode($finalarray);
Or with a custom join "glue"
echo implode(', ', $finalarray);