Highscharts not working after array passed array_diff - php

I need to remove values = 99 from array. But after the array_diff the Highcharts doesn't show any data anymore.
$a = array ("$object->hdc_wed_01", "$object->hdc_wed_02", "$object->hdc_wed_03", "$object->hdc_wed_04", "$object->hdc_wed_05", "$object->hdc_wed_06", "$object->hdc_wed_07", "$object->hdc_wed_08", "$object->hdc_wed_09", "$object->hdc_wed_10", "$object->hdc_wed_11", "$object->hdc_wed_12", "$object->hdc_wed_13", "$object->hdc_wed_14", "$object->hdc_wed_15", "$object->hdc_wed_16", "$object->hdc_wed_17", "$object->hdc_wed_18", "$object->hdc_wed_19", "$object->hdc_wed_20", "$object->hdc_wed_21", "$object->hdc_wed_22", "$object->hdc_wed_23", "$object->hdc_wed_24", "$object->hdc_wed_25", "$object->hdc_wed_26", "$object->hdc_wed_27", "$object->hdc_wed_28", "$object->hdc_wed_29", "$object->hdc_wed_30", "$object->hdc_wed_31", "$object->hdc_wed_32", "$object->hdc_wed_33", "$object->hdc_wed_34", "$object->hdc_wed_35");
$b = array (99);
$c = array_diff($a, $b);
$handicaps = json_encode($c);
$handicaps_clean = str_replace('"',"", $handicaps);
If I'm using $handicaps = json_encode($a); (the unfiltered array) it works fine, but the values equal to 99 are in it. I want to remove them.

As I clearly understood you there's an array of values like $ar = [3,5,2,5,6,7,99,6,5,7,8,99] and some specific array with non-desired values like $remove = [99].
If so, then you can use unset() function to remove such elements from $ar:
$ar = [3,5,2,5,6,7,99,6,5,7,8,99];
$remove = [99];
foreach($ar as $ind => $val){
if (in_array($val,$remove)) unset($ar[$ind]);
}
Outputs:
Array
(
[0] => 3
[1] => 5
[2] => 2
[3] => 5
[4] => 6
[5] => 7
[7] => 6
[8] => 5
[9] => 7
[10] => 8
)
Demo
Hope you've figured out the idea.

Related

Add default values to array to fill gaps between indexesif array keys are consecutive [duplicate]

This question already has answers here:
Add elements to array which has gapped numeric keys to form an indexed array / list
(5 answers)
Closed 5 months ago.
I need to check an array for its keys.
If they are consecutive, its good,if not i need to fill in some data.
e.g.
Array
(
[0] => 0
[1] => 1
[2] => 8
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[10] => 0
[11] => 0
[12] => 0
[14] => 0
[15] => 0
)
In this case, the indexes 9 and 13 are missing.
To make the example easier, I just want to fill the missing data with the number 999.
My solution however is a little sloppy and doesn't work properly:
$oldK = 0;
foreach($array as $k=>$entry)
{
if($oldK !== $k)
{
$array[$oldK] = 999;
}
$oldK ++;
}
produces following output:
Array
(
[0] => 0
[1] => 1
[2] => 8
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 999
[10] => 999
[11] => 999
[12] => 999
[13] => 999
[14] => 0
[15] => 0
)
is there a smooth way that works?
You may use array_replace combined with array_fill:
$keys = array_keys($a);
$result = array_replace(array_fill(0, max($keys), 999), $a);
Grabbing keys through array_keys first should make it work for any number of elements (provided the keys are numeric, obviously).
Demo: https://3v4l.org/Ik71a
You can use array-fill to create you array and then fill (override) with data from the original array:
$keys = array_keys($arr);
$new = array_fill(min($keys), max($keys),999);
foreach($new as $k => &$v)
if (isset($arr[$k])) $v = $arr[$k];
Notice the & to alter the value in the looping array
Another way to achieve that is with the + operator. Just use ksort afterward if you want the order:
$keys = array_keys($arr);
$new = $arr + array_fill(min($keys), max($keys),999);
ksort($new);
Live example 3v4l
$oldK = 0;
foreach($array as $k=>$entry)
{
if($oldK !== $k)
{
$array[$oldK] = 999;
}
$oldK ++;
}
In your above code, you are advancing the array pointer $k without waiting for keys to fill in a sequential order. To compare $oldK !== $k, the $k needs to be still until $oldK reaches it. Below is the fixed code where in we loop over the keys of the array and do the checks.
$keys = array_keys($array);
$oldK = 0;
while(current($keys) !== false){
$key = current($keys);
if($key === $oldK) next($keys);
else $array[$oldK] = 999;
$oldK++;
}
ksort($array);// to arrange them in sorted order
Demo: https://3v4l.org/GD61j
This should be your complete solution
//4 and 5 is missing in given array1
$arr1 = [0 => 0, 1 => 0, 2 => 8, 3 => 0, 11 => 0, 12 => 0, 15 => 0];
//Create a new array
$arr2 = range(1,max($arr1));
// use array_diff to get the missing numbers and create a new array with them
$missingNumbers = array_diff($arr1,$arr2);
$keys = array_keys($missingNumbers);
$Sonuc = array_replace(array_fill(min($keys), max($keys), 999), $arr1);
print_r($Sonuc);
Demo : https://3v4l.org/TbAho

