This question already has answers here:
Looping a multidimensional array in php
(3 answers)
Output multidimensional array with keys and values
(3 answers)
Closed 4 months ago.
How do I get the value of the associative arrays for the year and quarter in a loop in php? I want to extract the numbers like 2021 4, the other value doesn't matter.
$this->financials[2021][4] = 5;
$this->financials[2022][1] = 7;
$this->financials[2022][2] = 9;
$this->financials[2022][3] = 11;
I attempted this method, but it didn't work, but hopefully gives you a better idea of what I am trying to achieve. I was hoping for the result e.g. 20214
foreach($this->financials as $f[$y][$q]) {
echo $y.$q;
}
$q in your foreach loop is an associative array so you have to break it down with another loop.
foreach ($this->financials as $year => $quarterArray) {
foreach ($quarterArray as $quarter => $value) {
echo $year.$quarter;
}
}
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 3 years ago.
im trying to remove from my string the comma separeted and multiples strings and then to count strings as total, i have no luck so.. any help will be apreciated, thanks!
Example names [alex,daniel,obama,alex,alex,alex,diana] = 7 names and without duplicates = 4 as total. This i'm trying to achive.
Here's the Php code:
$q = mysqli_query($db,"SELECT DISTINCT(names) FROM users WHERE uid = 1 ");
while ($data = mysqli_fetch_array($q)) {
$names = count($data['names']);
echo implode(',', array_keys(array_flip(explode(',',$names))));
}
If $data['names'] is a comma separated list of names, you can get only the unique values like this:
$names = array_unique(explode(',', $data['names']));
$count = count($names);
$names = implode(',', $names);
echo "$count names: $names\n";
For your sample data, this gives
4 names: alex,daniel,obama,diana
Demo on 3v4l.org
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 3 years ago.
I have some comments from customers, and I want to sort them by popularity. I can put them in an array to count each comment, but I want to order the comments by said count, how is this achieved?
I can make the arrays like this:
$arr['comment1'] = 4;
$arr['comment2'] = 2;
$arr['comment3'] = 6;
Or
$arr[] = array('comment'=>'comment1','count'=>4);
$arr[] = array('comment'=>'comment2','count'=>2);
$arr[] = array('comment'=>'comment3','count'=>6);
So I would like to re-order the array into this order - comment3, comment1, comment2 - i.e. in order of count. Is this possible?
You probably need asort() or arsort(). This will sort the array while keeping the keys with the proper values.
Ascending: $sorted = asort($arr);
Descending: $sorted = arsort($arr);
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
For Example:
$array_1 = [1,'a','b','c','d','e','f','g','h','i'];
$array_2 = [2,'aa','bb','cc','dd','ee','ff','gg','hh','ii'];
$array_3 = [3,'aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii'];
$array_4 = [4,'aaaa','bbbb','cccc','dddd','eeee','ffff','gggg','hhhh','iiii'];
$array_5 = [5,'aaaaa','bbbbb','ccccc','ddddd','eeeee','fffff','ggggg','hhhhh',
'iiiii'];
Using PHP Script I want to convert it into
$array_1 = [1,2,3,4,5];
$array_2 = ['a','aa','aa a','aa aa','aa aa a'];
$array_3 = ['b','bb','bb b','bb bb','bb bb b'];
$array_4 = ['c','cc','cc c','cc cc','cc cc c'];
$array_5 = ['d','dd','dd d','dd dd','dd dd d'];
$array_6 = ['e','ee','ee e','ee ee','ee ee e'];
.
...
$array_10 = ['i','ii','iii','iiii','iiiii'];
Can anyone provide me a solution for that.
Simply you can use array_map like as
$array_1 = [1,'a','b','c','d','e','f','g','h','i'];
$array_2 = [2,'aa','bb','cc','dd','ee','ff','gg','hh','ii'];
$array_3 = [3,'aaa','bbb','ccc','ddd','eee','fff','ggg','hhh','iii'];
$array_4 =
[4,'aaaa','bbbb','cccc','dddd','eeee','ffff','gggg','hhhh','iiii'];
$array_5 =
[5,'aaaaa','bbbbb','ccccc','ddddd','eeeee','fffff','ggggg','hhhhh',
'iiiii'];
$result = array_map(null,$array_1,$array_2,$array_3,$array_4,$array_5);
print_r($result);
Demo
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
I've got 6 arrays - 1 with name and 5 with some properties - which should be assigned to that name. All values are of course in order. I'd like to make a 2-dimensional array with will be later put into CSV and the result should be as on the table here:
I guess that i have to do 2 loops here, but I can't make them work. How to construct such array?
Solution found
I've connected all arrays:
$final_array = array($nazwa_array,$new_ilosc_array,$new_koszt_array,$new_cena_lifo_array,$new_cena_fifo_array,$new_rodzaj_array);
I've found a matrix transposition function, which returns array in correct order:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$a = array();
foreach ( $names AS $key => $value ) {
$a[$key]['name'] = $value;
$a[$key]['property1'] = $value.'->'.$property1_array[$key];
$a[$key]['property2'] = $value.'->'.$property2_array[$key];
$a[$key]['property3'] = $value.'->'.$property3_array[$key];
$a[$key]['property4'] = $value.'->'.$property4_array[$key];
$a[$key]['property5'] = $value.'->'.$property5_array[$key];
}