build an array multidimensional from query results - php

I have this code that outputs something like:
Array (
[0] => 15
[1] => 13
[2] => 16
[3] => 16
[4] => 10
[5] => 10
[6] => 13
[7] => 13
)
But, i want this structure:
Array (
[0] => Array ( [0] => 15, [1] => 13, [2] => 16, [3] => 16)
[0] => Array ( [0] => 10, [1] => 10, [2] => 13, [3] => 13)
)
This didn't solve : $score[] = array($score_bd);. Any idea ?
php code
$i =0;
foreach ($arr_user_apply as $val) {
$new_val = array($val);
$arr[$i] = array_merge($str, $new_arr_tags_ids, $new_val, $new_id_oferta);
$sql = $db -> prepare("
query
"
);
call_user_func_array(array($sql, "bind_param"), $arr[$i]);
$sql -> execute();
$sql -> bind_result($score_bd);
while ($sql -> fetch()) {
$score[] = $score_bd;
};
$i++;
}

If you're just trying to add another dimension to your first array by placing the key value of the key into the new array something like this might work:
$firstArray = Array ( 0 => 15, 1 => 15, 2 => 13, 3 => 13, 4 => 16, 5 => 16, 6 => 10, 7 => 10, 8 => 13, 9 => 13, 10 => 6, 11 => 6 );
$newArray = Array();
foreach($firstArray as $key => $value)
{
$newArray[] = Array($key => Array($value));
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
EDIT:
if that's one dimension too deep, then just try this:
$firstArray = Array ( 0 => 15, 1 => 15, 2 => 13, 3 => 13, 4 => 16, 5 => 16, 6 => 10, 7 => 10, 8 => 13, 9 => 13, 10 => 6, 11 => 6 );
$newArray = Array();
foreach($firstArray as $key => $value)
{
$newArray[$key] = Array($value);
}
echo "<pre>";
print_r($newArray);
echo "</pre>";

$arr = array ( 0 => 15 ,1 => 15, 2 => 13, 3 => 13 ,4 => 16 ,5 => 16 ,6 => 10, 7 => 10);
foreach($arr as $key=>$value){
$new_arr[$key][0] = $value;
}

i am not sure if the question is clear, but this works properly to me
$score = array();
while ($sql -> fetch()) {
$score[] = $score_bd;
};
$new_score[] = array($score);
Array (
[0] => Array ( [0] => Array ( [0] => 15 [1] => 13 [2] => 16 ) )
[1] => Array ( [0] => Array ( [0] => 10 [1] => 13 [2] => 6 ) )
)

Related

How to remove array if all keys acquired by another array in multidimensional array PHP

can you please help me out in this.
What will be the function to remove 4th and 5th array in PHP.
Array
(
[0] => Array
(
[10] => 98
[11] => 1
[433438] => 8
)
[1] => Array
(
[10] => 98
[11] => 1
[433438] => 1
)
[2] => Array
(
[13] => 98
[11] => 2
[433438] => 8
)
[3] => Array
(
[14] => 98
[11] => 2
[433438] => 1
)
[4] => Array
(
[10] => 18
[11] => 1
)
[5] => Array
(
[14] => 18
[11] => 2
)
)
Thanks in Advance.
$base = [
[10 => 98, 11 => 1, 433438 => 8],
[10 => 98, 11 => 1, 433438 => 1],
[13 => 98, 11 => 2, 433438 => 8],
[14 => 98, 11 => 2, 433438 => 1],
[10 => 18, 11 => 1],
[14 => 18, 11 => 2],
];
$invalid = [];
for ($i = 0; $i <= count($base) - 1; $i++) {
for ($j = 0; $j <= count($base) - 1; $j++) {
$refCount = count($base[$j]);
$interSectCount = count(array_intersect(array_keys($base[$i]), array_keys($base[$j])));
if (count($base[$i]) !== $refCount && $interSectCount === $refCount) {
$invalid[] = $j;
}
}
}
foreach ($invalid as $item) {
unset($base[$item]);
}
You can use array_pop() function which eliminates the last element of the array but if you use it two times you will get the desired result as
<?php
$data = [
1 => [1,2],
2 => [1,3],
3 => [1,4]
];
$value = array_pop($data);
$value = array_pop($data);
?>
Output
Array
(
[1] => Array
(
[0] => 1
[1] => 2
)
)

How to randomly get min value from Array?

I have an array with multiple similar minimum value.
May I know how to randomly get one of the minimum value?
Here is my sample code:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
$intNo = $b[0];
May I know how to get min value list (3 => 12, 4 => 12,5 => 12,10 => 12) and randomly pick one of them so that I can set in $intNo?
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
// Taking a random KEY from $b
$key = array_rand($b);
// Taking a KEY from $aryNo which is under `$key`
echo $b[$key];
// Taking a VALUE from `$aryNo` which is under `$b[$key]`
echo $aryNo[$b[$key]];
The fiddle.
Try something like this:
$aryNo = [34,34,34,51,12,12,12,56,876,453,43,12];
foreach($aryNo as $a) {
$finalArray[$a][] = $a;
}
print("<pre>".print_r($finalArray,true)."</pre>");
$minKey = min(array_keys($finalArray));
print("<pre>".print_r($finalArray[$minKey],true)."</pre>");
$randIndex = array_rand($finalArray[$minKey]);
print_r("Key: ".$randIndex.", ".$finalArray[$minKey][$randIndex]);
First print prints:
Array
(
[34] => Array
(
[0] => 34
[1] => 34
[2] => 34
)
[51] => Array
(
[0] => 51
)
[12] => Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
[56] => Array
(
[0] => 56
)
[876] => Array
(
[0] => 876
)
[453] => Array
(
[0] => 453
)
[43] => Array
(
[0] => 43
)
)
Than you select min key, and that prints this:
Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
At the end you pick random key from this array and print the value:
Key: 2, Value: 12
`<?php
$sortArr = array();
$aryNo = array(0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,5 =>
12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,10 => 12);
asort($aryNo);
$aryNo = array_values($aryNo);
print_r($aryNo);
echo $aryNo[0];
?>`
I found that if I using shuffle(); also work for me.
Here is my example:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$aryNo2 = array_keys($aryNo, min($aryNo));
shuffle($aryNo2); //
$intWinNo = $aryNo2[0];
Thanks #u_mulder suggestion & answer.

I want to add one array key and value with another array values

I want to add one array key and value with another array's value.
If I have arrays as follow:
Array ( [6] => 12 [8] => 9 [10] => 11 )
Array ( [6] => 70 [8] => 10 [9] => 35 [10] => 25 [11] => 25 [12] => 2 )
For example (6 => 12 when processed result in 70 + 2 )
This is the expected output with the two arrays above:
array ([0] => 72 [1] => 45 [2] => 50 )
Thanks In Advance
Use the following simple solution:
$arr1 = [6 => 12, 8 => 9 , 10 => 11];
$arr2 = [6 => 70, 8 => 10, 9 => 35, 10 => 25, 11 => 25, 12 => 2];
$result = [];
foreach ($arr1 as $k => $v) {
if (isset($arr2[$k]) && isset($arr2[$v])) {
$result[] = $arr2[$k] + $arr2[$v];
}
}
print_r($result);
The output:
Array
(
[0] => 72
[1] => 45
[2] => 50
)
$array1 = Array ( 6 => 12, 8 => 9, 10 => 11 );
$array2 = Array ( 6 => 70, 8 => 10, 9 => 35, 10 => 25, 11 => 25, 12 => 2 );
$newarray = array();
foreach ($array1 as $key => $item) {
$newarray[] = $array2[$item] + $array2[$key];
}
var_dump($newarray);

Calculate the sum depending on column php

I have the following array :
Array
(
[0] => Array
(
[year_col] => 2016
[week_col] => 21
[total] => 1
)
[1] => Array
(
[year_col] => 2016
[week_col] => 22
[total] => 2
)
[2] => Array
(
[year_col] => 2016
[week_col] => 22
[total] => 2
)
[3] => Array
(
[year_col] => 2016
[week_col] => 22
[total] => 3
)
[4] => Array
(
[year_col] => 2016
[week_col] => 23
[total] => 2
)
)
I neet to get the sum of total for each week_col. In this case I want to get :
21 => 1, 22 => 7, 23 => 2
I try :
$sum = 0;
foreach($array as $draw){
$a_filter_draws[$draw['week_col']] = array(
'total_a' => $sum+=$draw['total']
);
}
Can you help me please ? It don't return the good sum. Thx in advance and sorry for my english
$a_filter_draws = array();
foreach($array as $draw){
if (!isset($a_filter_draws[$draw['week_col']])) {
$a_filter_draws[$draw['week_col']] = 0;
}
$a_filter_draws[$draw['week_col']] += $draw['total'];
}
Try this:
<?php
$arrSum = array();
foreach($array as $draw){
if(isset($arrSum[$draw['week_col']])) {
$arrSum[$draw['week_col']] += $draw['total'];
} else {
$arrSum[$draw['week_col']] = $draw['total'];
}
}
?>
<?php
$arr=Array
(Array
(
"year_col" => 2016,
"week_col" => 21,
"total" => 1
)
,Array
(
"year_col" => 2016,
"week_col" => 22,
"total" => 2
)
, Array
(
"year_col" => 2016,
"week_col" => 22,
"total" => 2
)
, Array
(
"year_col" => 2016,
"week_col" => 22,
"total" => 3
)
,Array
(
"year_col" => 2016,
"week_col" => 23,
"total" => 2
)
);
$sum = 0;
$total=0;
foreach($arr as $draw){
$sum+=$draw["week_col"];
$total+=$draw["total"];
}
echo $sum;
echo "<br>";
echo $total;
?>

GET Max array from two array PHP

I want which is the biggest array from following array.
[13] => Array
(
[0] => 1
[1] => 3
[2] => 9
)
[15] => Array
(
[0] => 1
[1] => 5
[2] => 8
)
[33] => Array
(
[0] => 1
[1] => 9
[2] => 13
)
I want a code that would return last array with key 33.
Please Help.
Use max to get the maximum from the keys of your array
$yourarray=array(13 => Array
(
0 => 1,
1 => 3,
2 => 9,
),
15 => Array
(
0 => 1,
1 => 5,
2 => 8,
),
33 => Array
(
0 => 1,
1 => 9,
2 => 13,
));
$arr=max(array_keys($yourarray));
print_r($yourarray[$arr]);
Output:
Array
(
[0] => 1
[1] => 9
[2] => 13
)
This should do the trick...
<?php
$tests = array(
13 => array(1,3,9),
15 => array(1,5,8),
33 => array(1,9,13)
);
$array_totals = array_map('array_sum', $tests);
krsort($array_totals);
$maxArray = each($array_totals);
var_dump($maxArray);
Gives
array (size=4)
1 => int 23
'value' => int 23
0 => int 33
'key' => int 33
Not the most beautiful thing, but readable ;)
$tests = array(
13 => array(1,3,9),
15 => array(1,5,8),
33 => array(1,9,13)
);
$max = -1;
$max_key = -1;
foreach ($tests as $k => $v) {
$cur_max = max($v);
if ($cur_max >= $max) {
$max = $cur_max;
$max_key = $k;
}
}
echo "max: $max; max_key: $max_key";
Gives:
max: 13; max_key: 33
To make it more beautiful: use array_map and sorting.

Categories