How to remove same value from an array

I have this array. I want to duplicate all records form an array. I tried array_unique but it's removing duplicate but doesn't remove orignal value.
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 1
[4] => 6
[5] => 1
[6] => 23
[7] => 2
)
I want to remove all duplicate value like 1 and 2 and I want this output :
Array
(
[0] => 3
[1] => 6
[2] => 23
)
You could use a combination of array_filter and array_count_values.
$values = [1,2,3,1,6,1,23,2];
$result = array_filter(array_count_values($values), function($x) {
return $x === 1;
});
print_r(array_keys($result));
Result:
Array
(
[0] => 3
[1] => 6
[2] => 23
)
You can also use array_intersect with array_count_values.
Array_intersect returns values that is 1, and array_keys returns the keys (values).
$values = [1,2,3,1,6,1,23,2];
$result = array_keys(array_intersect(array_count_values($values), [1]));
var_dump($result); //[3,6,23]
https://3v4l.org/cHU5E
Another option is to use array_unique and the use array_diff_assoc() to get a list of what has been removed.
Using that array list in array_diff results in the values that is not duplicated.
$values = [1,2,3,1,6,1,23,2];
$diff = array_diff_assoc($values, array_unique($values));
$result = array_diff($values, $diff);
var_dump($result); //[3,6,23]
https://3v4l.org/XM5sk

How to filter out blank values from an array in PHP & Reindex the array

I am printing out the values using print_r($_POST["prod_sizes"]);
print_r($_POST["prod_sizes"]);
So i get output like this => dsf,,thisnajndk,faklsf,klam,flkamlkd,mklmd,l,,adad
After that i use this code:
$sizes = strip_tags(preg_replace('/[^a-z0-9,]/i', '', $_POST["prod_sizes"]));
$var = explode(',', $sizes);
print_r($var);
I get output like
Array
(
[0] => dsf
[1] =>
[2] => thisnajndk
[3] => faklsf
[4] => klam
[5] => flkamlkd
[6] => mklmd
[7] => l
[8] =>
[9] => adad
)
As from the above output we can see that there are some blank values in an array. How to remove those blank values?
PHP's built in array_filter() will do this for you:
$sizes = array_filter($sizes);
Keep in mind that any values are equate to false will also be filtered out. So if zero and null is a valid value you will need to write your own callback to filter by.
If you also want to re-index the keys just use array_values() on the result:
$sizes = array_values(array_filter($sizes));
Replace your code from this line => $var = explode(',', $sizes); & add this:
$var = array_filter(explode(',', $sizes));
$reindexed = array();
foreach ($var as $row)
{
if ($row !== null)
$reindexed[] = $row;
}
print_r($reindexed);
exit();
Let's See The Explaination of the code now
1.) This is 1st reference link from where i took the idea to filer but ass you prnt the array you will see that the array indexes are jumbled => Remove empty array elements
$var = array_filter(explode(',', $sizes));
$reindexed = array();
so we create a new variables reindexed as an array to store the reindexed array value
2.) To remove the jumbled array index and reindex the array i took refernce from this link => How to remove null values from an array?
$reindexed = array();
foreach ($var as $row)
{
if ($row !== null)
$reindexed[] = $row;
}
print_r($reindexed);
Here is the best way:
# Array
$array = array('tomato', '', 'apple', 'melon', 'cherry', '', '', 'banana');
that returns
Array
(
[0] => tomato
[1] =>
[2] => apple
[3] => melon
[4] => cherry
[5] =>
[6] =>
[7] => banana
)
by doing this
$array = array_values(array_filter($array));
you get this
Array
(
[0] => tomato
[1] => apple
[2] => melon
[3] => cherry
[4] => banana
)
Explanation
array_values() : Returns the values of the input array and indexes numerically.
array_filter() : Filters the elements of an array with a user-defined function (UDF If none is provided, all entries in the input table valued FALSE will be deleted.)

Search array input key in array

