turn 2d array into 1d PHP - php

I have an array (sample below) containing 3 arrays with very similar content. I was wondering if there was an easy way to put everything into one array, rather than 3 separate ones. Everything I try seems to just overwrite the data and I'm left with the info from the 3rd array only.
array
0 =>
array
'america' => int 19
'music' => int 6
'states' => int 5
'bank' => int 5
1 =>
array
'america' => int 19
'home' => int 3
'society' => int 2
'writers' => int 2
2 =>
array
'america' => int 19
'lutheran' => int 4
'church' => int 4
'national' => int 4
'cruises' => int 3
Ideally the end result will look like this:
array
'america' => int 19
'music' => int 6
'states' => int 5
'bank' => int 5
'america' => int 19
'home' => int 3
'society' => int 2
'writers' => int 2
'america' => int 19
'lutheran' => int 4
'church' => int 4
'national' => int 4
'cruises' => int 3
Any solutions? thanks

You won't be able to have an associative array with the same key values (i.e. 3 keys of "America", etc.). When you try to set those keys in the array, you will simply overwrite the previous value.

I was wondering if there was an easy way to put everything into one
array
Short answer is no.
You could make a class with a variable for the original int and a value for the number of times it occurs and then have the array point to objects of the class. Same idea could be implemented using a linked list, though both ways are more complex than what you already have.
For example the key 'america' would point to an object with one variable set to 19 and the other variable set to 3.
Question: do you need to know which of the original arrays a value comes from? If yes then what I mentioned above would loose that property.

http://php.net/manual/en/function.array-merge.php

function array_2d_to_1d($array)
{
$my_val=array();
$my_key=array();
foreach ($array as $value) {
array_push($my_val, $value['cat_title']);
array_push($my_key, $value['cat_id']);
}
$result = array_combine($my_key, $my_val);
return $result;
}

Related

PHP - sorting and merging arrays

I have a site with a search feature but am trying to improve the search for a fuzzy search.
So far what I've done is query all of my products and use similar_text() to see how close they are to the actual search term
I then create an array where the key is how similar it is and the value is the product id. I then sort this by the key/similarity.
$term = $_GET['search_term'];
$similar_array = [];
$sql = "SELECT * FROM products";
$stmt = DB::run($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$id = $row["pro_id"];
$result = $row['product'];
similar_text($term,$result,$similarity);
$similar_array[$similarity][] = $id;
}
$closest_match = array_keys($similar_array);
rsort($closest_match);
$match_count = count($closest_match);
for($i=0; $i<$match_count; $i++){
var_dump($similar_array[$closest_match[$i]]);
}
This gives me something like:
array (size=9)
0 => int 28
1 => int 1628
2 => int 1665
3 => int 1666
4 => int 1667
5 => int 1668
6 => int 1669
7 => int 1670
8 => int 1671
C:\wamp64\www\V2\search.php:65:
array (size=2)
0 => int 37
1 => int 38
C:\wamp64\www\V2\search.php:65:
array (size=1)
0 => int 481
C:\wamp64\www\V2\search.php:65:
array (size=3)
0 => int 27
1 => int 1009
2 => int 1620
C:\wamp64\www\V2\search.php:65:
array (size=14)
0 => int 30
1 => int 104
2 => int 131
3 => int 134
4 => int 168
5 => int 169
6 => int 170
7 => int 557
8 => int 1011
9 => int 1014
10 => int 1661
11 => int 1662
12 => int 1663
13 => int 1664
I have a show_products() function that I just need to pass an array of ID's, so what I want to do now is take the multiple arrays of ID's from above and merge it in this specific order and then crop the array to a certain number so it won't have every product but will cut off after so many results.
As per your query you can use follow below steps
you merge all array by array_merge
sort the array using rsort (highest merge come first);
for display you can use array_slice or array_chunk

Why an array key-value sets in the end of an array instead of a specific place?

to be precise I have:
$foo = [1, 4, 1, 5, 8, 1, 3, 5, 1, 4, 1, 3, 7, 2];
sort($foo);
$bar = array_count_values($foo);
for ($i = 1; $i < count($bar); $i++) {
if (!isset($bar[$i])) {
$bar[$i] = 0;
}
}
Actual result:
array (size=8)
1 => int 5
2 => int 1
3 => int 2
4 => int 2
5 => int 2
7 => int 1
8 => int 1
6 => int 0
Expected result:
array (size=8)
1 => int 5
2 => int 1
3 => int 2
4 => int 2
5 => int 2
6 => int 0
7 => int 1
8 => int 1
Why my key value pair 6 => 0 appears in the bottom of an array instead of a specific place?
It's because you are appending a new value to the array.
To "fix" that, use ksort($bar);.
As #Rob Ruchte wrote in comments, the array returned by array_count_values() is associative, which means there is no order with the keys.
http://php.net/manual/en/function.array-count-values.php
Returns an associative array of values from array as keys and their count as value.

