How to Extract Values from php array - php

I'm trying to return the LARGEST numerical value associated with the 'reduced' array below. Easy enough if there is one value but many have two- as below. I'm using
$reduced_array = $data['rates'][1]['rates'][0];
but this only works in returning the first value. I need to return just the highest value however- so below it would be 8.
would something like
if(count($data['rates'][1]['rates']) > 2) {
***return largest value here***
work? I'm just not sure how to perform the asterisked task- maybe a for loop? here is the array.
Array
(
[rates] => Array
(
[0] => Array
(
[name] => Super Reduced
[rates] => Array
(
)
)
[1] => Array
(
[name] => Reduced
[rates] => Array
(
[0] => 5
[1] => 8
)
)
[2] => Array
(
[name] => Standard
[rates] => Array
(
[0] => 23
)
)
[3] => Array
(
[name] => Increased
[rates] => Array
(
)
)
[4] => Array
(
[name] => Parking
[rates] => Array
(
)
)
)
[disclaimer] => Rates data is based on information published by the European Commission, updated 1st January 2017.
)
Thanks for any help

You can use max() for that.
$array = array(
"rates" => array(
array(
"name" => "Super Reduced",
"rates" => array()
),
array(
"name" => "Reduced",
"rates" => array(
5,
8
)
)
)
);
echo max($array["rates"][1]["rates"]); // 8

Easiest solution will be using of max on inner array
example:
if(!empty($data['rates'][1]['rates'])) {
$maxvalue = max($data['rates'][1]['rates']);
}

Related

How to populate missing array data by flipping 2nd and 3rd level keys?

I have a multidimensional array and I need to ensure that paired/related subarrays are generated in the deepest level of my array.
The idea is that subarrays [65][1] and [155][1] should exist. At the same time, subarrays [65][2] and [155][2] should exist.
In other words, [nn][1] and [nn][2] must exist.
I would like to be able to automatically add subarrays which do not exist. I do not know how to do it with PHP.
So, I am looking for a code which goes through the array and creates subarrays which do not exist.
Here is an example.
Array (
[65] => Array (
[1] => Array (
[2] => Array (
[points] => 0000000600
[competition] => 0000000011
)
)
)
[155] => Array (
[1] => Array (
[2] => Array (
[points] => 0000000900
[competition] => 0000000011
)
)
[2] => Array (
[1] => Array (
[points] => 0000000750
[competition] => 0000000025
)
)
)
}
However, the subarray [65][2] does not exist.
As said, I am looking for a code which goes through the array and creates subarrays which do not exist.
The result should look like this:
Array (
[65] => Array (
[1] => Array (
[2] => Array (
[points] => 0000000600
[competition] => 0000000011
)
)
/* this should be added automatically */
[2] => Array (
[1] => Array (
[points] => 0000000000
[competition] => 0000000000
)
)
)
/* */
[155] => Array (
[1] => Array (
[2] => Array (
[points] => 0000000900
[competition] => 0000000011
)
)
[2] => Array (
[1] => Array (
[points] => 0000000750
[competition] => 0000000025
)
)
)
}
Notice that the default data is assign to a new subarray which is assigned 2nd and 3rd level keys using the 3rd and 2nd level keys respectively.
If I understood you correctly, all the elements in your main array are themselves arrays, and each of them must have both a [1] and a [2] element. You can check for this with the array_key_exists() function. Something like:
foreach($rg as $rgkey) {
if(!array_key_exists(1,$rg[$rgkey])) {
/* code to initialize $rg[$rgkey][1] */ }
if(!array_key_exists(2,$rg[$rgkey])) {
/* code to initialize $rg[$rgkey][2] */ }
}
Traverse the top two levels of your structure and then check for the expected third level key, if it doesn't exist, populate the new subarray with the default data using flipped keys.
Code: (Demo)
foreach ($rg as $id1 => $level2) {
foreach ($level2 as $id2 => $level3) {
$id3 = key($level3);
if (!isset($level2[$id3])) {
$rg[$id1][$id3][$id2] = [
'points' => '0000000000',
'competition' => '0000000000'
];
}
}
}
var_export($rg);
Output:
array (
65 =>
array (
1 =>
array (
2 =>
array (
'points' => '0000000600',
'competition' => '0000000011',
),
),
2 =>
array (
1 =>
array (
'points' => '0000000000',
'competition' => '0000000000',
),
),
),
155 =>
array (
1 =>
array (
2 =>
array (
'points' => '0000000900',
'competition' => '0000000011',
),
),
2 =>
array (
1 =>
array (
'points' => '0000000750',
'competition' => '0000000025',
),
),
),
)

php combine multidimensional arrays

I'm having trouble combining these two arrays so that the keys are kept together. The problem (I think) I'm having is that the arrays don't match in their structures, and the array keys are integers in one and names in the other. I feel like I need to have one array (feel free to correct me) so that I can display the prices coherently on the page, but I can't wrap my head around how to do it. I tried an array_merge, but it looses the indexed tlds sub-array:
$result = array();
foreach($cats[0]['domorder'] as $domorder) {
$result = array_merge($domorder, $prices[0]);
}
Maybe I can somehow (this isn't working either) add a 'price' sub-array that won't be overwritten?
$result = array();
$prc = array();
$prc['price'] = $prices[0];
foreach($prc as $p) {
$result = array_merge($p, $cats[0]['domorder'][0]);
}
Here's basically what I'm working with...my apologies if these are not formatted correctly for questions here.
Array 1, category definitions of hosting/domain name products:
Array
(
[0] => Array
(
[hosting] => Array
(
[0] => vpslinuxin
[1] => resellerhostinglinuxuk
[2] => resellerwindowshostinguk
........etc,etc.........
[34] => hosting
)
[domorder] => Array
(
[0] => Array
(
[dombiz] => Array
(
[0] => biz
)
)
[1] => Array
(
[dominfo] => Array
(
[0] => info
)
)
........etc,etc.........
Array 2, prices associated to the above categorized products:
Array
(
[0] => Array
(
[resellerhostinglinuxuk] => Array
(
[131] => Array
(
[renew] => Array
(
[1] => 43.19
)
[ssl] => 4.79
[add] => Array
(
[1] => 43.19
)
)
........etc,etc.........
[dombiz] => Array
(
[addtransferdomain] => Array
(
[1] => 10.69
)
[restoredomain] => Array
(
[1] => 69.95
)
[addnewdomain] => Array
(
[10] => 10.89
[9] => 10.89
)
........etc,etc.........
Anyone? I feel like this should be a fairly easy merge, but I can't figure out how to make it work.
Edit
Here's an example of how I think it should work:
Array
(
[0] => Array
(
[hosting] => Array
(
[vpslinuxin] => Array
(
[prices] => Array
(
[addons] => Array
(
.......
)
[plans] => Array
(
.......
)
)
)
)
[domorder] => Array
(
[0] => Array
(
[dombiz] => Array
(
[tlds] => Array
(
[0] => biz
)
[prices] => Array
(
[addtransferdomain] => Array
(
.......
)
[restoredomain] => Array
(
.......
)
[addnewdomain] => Array
(
.......
)
[renewdomain] => Array
(
.......
)
)
)
)
)
)
)
thanks for your help Michael but I managed to get it.
I was thinking too hard about it, so after dinner and some relaxing, I decided to simplify what I've been trying. There's no hard/fast rule saying that the two arrays need to be together - ultimately they're going to end up together anyway. So I just appended one to the other, defined by a 'product' and 'price' key:
$result = array();
$result[]['product'] = $cats[0];
$result[]['prices'] = $prices[0];

Rearrange array values according to another array values in php

I have got 2 arrays(One single and one multidimensional).
Single array "A" looks like
[questionid] => Array
(
[0] => 12
[1] => 13
[2] => 55
[3] => 15
[4] => 16
)
Multidimensional array "B" looks like
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 15
[answer] =>
)
[3] => Array
(
[quid] => 16
[answer] =>
)
[4] => Array
(
[quid] => 55
[answer] =>
)
)
Now I want the array B (quid) values to be rearranged depending upon the values from array A. So in array B the value of quid last element(55) is at the very end whereas in array A it is in 3rd position.
I want the array B look like this
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 55
[answer] =>
)
[3] => Array
(
[quid] => 15
[answer] =>
)
[4] => Array
(
[quid] => 16
[answer] =>
)
)
The code for multidimensional array is
$ansid = array
(
array
(
"quid" => 12,
"answer" => "AAA"
),
array
(
"quid" => 13,
"answer" => "neighbour"
),
array
(
"quid" => 15,
"answer" =>""
),
array
(
"quid" => 16,
"answer" =>""
),
array
(
"quid" => 55,
"answer" =>""
)
);
Not using array_walk() as to be mor demonstrative, you could just
$newB=array()
foreach ($arrayB as $b) $newB[$b['quid']]=$b;
$newA=array()
foreach ($arrayA as $k=>$v) $newA[$k]=$newB[$v]
//$newA has the required structure
With the user sort function:
$single_array = ...; // order by the index of this array
$mult_dim_array = ...; // to be ordered by the 'quid' value of the elements
function my_comp($a, $b) {
return array_search($a['quid'], $single_array ) - array_search($b['quid'], $single_array );
}
usort($mult_dim_array, "my_comp");
This will get the index on your first array to determine which element goes first or later. The function reads $single_array as a global variable (defined outside the function).
Documentation at http://php.net/manual/en/function.usort.php

Sort a multidimensional array in PHP

I have this multidimensional array, called $rent:
Array
(
[product2] => Array
(
[dates] => Array
(
[2013-07-25] => 2
[2013-07-23] => 1
[2013-07-21] => 3
)
)
[product3] => Array
(
[dates] => Array
(
[2013-07-24] => 5
[2013-07-22] => 4
[2013-07-20] => 3
)
)
[product1] => Array
(
[dates] => Array
(
[2013-07-29] => 1
[2013-07-28] => 2
[2013-07-27] => 2
)
)
)
I'd like to do a double sort:
First, by productX ascending
Then, for each product, by date of rent ascending
So that the resulting array would be:
Array
(
[product1] => Array
(
[dates] => Array
(
[2013-07-27] => 2
[2013-07-28] => 2
[2013-07-29] => 1
)
)
[product2] => Array
(
[dates] => Array
(
[2013-07-21] => 3
[2013-07-23] => 1
[2013-07-25] => 2
)
)
[product3] => Array
(
[dates] => Array
(
[2013-07-20] => 3
[2013-07-22] => 4
[2013-07-24] => 5
)
)
)
How can I reach this? Many thanks in advance
try this:
ksort($rent);
foreach($rent as &$item) {
ksort($item['dates']);
}
You can simply ksort the products, then iterate through them and use the same for the dates key.
ksort($products);
foreach($products as &$product)
ksort($product['dates']);
Where $products is the array you showed us. Note that you need to pass the value in the foreach loop as a reference (using the & operator) otherwise the changes won't be updated in the original array.
for my understanding of your problem; Nadh solution is almost there. but i believe you want ksort()
this is my corrections to Nadh answer
ksort($rent);
foreach($rent as $product => $dates) {
ksort($rent[$product]['dates']);
}
print_r($rent);

Merge a 3D Array with 2D Array Based on Shared Values

I need to merge a three-dimensional array with a two-dimensional array based on a shared value for 'id.'
In the example below, "George Washington" has an 'id' of 1. I need to append his 'value', which is found in the second array where 'id' is also 1.
The 'id' of "John Adams" is 2, which is not found in the second array. As a result, his 'value' needs to be set to 0 (or NULL).
The final result is a three-dimension array where 'value' has been added to each item in the original array.
Array #1
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => "George Washington"
)
[total] => 8
[average] => 2.5
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => "John Adams"
)
[total] => 6
[average] => 3.0
)
[2] => Array
(
[0] => Array
(
[id] => 5
[name] => "James Monroe"
)
[total] => 9
[average] => 2.0
)
)
Array #2
Array
(
[0] => Array
(
[id] => 1
[value] => 12
)
[1] => Array
(
[id] => 5
[value] => 18
)
)
Desired Result:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => "George Washington"
)
[total] => 8
[average] => 2.5
[value] => 12
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => "John Adams"
)
[total] => 6
[average] => 3.0
[value] => 0
)
[2] => Array
(
[0] => Array
(
[id] => 5
[name] => "James Monroe"
)
[total] => 9
[average] => 2.0
[value] => 18
)
)
What I've tried so far:
I separated all of the 'id' values from the first array into a new array named $ids. Then while looping through the items in the second array, I check to see whether the 'id' from the second array is found in the $ids array.
But then I'm stuck because I don't know how to specify which item in the first array needs to receive the new 'value'. Also, this seems like a messy solution because of the overhead involved in creating the $ids array.
Didn't even realize you had tried to list the IDs and such before I was done writing this, but here you go anyway. Good luck!
$array1_size = count($array1);
$array2_size = count($array2);
// Creates a list containing all available IDs of Array1
for ($i = 0; $i < $array1_size; $i ++) {
$id_list[] = $array1[$i][0]['id'];
$array1[$i]['value'] = NULL; // Adds a NULL to all value fields
}
// Loops through Array2
for ($i = 0; $i < $array2_size; $i++) {
if (in_array($array2[$i]['id'], $id_list)) { // Checks if each ID exists in the ID list
$key = array_search($array2[$i]['id'], $id_list); // Gets the key of the matching ID
$array1[$key]['value'] = $array2[$i]['value']; // Adds the value
}
}
Edit: Values are now set to NULL by default and thus only gets changed later on if needed.

Categories