How do I find the keys array of disciplines that have appropriate values?
For Example:
$arr1 = [2, 4, 12];
$result = [...] // Var_dump in link
in_array($arr1, $result);
Regardless of their order, I need to see if there is a set of keys or a no.
But in_array() does not work.
Thanks!
Dump array
Update (01.03.2017)
This is my version of the solution to this problem
$findResult = array_filter($result, function($val)use($get){
$requiredDisciplines = [1, $get['disciplines']['second'], $get['disciplines']['third'], $get['disciplines']['four']]; // запрос
$activePriorities = [];
foreach ($val['disciplines'] as $discipline) {
if (in_array($discipline['id'], $requiredDisciplines)) {
$activePriorities[] = $discipline['priority'];
}
}
for ($i = 0; $i<3; $i++){
if(!in_array($i, $activePriorities))
return false;
}
/*if(in_array(0, $activePriorities) && in_array(1, $activePriorities) && in_array(2, $activePriorities) != true)
return false;*/
// print_r($activePriorities);
return true;
});
I've got a versatile one-liner that will give you all of the arrays containing matches. (so you can derive the keys or the count from that). (demo)
This function is only set to compare the values between the needle and the haystack, but can be set to search keys-values pairs by replacing array_intersect with array_intersect_assoc and adding ,ARRAY_FILTER_USE_BOTH to the end of the filter function (reference: 2nd snippet # https://stackoverflow.com/a/42477435/2943403)
<?php
// haystack array
$result[]=array(1,2,3,4,5,6,7,8,9,10,11,12);
$result[]=array(1,3,5,7,9,11);
$result[]=array(2,4,6,8,10,12);
// needle array
$arr1=array(2,4,12);
//one-liner:
$qualifying_array=array_filter($result,function($val)use($arr1){if(count(array_intersect($val,$arr1))==count($arr1)){return $val;}});
/*
// multi-liner of same function:
$qualifying_array=array_filter(
$result,
function($val)use($arr1){ // pass in array to search
if(count(array_intersect($val,$arr1))==count($arr1)){ // total pairs found = total pairs sought
return $val;
}
}
);*/
echo 'Total sub-arrays which contain the search array($arr1): ',sizeof($qualifying_array),"<br>";
echo 'Keys of sub-arrays which contain the search array($arr1): ',implode(',',array_keys($qualifying_array)),"<br>";
echo 'Is search array($arr1) in the $result array?: ',(sizeof($qualifying_array)?"True":"False"),"<br>";
echo "<pre>";
print_r($qualifying_array);
echo "</pre>";
The output:
Total sub-arrays which contain the search array($arr1): 2
Keys of sub-arrays which contain the search array($arr1): 0,2
Is search array($arr1) in the $result array?: True
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11] => 12
)
[2] => Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
[5] => 12
)
)

PHP create a Array

Does anyone know how I can create an Array?
$string = '3-1-0-1.11,3-1-1-1.12,3-1-2-1.13,3-1-3-1.14,3-2-0-1.02,3-2-1-1.03,3-2-2-1.04,3-2-3-1.05,3-2-4-1.06,3-3-0-3.23,3-3-1-3.24,3-3-2-3.25,3-3-3-3.26';
$array = explode(',', $string);
$last_entry = null;
foreach ($array as $current_entry) {
$first_char = $current_entry[2]; // first Sign
if ($first_char != $last_entry) {
echo '<h2>'. $first_char . '</h2><br>';
}
echo $current_entry[4] . '<br>';
$last_entry = $first_char;
}
I need an Array like this:
Array
(
[1] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[2] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
[3] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
The first number 3 and other numbers 3 after comma are not important.
Important numbers are second and third numbers in values of $array.
I need categories. Example: if the first (second) number is 1 create Category 1 and subcategory 1 where first (second) number actual is 1.
To create an an array, you need to declare it using array() feature. Below I have created a blank array.
$array = array();
An array with values looks like this
$array = array("string", "string2", "string3");
To add values in an array, you use the array_push method.
array_push($array, "string4");
On multidimensional arrays, declare the array then add the inner array, below is objct oriented
$array = array("string"=>array("innerstring", "innerstring2"), "string2" => array("innerstring3", "innerstring4"), "string3" => array("innerstring5", "innerstring6"));
and procedural
$array=array(array("string", "innerstring", "innerstring2",), array("string2", "innerstring3", "innerstring4"), array("string3", "innerstring5", "innerstring6"));
Try next script:
$string = '3-1-0-1.11,3-1-1-1.12,3-1-2-1.13,3-1-3-1.14,3-2-0-1.02,3-2-1-1.03,3-2-2-1.04,3-2-3-1.05,3-2-4-1.06,3-3-0-3.23,3-3-1-3.24,3-3-2-3.25,3-3-3-3.26';
foreach(explode(',', $string) as $tpl) {
$tpl = explode('-', $tpl);
$tpl[3] = explode('.', $tpl[3]);
$result[$tpl[1]][$tpl[2]][$tpl[3][0]] = !empty($tpl[3][1]) ? $tpl[3][1] : null;
}
var_dump($result);

Categories