PHP trader_stochrsi returns false

I am trying to pass variables to the PHP pecl extension's 'trader' project's trader_stochrsi() function.
here is my use example :
$stochrsi = trader_stochrsi(array(5.5), 14, 3, 3);
var_dump($stochrsi);
I get the following read-out in the var_dump :
bool(false)
--Any thoughts as to why this may be happening?
Thanks,
GS
You are specifying 14 intervals the function must have to be able to give an RSI value, but your array only contains one interval i.e. 5.5.
You should put 15 items in your array. It will use the first 14 to calculate a value and output it for 16th interval.
For me it works after 19 elements:
array (size=19)
0 => float 1.298E-5
1 => float 1.246E-5
2 => float 1.129E-5
3 => float 1.091E-5
4 => float 1.015E-5
5 => float 1.075E-5
6 => float 1.056E-5
7 => float 1.046E-5
8 => float 1.07E-5
9 => float 1.046E-5
10 => float 1.113E-5
11 => float 1.163E-5
12 => float 1.216E-5
13 => float 1.253E-5
14 => float 1.295E-5
15 => float 1.356E-5
16 => float 1.285E-5
17 => float 1.43E-5
18 => float 1.426E-5
->>>
array (size=2)
0 =>
array (size=1)
18 => float 100
1 =>
array (size=1)
18 => float 100

array_rand returning same value

I am having a problem returning random array keys if the specified number of entries is the same as the number of items in the array.
$rdm = array_rand($similar_product_array, 4);
will always return key values 0, 1, 2, 3 if there is 4 items in the array.
for example:
// Items in array
array (size=4)
0 => string 'Batman Heroes Edition Nendoroid' (length=31)
1 => string 'Oberyn' (length=6)
2 => string 'White Walker' (length=12)
3 => string 'Avengers Age of Ultron Hulk' (length=27)
// "randomly" generated array keys is always 0 , 1, 2, 3
array (size=4)
0 => int 0
1 => int 1
2 => int 2
3 => int 3
however, if i have:
$rdm = array_rand($similar_product_array, 3);
// Returns randomly as expected
array (size=3)
0 => int 0
1 => int 2
2 => int 3
it will return randomly generated keys as it should.
What could i be doing wrong here?
You misunderstood purpose of array_rand() function, it is supposed to give you random entries from array, but not in random order. That means that if you are asking for 4 random items from array with 4 items, it will always return all the items (in the original order).
If you just need to change randomly the order of array entries, use shuffle() function, for example in this way:
$array_copy = $array;
shuffle($array_copy);
$rdm = array_rand($array_copy, <how_many_you_need>);

Count and sum multidimensional array php

I have an array like this one:
array (size=1)
0 =>
array (size=33)
0 => int 126
1 => int 43
2 => int 4
3 => int 0
4 => int 3
5 => int 3
6 => int 30
7 => int 15
8 => int 22
9 => int 27
10 => int 22
11 => int 46
12 => int 0
13 => int 8
14 => int 14
15 => int 8
array (size=1)
1 =>
array (size=33)
0 => int 273
1 => int 3
2 => int 4
3 => int 28
4 => int 36
5 => int 19
6 => int 142
7 => int 81
8 => int 59
9 => int 71
10 => int 88
11 => int 47
12 => int 42
13 => int 0
14 => int 12
15 => int 97
(of course it is way longer)
and I need both to sum all the value with the same key and count how many values with the same key are >0 (cause I have to find the avarage of all the numbers >0
My expected result is
0=>
'sum' => 399
'count'=>2
1=>
'sum' =>46
'count'=>2
how can I create this array?
There's an inbuilt function in PHP to count the sum of all the elements of an array. Here, this will give you your expected output :
<?php
$arr = [[10, 20, 30, 40], [10, 20, 30], [10, 20, 30, 4]];
// Let the magic happen...
$yourArray = array_map(function ($el){ return ["sum" => array_sum($el), "count" => count($el)]; }, $arr);
print_r($yourArray);
?>
I have thought about this, and came up with a solution (I think...), it comes in the form of a function and it goes like this:
function getSumAndCount(array $arr)
{
$sum = 0;
$count = 0;
foreach ($arr as $v)
{
$count++;
if (is_array($v))
{
$next = getSumAndCount($v);
$count += $next['count'];
$sum += $next['sum'];
}
else
{
!is_numeric($v) ?: $sum += $v;
}
}
return [ 'sum' => $sum, 'count' => $count ];
}
It has a recursive check to if the array is multidimensional, checks if the value is numeric and sets the count and sum in a return array.
I haven't tested this properly as yet, but please test and let me know if you get the desired output.
EDIT
I will extend this to count dupe keys soon

Categories