php combine multidimensional arrays - php

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

Related

Search multidimensional array for value

I wonder if there is better (faster) way to search for value in multidimensional array than looping through every item.
Lets say i have
$id_to_search = '16819976033';
And array which is pretty big
Array
(
[0] => Array
(
[id] => Array
(
[0] => 16771055710
[1] => 16776555710
[2] => 16819976033
)
[o] => 21566
[p] => 12597.66
)
[1] => Array
(
[id] => Array
(
[0] => 14089762
)
[o] => 12606
[p] => 1747.49
)
etc ...
)
I can find it if i loop through each item and than compare them but its very slow because array is big.
You can use by array_search function in PHP:
$key = array_search($id_to_search, array_column($YourArray, 'id'));

Array structure rearrangement without loop

I have an array like mentioned below, which I want to rearrange without using loop:
Array
(
[0] => Array
(
[Books] => Array
(
[id] => 4
)
)
[1] => Array
(
[Books] => Array
(
[id] => 3
)
)
[2] => Array
(
[Books] => Array
(
[id] => 2
)
)
[3] => Array
(
[Books] => Array
(
[id] => 1
)
)
)
I want an output like this:
Array(4,3,2,1)
I'm assuming you do not want to use for or foreach loops, but anything else that internally is or uses a loop is fine.
in this case, you can use array_map:
$result = array_map(function($item){
return $item['books']['id'];
}, $currentArray);
OR
if you do not even want that:
$v1 = array_column($input, 'books');
$result = array_column($v1, 'id');

Multidimensional Array - Get unique values, but count all duplicates

I have a multi-dimensional array that I would like to get unique sub-values from, but also have a count of how many times those unique sub-values occurred.
For instance, this would be my starting array:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 3333333333333333
)
)
[1] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 5555555555555555
)
)
[2] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 77777777777777777
)
)
In the end, I'd like to have an array that looks like this:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
[count] => 3
)
[1] => Array
(
[id] => 3333333333333333
[count] => 1
)
[2] => Array
(
[id] => 5555555555555555
[count] => 1
)
[3] => Array
(
[id] => 77777777777777777
[count] => 1
)
)
Is there any general/easy way to do this without iterating through the first array for each value, comparing/storing the values in a temporary array, checking them, and adding to the count?
To get this exact format you may need to iterate thought your current array and do the counting manually, however php has the array_count_values() and array_unique() functions for this kind of thing:
http://php.net/manual/en/function.array-count-values.php
http://php.net/manual/en/function.array-unique.php
Because you are only concerned with the deepest values of the array, using array_walk_recursive seems suitable for this. Note that a reference to the output array $counted is used in the callback.
array_walk_recursive($ids, function($id, $k) use (&$counted) {
$counted[$id] = isset($counted[$id]) ? $counted[$id] + 1 : 1;
});
Using the id as the key in the $counted array will simplify the counting. The result of this will be somewhat different from your suggested output, but in my opinion it would actually be simpler to use. (e.g. foreach ($counted as $id => $count) {...).
$counted = array(
"1533438473619168" => 3
"3333333333333333" => 1
"5555555555555555" => 1
"77777777777777777" => 1);

How to sort multidimensional array with PHP?

I've an array like below, what I want is to display record having row at the bottom whose "quotes" array is empty.
Array
(
[0] => Array
(
[id] => 0
[regNo] => LHR7171
[quotes] => Array
(
)
)
[1] => Array
(
[id] => 2
[regNo] => YN09 BYY (9)
[quotes] => Array
(
somevalues in array format
)
)
uasort(
$i,
function($value1, $value2) {
return count($value2['quotes']) - count($value1['quotes']);
}
);
And regarding the tags below your question: this has (of course) absolutely nothing to do with SF2 or Twig. This is just plain PHP.

How can I sort through multidimensional arrays within multidimensional arrays to eliminate repetitions?

Here is an example output from when I print out its contents:
Array
(
[0] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbA] =>
)
)
)
)
[1] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbB] =>
)
)
)
)
[2] => Array
(
[CountryA] => Array
(
[ProvinceB] => Array
(
[CityB] => Array
(
[SuburbC] =>
)
)
)
)
[3] => Array
(
[CountryB] => Array
(
[ProvinceD] => Array
(
[CityE] => Array
(
[SuburbE] =>
)
)
)
)
What I would like to do is create a function that parses it in some way (and perhaps creates a new array) so that the result will look something like:
Array
(
[0] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbA] =>
[SuburbB} =>
)
)
[ProvinceB] =>
(
[CityB] => Array
(
[SuburbC] =>
)
)
)
)
[1] => Array
(
[CountryB] => Array
(
[ProvinceD] => Array
(
[CityE] => Array
(
[SuburbE] =>
)
)
)
)
Thanks in advance!!
Change you structure, your array should not look like this :
Array(
[0] => Array([Country A] => data),
[1] => Array([Country B] => data)
)
But more like this :
Array(
[Country A] => data,
[Country B] => data
)
Once you've done this, it will be trivial to add a city in your array :
If the country exists in the array, add to the country, else add it to the array and stop
[Add to country :] If the province exists in the country, add to the province, else add the province to the array and stop
Same for city, suburb... you get the idea

Categories