How do I extracting the multidimensional array value in php? [duplicate] - php

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;
}
}

Related

PHP: Find highest index of numeric array that has missing elements [duplicate]

This question already has answers here:
Search for highest key/index in an array
(7 answers)
Closed 4 years ago.
Say I have the following array:
$n[5] = "hello";
$n[10]= "goodbye";`
I would like to find out the highest index of this array.
In Javascript, I could do $n.length - 1 which would return 10.
In PHP, though, count($n) returns 2, which is the number of elements in the array.
So how to get the highest index of the array?
Use max() and array_keys()
echo max(array_keys($n));
Output:-https://eval.in/997652
$n = [];
$n[5] = "hello";
$n[10]= "goodbye";
// get the list of key
$keyList = array_keys($n);
// get the biggest key
$maxIndex = max($keyList);
echo $n[$maxIndex];
output
goodbye

PHP - Two JSON lists in one (two-dimensional) [duplicate]

This question already has answers here:
How to create key value pair using two arrays in JavaScript?
(7 answers)
Closed 5 years ago.
I'm trying using PHP to bring the two JSON lists into one, as follows:
list1
[1,2,3,4,5]
list2
["a","b","c","d","e"]
How to join lists to get:
[[1,"a"],[2,"b"],[3,"c"],[4,"d"],[5,"e"]]
How to link two lists to one, as in a given example?
You need to loop through the arrays, and create a new one with the combined values:
<?php
$arr1 = [1,2,3,4,5];
$arr2 = ["a","b","c","d","e"];
$result = [];
foreach ($arr1 as $i => $a) {
$result[] = [$a, $arr2[$i] ?? ''];
}
?>

loop through a multi-dimensional array and echo the value in PHP [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have a multi-dimensional array that takes the form:
array = [value1, some number],[value2, some number]...
I need to loop through the array and echo the value followed by a tokenizer so the final output looks like:
value1!##$value2!##$
I know that I have to concatenate the return value with ."!##$" but I do not know how to loop through the array. Can anyone offer some help.
My array is being made from a MySQL Query as follows:
while($row = $results -> fetch_assoc()) {
$return_array[] = array(
$row['uid'],($row['column1] - $row['column2']));
}
Then I am perfoming a usort on the array
To be simple enough, you can use implode and array_column:
$array = [['value1', 123], ['value2', 234]];
echo implode('!##$', array_column($array, 0)) . '!##$';
This gives:
value1!##$value2!##$
Explanation:
implode - Joins array values using some specified value, here !##$
array_column - implode accepts one dimensional array, also you want only the first index of the array to be joined, so creating an array with just first index.

convert string to array and get value in php [duplicate]

This question already has answers here:
PHP string to array
(4 answers)
Closed 8 years ago.
I have a complete string how can i get some part of it and insert into an array this is my string in php
[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]
i want something similar to this
$value1 = $array[0];
// {"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"}
is this possible to get each value like this
$value1 = $array[0]['albumid'];
// ASaBFzCtl8
Yes use json_decode()
$j = '[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]';
$data = json_decode($j,true);
You can use loop to read the data as
foreach($data as $key=>$val){
echo $val["albumid"]."<br />";
}
Above will just get the albumid you can read whatever you want from this array.

PHP - multidimensional array from arrays [duplicate]

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];
}

Categories