I have the following array for tire sizes:
Array
(
[0] => 155
[1] => 70
[2] => 13
)
Array
(
[0] => 155
[1] => 80
[2] => 13
)
Array
(
[0] => 165
[1] => 65
[2] => 14
)
Array
(
[0] => 175
[1] => 65
[2] => 14
)
Array
(
[0] => 175
[1] => 70
[2] => 13
)
Array
(
[0] => 175
[1] => 70
[2] => 14
)
and so on. Now I am creating a drop down so people can select the tire size they are searching for. so here is my PHP code:
include 'database.php';
$result = $mysqli->query('SELECT DISTINCT SKU_SIZE FROM SKU_DATA WHERE SKU_BRANDNAME = "'.$brand.'" ORDER BY SKU_SIZE');
while( $row = $result->fetch_assoc()){
$sku_size = $row['SKU_SIZE'];
$chars = preg_split('/[^\d]/', $sku_size, -1, PREG_SPLIT_NO_EMPTY);
echo "<option>".$chars[0]."</option>";
}
Now that code is just showing the first number in each array, for the very first drop down they select.
Right now it is showing 155, 155, 165, 175, 175 - and what I want it to do is just show the unique values so it would just show 155, 165, 175.
Update: Thanks! I got that part working. One quick question.. the order is not quite right, not sure what I am doing wrong. Here is a preview:
Create an array and check to see if each value is in the array before outputting it. If it is not in the array, add it in before outputting.
include 'database.php';
$result = $mysqli->query(
'SELECT DISTINCT SKU_SIZE
FROM SKU_DATA WHERE SKU_BRANDNAME = "'.$brand.'"
ORDER BY SKU_SIZE'
);
$seen = array();
while( $row = $result->fetch_assoc()){
$sku_size = $row['SKU_SIZE'];
$chars = preg_split('/[^\d]/', $sku_size, -1, PREG_SPLIT_NO_EMPTY);
if(in_array($chars[0], $seen))
continue;
$seen[] = $chars[0];
echo "<option>".$chars[0]."</option>";
}
You can remove any duplicate unique items from an array using the array_unique() function.
EG:
$arrays = array(1,2,3) + array(1,2,3);
print_r(array_unique($arrays));
// Will print just: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
use this:
while( $row = $result->fetch_assoc()){
$sku_size = $row['SKU_SIZE'];
$chars = preg_split('/[^\d]/', $sku_size, -1, PREG_SPLIT_NO_EMPTY);
$sizes[$chars[0]] = true;
}
ksort($sizes, SORT_NUMERIC);
foreach ($sizes as $size => $tmp){
echo "<option value=\"$size\">$size</option>";
}
Use a temporary array to store the numbers that have been echoed then.
Make a new array with just the first value
$diameter = array();
foreach ($tires as $tire) {
$diameter[] = $tire[0];
}
Then, use array_unique() to remove the duplicates, or only add them to $diameter if they are not already in there.
Then use that $diameter array to create the dropdown.
This has the advantage that you can also sort the $diameter array.
Related
My code looks like this:
$sql = $pdo->prepare('SELECT source_clicks,source_impr,source_spend FROM `statistic` WHERE camp_id =? AND date BETWEEN ? AND ?');
$sql->bindColumn('source_clicks',$source_clicks);
$sql->bindColumn('source_impr',$source_impr);
$sql->bindColumn('source_spend',$source_spend);
$sql->execute([$_POST['get_source_stat'],$date[0],$date[1]]);
while ( $sql->fetch()) {
$data = explode(',',$source_clicks);
print_r($data);
}
the output i get is:
Array ( [0] => 30 [1] => 30 [2] => 51 [3] => 108 ) Array ( [0] => 30 [1] => 30 [2] => 51 [3] => 228 )
i need to sum this arrays saving their keys and get something like this:
array ([0] => 60 [1] => 60 [2] => 102 [3] => 336)
Can you tell me how can i do it?
One way to do this would be to push each exploded $source_clicks value into an array, and then use array_sum and array_column to get the results e.g.
$data = array();
while ($sql->fetch()) {
$data[] = explode(',',$source_clicks);
}
$sums = array();
foreach (array_keys($data[0]) as $column) {
$sums[$column] = array_sum(array_column($data, $column));
}
print_r($sums);
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);
Ok, I've almost finished my script, but my output is not quite as supposed to be. Scripts task is to create all possible number combinations from the sum of all multidimensional array keys. Ive edited few scripts and combined in one script, but I cant seem to get desired output.
Ok, for example, lets say I have array like this one:
$test = array(0 => array(53, 22, 12),
1 => array(94, 84, 94),
2 => array(56, 45, 104)
);
Then I fetch array keys and store them in new array:
foreach ($test as $key => $row) {
$output[] = count($row);
}
for($keycount = 1; $keycount <= count($output); $keycount++){
$newarray[$keycount] = $keycount;
}
And then I count keys from newly created array, so the final combination is based on that number. In aforementioned example, I have 3 combinations, so the final array is supposed to look like this:
111
211
311
121
221
321
131
231
331
112
.
.
.
333
But with my script:
$arraycount = count($newarray);
$maxcombinations = pow($arraycount, $arraycount);
$return = array();
$conversion = array();
foreach ($newarray as $key => $value) {
$conversion[] = $key;
}
for ($i = 0; $i < $maxcombinations; $i++) {
$combination = base_convert($i, 10, $arraycount);
$combination = str_pad($combination, $arraycount, "0", STR_PAD_BOTH);
$return[$i][] = substr(strtr($combination, $conversion), 1, $arraycount);
}
echo "<pre>".print_r($return, true)."</pre>";
I'm getting output like this:
Array
(
[0] => Array
(
[0] => 111
)
[1] => Array
(
[0] => 121
)
[2] => Array
(
[0] => 131
)
[3] => Array
(
[0] => 211
)
[4] => Array
(
[0] => 221
)
[5] => Array
(
[0] => 231
)
[6] => Array
(
[0] => 311
)
You are using array like $return[$i][] thus you are getting your output as unexpected
Try
$return[$i] = substr(strtr($combination, $conversion), 1, $arraycount);
Instead of
$return[$i][] = substr(strtr($combination, $conversion), 1, $arraycount);
I have an array with some values in different spots. I want to check if there is a value in the index then put it in a new array starting at 0 then put in index 1 then next value at index 2 and so on. I need to shorten it and move them all to the left really.
Array ( [0] => 53 [1] => [2] => 55 [3] => 76 [4] => [5] => [6] => [7] => )
The new array would be:
newArray ( [0] => 53 [1] =>55 [2] => 76)
Maybe something like this:
for ($i=0; $i < sizeof($questionWorth); $i++)
{
if($questionWorth[$i] has a value)
{
put it in new array starting at index zero
then increment the index of new array
}
}
To only get values that is not NULL or empty you could use array_filter() and array_values() like this:
$array = array(76, NULL, NULL, 56);
// remove empty values from array, notice that since no callback
// is supplied values that evaluates to false will be removed
$array = array_filter($array);
// since array_filter will preserve the array keys
// you can use array_values() to reindex the array numerically
$array = array_values($array);
// prints Array ( [0] => 76 [1] => 56 )
print_r($array);
You can use
array_filter($yourArray)
It will remove all empty values for you
Try array_filter which makes exactly this
var_dump(array_filter(array(0 => 55, 1 => 60, 2 => null)))
If you want to check if an index has a value, do this :
$variable = array ([0] => 53, [1] => , [2] => 55, [3] => 76, [4] => , [5] => , [6] => , [7] => )
foreach ($variable as $key => $value) {
var_dump($key.' => '.$value);
}
It's as simple as this: if ( $array [$i] ), and then put the value in another array with another counter that starts from 0.
$array = array(76, NULL, NULL, 56);
$count = 0;
for ($i=0; $i < sizeof($array); $i++)
{
if($array[$i])
{
$arr[$count] = $array[$i];
$count++;
}
};
print_r($array);
print_r($arr);
Good morning!
I'm using a SQL query to get category IDs from posts. Several post can have the same ID if they are from the same category. This is my array:
Array (
[0] => stdClass Object ( [catid] => 48 )
[1] => stdClass Object ( [catid] => 77 )
[2] => stdClass Object ( [catid] => 57 )
[3] => stdClass Object ( [catid] => 57 )
[4] => stdClass Object ( [catid] => 38 ))
Now I need to get the sum of all different catid's (in this case 4). I could achieve this by using "distinct" or a "group by" in my SQL, anyway I didnt because they mess up my results.
The problem is, that some catids should be replaced. For example is catid 48 'HDDs' and 77 is 'Storage' so 48 should be a part of 77 (So the sum is now 3). I already got an array of the catids that should be merged:
$catMergeArray = array(
38=>74,36=>46,34=>72,37=>73,40=>76,39=>75,103=>85,102=>81,53=>82,51=>80,50=>79,
55=>90,56=>91,57=>92,58=>93,55=>90,104=>68,106=>69,65=>70,66=>86,108=>95,48=>77,
172=>171,105=>170,111=>169);
by using
foreach ($result as $cats) {
$newarray[] = $cats->catid;
}
I changed my Array to
$newarray = Array ([0] => 48 [1] => 77 [2] => 57 [3] => 57 [4] => 38)
Then (like mentioned in $catMergeArray where it says 48=>77) remove the [0] => 48 value so the result should be $catids = Array ([0] => 77 [1] => 57 [2] => 38) so I can count the elements and have 3 as result to work with.
ummm have you tried using array_unique? http://php.net/manual/en/function.array-unique.php
or you can also group them into a unique array index like this:
$summary = array();
foreach( $catids as $catid => $cat){
$summary[$catid][] = $cat;
}
It'd probably be better to do it in php. Here's a rough idea and some pseudocode.
probably not very efficent nor elegent, but it should work.
$list_of_id = mysqli_fetch_array(query("distinct catId"));
for($i=0;$i<count($list_of_id);$i++){
$parent_cat_id = $catMergeArray[$list_od_id[$i]];
if ($parent_cat_id > 0){
for($j=0;$j<count($list_of_id);$j++){
if($list_of_id[$j]==$parent_cat_id){
unset($list_of_id[$i]);
break;
}
}
}
}
echo count($list_of_id);
EDIT: since OP doesn't seems to understand pseudocode...I'll make a php version... Surely it's clear enough?
Thanks for all the help. I now solved my problem with this:
foreach ($result as $value) {
$intArray[] = $value->catid;
}
$uniqueArray = array_unique($intArray);
$cUniqueArray = count($uniqueArray);
$cma = array(48=>77);
$lenght = $cUniqueArray-1;
for($i = 0; $i <= $lenght; $i++){
$key = array_search($uniqueArray[$i],$cma);
if(is_int($key)){
$uniqueArray[$i] = $key;
}
}