This question already has answers here:
How to detect duplicate values in PHP array?
(13 answers)
Closed 8 years ago.
Ok I am pretty sure there is a simple solution, and I am missing something on this but
lets say I have this a simple array:
Array
(
[0] => 79990
[1] => 79040
[2] => 79100
[3] => 79990
[4] => 79490
[5] => 79290
[6] => 79990
)
0, 3 and 6 are the same value
how do I mark/highlight these values on a foreach loop? result should be something like:
Array
(
[0] => *79990*
[1] => 79040
[2] => 79100
[3] => *79990*
[4] => 79490
[5] => 79290
[6] => *79990*
)
edit: typos
This should do the trick:
<?php
$array = array( '79900',
'79040',
'79100',
'79990',
'79490',
'79290',
'79990');
$count = array_count_values($array);
echo "<pre>".print_r($array, true)."</pre>";
foreach($array as $val)
{
if($count[$val]>1) {
$output[] = "*".$val."*";
} else {
$output[] = $val;
}
}
echo "<pre>".print_r($output, true)."</pre>";
?>
Outputs:
Array
(
[0] => 79900
[1] => 79040
[2] => 79100
[3] => 79990
[4] => 79490
[5] => 79290
[6] => 79990
)
Array
(
[0] => 79900
[1] => 79040
[2] => 79100
[3] => *79990*
[4] => 79490
[5] => 79290
[6] => *79990*
)
Note: Your [0] isn't actually the same as [3] and [6], but I'm assuming this is just a typo
Let me know how you get on!
$array = array("79900","79040","79100","79990","79490","79290","79990");
$count = array_count_values( $array );
$list = array();
foreach( $count as $index => $value ){
if( $value > 1 ){
$list[] = "*" . $index . "*";
}else{
$list[] = $index;
}
}
Note that the repeated index is removed
Related
I have some sorting problem to this i want it to compare properly but the array result is incrementing
$query=mysqli_query($conn,"SELECT * FROM questions WHERE MATCH (question) AGAINST ('$msg*' IN BOOLEAN MODE) limit $limit ");
}
while($row = mysqli_fetch_assoc($query)){
$id = $row ["id"];
$question = $row ["question"];
$answer = $row ["answer"];
$words2 = explode(" ", cleanWords($question));
foreach($words2 as $word)
{
$stem = PorterStemmer::Stem($word);
if(!in_array($stem, $stop_words)) {
$stem_words[] = $stem;
}
}
print_r($stem_words);
echo "<br>";
}
}
Now my result set is incrementing like
Array ( [0] => What [1] => IT [2] => STAFF [3] => )
Array ( [0] => What [1] => IT [2] => STAFF [3] => [4] => what [5] => it [6] => )
Array ( [0] => What [1] => CEIT [2] => UCC [3] => [4] => what [5] => it [6] => [7] => Where [8] => it [9] => )
**I want to get the result like this
Array ( [0] => What [1] => IT [2] => STAFF [3] => )
Array ( [0] => what [1] => it [2] => )
Array ( [0] => Where [1] => it [2] => )
The problem is that you are just adding each word to the $stem_words array. Instead for each record, build a list of words for this record ($newList in this code) and add this record to the $stem_words array...
$newList = [];
foreach($words2 as $word)
{
$stem = PorterStemmer::Stem($word);
if(!in_array($stem, $stop_words)) {
$newList[] = $stem;
}
}
$stem_words[] = $newList;
I have to remove an internal array from an array. Actually, the array is obtained by decoding JSON, and can go upto n levels. I need to remove an internal array from an array of its parent based on the key which is dynamic. Below is the code that I have tried referring to answers on php arrays.
$quotationHistory = json_decode($quotationCollection->getHistory(), true);
$quotationId = 5;
foreach ($quotationHistory as $sales_id => $history) {
foreach($history as $quotationIdValue => $values) {
if ($quotationId == $quotationIdValue) {
unset ($history[$quotationIdValue]);
}
}
}
sample:
Array
(
[1] => Array
(
[5] => Array
(
[0] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[1] => Array
(
[0] => 12
[1] => 8490.0000
[2] => 21-10-2016 11:43:40am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[2] => Array
(
[0] => 45
[1] => 8490.0000
[2] => 21-10-2016 11:43:54am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[3] => Array
(
[0] => 11
[1] => 8490.0000
[2] => 21-10-2016 11:44:04am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[4] => Array
(
[0] => 54
[1] => 8490.0000
[2] => 21-10-2016 11:44:16am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
)
)
)
Now, I want to remove the second level data with key = 5
You'd better do the unset on the original array:
$quotationHistory = json_decode($quotationCollection->getHistory(), true);
foreach($quotationHistory as $sales_id => $history) {
foreach($history as $quotationIdValue => $values) {
if($quotationId == $quotationIdValue){
unset($quotationHistory[$sales_id][$quotationIdValue]);
}
}
}
The reason is that the internal array is passed as a copy. But you can also specify an assignation by reference:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference (official Php doc).
$quotationHistory = json_decode($quotationCollection->getHistory(), true);
foreach($quotationHistory as $sales_id => &$history) {
foreach($history as $quotationIdValue => &$values) {
if($quotationId == $quotationIdValue){
unset($$history[$quotationIdValue]);
}
}
}
You will probably want a recursive iterator since the return can be any level down in the array (presumably that's what you mean by n levels). One note, it would remove any key with the same value so it would remove any key with 5. You would be better to recurse to remove the key based on value rtg:
$arr = array(
'one'=>array(
'one'=>'and a half',
'two'=>array(
'three'=>'three and a half'
)
)
);
function recurse($array,$remove=false)
{
foreach($array as $key => $value) {
if($key != $remove){
if(is_array($value)) {
$new[$key] = recurse($value,$remove);
}
else
$new[$key] = $value;
}
}
return $new;
}
# Run the iterator to remove the key named "three"
print_r(recurse($arr,'three'));
If you need to search by value, this should work:
function recurseValue($array,$remove=false)
{
foreach($array as $key => $value) {
if(is_array($value)) {
$new[$key] = recurseValue($value,$remove);
}
else {
if($value != $remove){
$new[$key] = $value;
}
}
}
return $new;
}
# Run the iterator to remove the key with the value named "Three and a half"
print_r(recurseValue($arr,'three and a half'));
Gives you:
Array
(
[one] => Array
(
[one] => and a half
[two] =>
)
)
This last option will normalize the array to one level so you can loop over it normally:
function recurseArray($array,&$new,$bkey = false)
{
foreach($array as $key => $value) {
if(is_array($value)) {
recurseArray($value,$new,$key);
}
else {
$new[$bkey][] = $value;
}
}
}
$new = array();
recurseArray($arr,$new);
print_r($new);
Gives you:
Array
(
[0] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[1] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[2] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
[3] => Array
(
[0] => 3
[1] => 8490.0000
[2] => 21-10-2016 11:43:18am
[3] => 24-11-2016 11:43:18am
[4] => 199
[5] => rtg
)
)
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 5 months ago.
here: Transforming array values in elements of a subarray using PHP I asked quite the same, but the arrays were flatter. I tried to adapt the code, but unfortunately without success.
How could I merge following arrays so the second array won't be added after the end of the first array, but each subarray of the first array will receive the correspondent values of subarrays of the second. In other words, I want to merge the subarrays. Here my example:
Array 01:
Array01 (
[0] => Array
(
[0] => 40292633
[1] => 412
)
[1] => Array
(
[0] => 41785603
[1] => 382
)
[2] => Array
(
[0] => 48792980
[1] => 373
)
[3] => Array
(
[0] => 44741143
[1] => 329
))
Array 02:
Array02(
[0] => Array
(
[0] => 3460581
[1] => 1407424B1
[2] => 951753
)
[1] => Array
(
[0] => 3484251
[1] => 1028325B1
[2] => 159357
)
[2] => Array
(
[0] => 3519102
[1] => 0586365A1
[2] => 456654
)
[3] => Array
(
[0] => 3529714
[1] => 1059876A1
[2] => 852258
))
Final array:
finalArray(
[0] => Array
(
[0] => 40292633
[1] => 412
[2] => 3460581
[3] => 1407424B1
[4] => 951753
)
[1] => Array
(
[0] => 41785603
[1] => 382
[2] => 3484251
[3] => 1028325B1
[4] => 159357
)
[2] => Array
(
[0] => 48792980
[1] => 373
[2] => 3519102
[3] => 0586365A1
[4] => 456654
)
[3] => Array
(
[0] => 44741143
[1] => 329
[2] => 3529714
[3] => 1059876A1
[4] => 852258
))
Many thanks in advance!
try this code
function merge_arrays($a1, $a2) {
return array($a1, $a2);
}
$result = array_map("merge_arrays", $a1, $a2);
foreach($result as $nr)
{
$nres[] = array_merge ($nr[0], $nr[1]);
}
Try this:
$result = array();
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
foreach($keys as $key) {
if( !array_key_exists($key, $arr1) ) {
$result[$key] = $arr2;
} else if( !array_key_exists($key, $arr2) ) {
$result[$key] = $arr1;
} else {
$result[$key] = array_merge($arr1[$key], $arr2[$key]);
}
}
It does not assume that both arrays have the same keys.
If you'd like to use array_map, this is an equivalent solution:
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
$result = array_map(function($key) use ($arr1,$arr2) {
if( !array_key_exists($key, $arr1) ) {
return $arr2;
} else if( !array_key_exists($key, $arr2) ) {
return $arr1;
}
return array_merge($arr1[$key], $arr2[$key]);
},
$keys);
If you're sure both arrays have the same keys, you can try this:
$result = array();
foreach($arr1 as $key => $arr1val) {
$result[$key] = array_merge($arr1val, $arr2[$key]);
}
I am having an array like this
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 6
[5] => 7
[6] => 8
[7] => 9
)
[4] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
[6] => 8
[7] => 9
[8] => 10
[9] => 11
)
)
Now i want to put this into another array using array_push keyword...
How can i achieve this?
<?php
foreach($yourArray as $array) {
array_push($firstArray, $array);
}
?>
or
<?php
foreach($yourArray as $array) {
$firstArray[] = $array;
}
?>
or
<?php
array_push($firstArray, $array);
?>
$shiftedarray=array();
$aftershift=array();
foreach($twodarray as $key=>$val)
{
//Remove 0th index in array
$shiftedarray[]=array_shift($val);
//Array After Removed 0th index
$aftershift[]=$val;
}
echo "<pre>";
print_r($shiftedarray);
print_r($aftershift);
$oneDimensionalArray = call_user_func_array('array_merge', $aftershift);
print_r($oneDimensionalArray);
I have an array a bit like:
Array (
[1] => Array
(
[1] => 21
[2] => 3
[0] => Analyst
)
[2] => Array
(
[1] => 47
[2] => 8
[0] => Catalysis
)
[3] => Array
(
[1] => 1
[2] => 0
[0] => Biomaterials
)
[4] => Array
(
[3] => 12
[4] => 2
[0] => Analyst
)
[5] => Array
(
[5] => 12
[6] => 2
[0] => Analyst
)
...
However I would like to renumber those entries with the same [0] value so that I end up with
[1] => Array
(
[1] => 21
[2] => 3
[3] => 12
[4] => 2
[5] => 12
[6] => 2
[0] => Analyst
)
So far I've tried getting the [0] values out of the $results array by putting them in their own array and saying if you're already there then add [3] and [4] to where [1] and [2] are in a new array but it's not working.
$final = array();
$jArray = array();
foreach($results as $key => $result) {
if(!in_array($result[0],$jArray) && !empty($result[0])) {
$jArray[$i] = $result[0];
$i++;
}
}
for($x = 0; $x < count($results); $x++) {
$k = array_search($results[$x][0],$jArray);
if(!isset($results[$x][1]))
$final[$k][1] = $results[$x][1];
if(!isset($results[$x][2]))
$final[$k][2] = $results[$x][2];
if(!isset($results[$x][3]))
$final[$k][3] = $results[$x][3];
if(!isset($results[$x][4]))
$final[$k][4] = $results[$x][4];
if(!isset($results[$x][5]))
$final[$k][5] = $results[$x][5];
if(!isset($results[$x][6]))
$final[$k][6] = $results[$x][6];
}
Any simpler ways of doing this?
You can do this way...
$new_arr=array();
$arkeys = array_unique(array_map(function ($v){ return $v[0];},$arr));
foreach($arr as $k=>$arr1)
{
$new_arr[$arr1[0]][]=array_slice($arr1,0,count($arr1)-1);
}
foreach($arkeys as $v)
{
$new_arr[$v] = call_user_func_array('array_merge', $new_arr[$v]);
}
print_r($new_arr);
OUTPUT :
Array
(
[Analyst] => Array
(
[0] => 21
[1] => 3
[2] => 12
[3] => 2
[4] => 12
[5] => 2
)
[Catalysis] => Array
(
[0] => 47
[1] => 8
)
[Biomaterials] => Array
(
[0] => 1
[1] => 0
)
)
Working Demo
If you just want to group by the first element of the sub array, a single loop is enough:
$result = array();
foreach ($array as $sub_arr) {
$key = $sub_arr[0];
unset($sub_arr[0]);
if (!isset($result[$key])) {
$result[$key] = array();
}
$result[$key] += $sub_arr;
}
Here
$final = array();
foreach($results as $key => $result) {
if (!in_array($result[0], array_keys( $final ) ) && !empty($result[0])) {
$final[$result[0]] = array( $result[0] );
}
foreach($result as $k => $v) {
if ($k != 0 && isset($v)) {
$final[$result[0]][$k] = $v;
}
}
}