I have an array:
$initialarray = array(
0 = array(
'unit' => 1,
'class' => 1,
'value' => 'string1'
),
1 = array(
'unit' => 1,
'class' => 2,
'value' => 'string2'
),
2 = array(
'unit' => 1,
'class' => 2,
'value' => 'string3'
),
3 = array(
'unit' => 2,
'class' => 1,
'value' => 'string4'
)
4 = array(
'unit' => 2,
'class' => 2,
'value' => 'string5'
)
);
What would be the best way to structure it (to group the resulting sub-arrays) depending first on the 'unit' field's values, and then depending on the 'class' field's values, like so:
$resultarray = array(
// array of all the sub-arrays of 'unit' = 1
$unit[1] = array(
// array of all the sub-arrays of 'unit' = 1 and 'class' = 1
$class[1] = array(
0 = array(
'unit' => 1,
'class' => 1,
'value' => 'string1'
)
)
// array of all the sub-arrays of 'unit' = 1 and 'class' = 2
$class[2] = array(
0 = array(
'unit' => 1,
'class' => 2,
'value' => 'string2'
),
1 = array(
'unit' => 1,
'class' => 2,
'value' => 'string3'
)
)
)
// array of all the sub-arrays of 'unit' = 2
$unit[2] = array(
// array of all the sub-arrays of 'unit' = 2 and 'class' = 1
$class[1] = array(
0 = array(
'unit' => 2,
'class' => 1,
'value' => 'string4'
)
)
// array of all the sub-arrays of 'unit' = 2 and 'class' = 2
$class[2] = array(
0 = array(
'unit' => 2,
'class' => 2,
'value' => 'string5'
)
)
)
)
I have asked a similar question here and got a working answer for only one iteration, i.e. for only structuring the array by one of the fields. But I could not make the same solution work for multiple iterations, i.e. for more than one field.
Also, is there a solution to structure a multidimensional array depending on more than two fields?
I think it's not a way of asking the question. It is very simple , you can do this by playing with arrays,keys and etc.... So first you should try hard for the problem. After If you have any problem in the middle of your tries then you can ask that here. I have solved your problem here is the complete code , but next time please do some work and then only post the problem. Never ask for the code.
foreach ($initialarray as $key1=>$val1)
{
foreach ($val1 as $key2=>$val2)
{
if($key2=='unit')
{
$num=$val2;
if($val2!=$num)
$testarr['unit'.$val2]=array();
}
if($key2=='class')
{
$testarr['unit'.$num]['class'.$val2][]=$val1;
}
}
}
print_r($testarr);
I must offer a better way for you and future researchers...
You only need one loop, and you merely need to nominate the result array's key values before using [] to "push" new data into the deepest subarray.
*there is absolutely no need for any condition statements or a second loop.
Code: (Demo)
$initialarray = [
['unit' => 1, 'class' => 1, 'value' => 'string1'],
['unit' => 1, 'class' => 2, 'value' => 'string2'],
['unit' => 1, 'class' => 2, 'value' => 'string3'],
['unit' => 2, 'class' => 1, 'value' => 'string4'],
['unit' => 2, 'class' => 2, 'value' => 'string5']
];
foreach ($initialarray as $row) {
$result[$row['unit']][$row['class']][] = $row;
}
var_export($result);
Output:
array (
1 =>
array (
1 =>
array (
0 =>
array (
'unit' => 1,
'class' => 1,
'value' => 'string1',
),
),
2 =>
array (
0 =>
array (
'unit' => 1,
'class' => 2,
'value' => 'string2',
),
1 =>
array (
'unit' => 1,
'class' => 2,
'value' => 'string3',
),
),
),
2 =>
array (
1 =>
array (
0 =>
array (
'unit' => 2,
'class' => 1,
'value' => 'string4',
),
),
2 =>
array (
0 =>
array (
'unit' => 2,
'class' => 2,
'value' => 'string5',
),
),
),
)
If I may express myself in the following manner: I only see the front-end of your problem and know nothing about its back-end, e.g. "Where does the data come from?", "How is it collected and stored", etc. so my answer might not be a real help but still I'll give my "tuppence".
If you can store all that data in a relational database (in form of table(s)) it would be much more easier and faster(!) to select the needed data from the database instead of rearranging arrays, which will take some more time in comparison.
Just as an example you might then select (and store it into an array) all items which have unit = '1' and / or all items which have class = '2'. That would make life much more easier IMHO, than having all the data in a multidimensional array and then try to sort it / rearrange it. Especially if you do that based on more than one property.
Related
I have an object from a sql-query. Every entry has the keys (id, type, title, parent_id).
My example data:
Content of the sql-object $list (var_export()):
$array = array(0 => (object) array( 'id' => 1, 'type' => 'label', 'title' => 'Product Categories', 'parent_id' => 0, ),
1 => (object) array( 'id' => 2, 'type' => 'label', 'title' => 'Shoes', 'parent_id' => 1, ),
2 => (object) array( 'id' => 3, 'type' => 'label', 'title' => 'T-Shirts', 'parent_id' => 1, ),
3 => (object) array( 'id' => 4, 'type' => 'label', 'title' => 'With Print', 'parent_id' => 2, ),
4 => (object) array( 'id' => 5, 'type' => 'label', 'title' => 'Without Print', 'parent_id' => 2, ),
5 => (object) array( 'id' => 6, 'type' => 'label', 'title' => 'Brands', 'parent_id' => 2, ),
6 => (object) array( 'id' => 7, 'type' => 'label', 'title' => 'Blue', 'parent_id' => 3, ),
7 => (object) array( 'id' => 8, 'type' => 'label', 'title' => 'Red', 'parent_id' => 3, ));
What i expect:
the function, should find the dependencies of the entries starting with a given id. Here for example the ID 7:
Array
(
[0] => stdClass Object
(
[id] => 7
[type] => "label"
[title] => "Blue"
[parent_id] => 3
)
[1] => stdClass Object
(
[id] => 3
[type] => "label"
[title] => "T-Shirts"
[parent_id] => 1
)
[2] => stdClass Object
(
[id] => 1
[type] => "label"
[title] => "Product Categories"
[parent_id] => 0
)
)
What i get:
I just get an array with the first entry, with the id I started with.
As example with starting ID 7:
array ( 0 => (object) array( 'id' => 7, 'type' => 'label', 'title' => 'Blue', 'parent_id' => 3, ), )
My current Function:
The function needs to search for the item with the given id, stores the information into a new array and then start a new search but with the parent_id as new search id. This should loop as long as there are dependencies, if there are no dependencies the loop should stop and returning the created array.
function getParentSelect($list, $parent) {
$next_id = true;
$result = array();
foreach($list as $k => $s) {
echo $s->id;
if ($s->id == $parent) {
$result[] = $s;
$next_id = $s->parent_id;
break;
}
else {
$next_id = false;
}
}
if ($next_id != false) {
$result = array_merge($result, getParentSelect($list, $next_id));
}
return $result;
}
Recursion is always somewhat difficult to understand. I think you got the main idea, but the execution was flawed. This is what I can up with:
function getParentSelect($list, $select_id) {
$result = [];
foreach($list as $s) {
if ($s->id == $select_id) {
$result = array_merge([$s], getParentSelect($list, $s->parent_id));
}
}
return $result;
}
The assumption here is that all the parent id's are valid.
How does the code work?
The function itself searched the whole list for items with the id that was selected. If it finds one it will add it to the results, but it also looks for any parents of that item. This is where the function recurses. This means that the function can also look for parents of parents, and so on. array_merge() is used to combine the items and all parents together to form the results.
i have a problem with this array, i need get sum form string merge with same key
$data = array(
0 => array(
'name' => 'Alfa Edison, Dwiki',
'budget' => 3700,
),
1 => array(
'name' => 'Maverick Sam',
'budget' => 500,
),
2 => array(
'name' => 'Dwiki',
'budget' => 1000,
),
3 => array(
'name' => 'Steve, Dwiki',
'budget' => 2000,
),
4 => array(
'name' => 'Alfa Edison',
'budget' => 700,
),
5 => array(
'name' => 'Maverick Sam',
'budget' => 4000,
),
6 => array(
'name' => 'Steve, Alfa Edison',
'budget' => 334,
),
);
i want the result this:
array(
0 => array(
'name' => 'Alfa Edison',
'budget' => 4734,
),
1 => array(
'name' => 'Dwiki',
'budget' => 6700,
),
2 => array(
'name' => 'Maverick Sam',
'budget' => 4500,
),
3 => array(
'name' => 'Steve',
'budget' => 2334,
),
);
How to merge String with same Key and Sum the Budget. i try to for each but i'm fail.
i try array_reduce and explode the name but fail.
The problem is that each of the "keys" (names) is really more than one key. So as you iterate the input array you'll need to split those up, then add an inner loop to use the names as keys in the result.
foreach ($data as $item) {
// separate the names
$names = explode(', ', $item['name']);
// iterate the names and set/increase values for them in the result array
foreach ($names as $name) {
$result[$name]['name'] = $name;
$result[$name]['budget'] = $item['budget'] + ($result[$name]['budget'] ?? 0);
}
}
// remove the string keys if necessary
$result = array_values($result);
I have an array
$info = array(
[0] => array(
'id' => 1,
'uid' => '677674e21aed487fd7180da4a7619a9d'
),
[1] => array(
'id' => 1,
'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1'
),
[2] => array(
'id' => 1,
'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1'
),
[3] => array(
'id' => 1,
'uid' => '658284e5395a29bf34d21f30a854e965'
),
[4] => array(
'id' => 1,
'uid' => '01f33ae45a463e0c1de4ad989b3ccad5'
),
[5] => array(
'id' => 1,
'uid' => '677674e21aed487fd7180da4a7619a9d'
)
)
As you can see, uid of 0th index and 5th index are same. Similarly, uid of 2nd index and 3rd index are same.
I want a PHP script by which I can randomly create one hexadecimal color code for duplicate uids. Say something like this.
$info = array(
[0] => array(
'id' => 1,
'uid' => '677674e21aed487fd7180da4a7619a9d',
'col' => 'black'
),
[1] => array(
'id' => 1,
'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1',
'col' => 'green'
),
[2] => array(
'id' => 1,
'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1',
'col' => 'green'
),
[3] => array(
'id' => 1,
'uid' => '658284e5395a29bf34d21f30a854e965'
),
[4] => array(
'id' => 1,
'uid' => '01f33ae45a463e0c1de4ad989b3ccad5'
),
[5] => array(
'id' => 1,
'uid' => '677674e21aed487fd7180da4a7619a9d',
'col' => 'black'
)
)
How can I do this with the most minimum execution time?
There might be various ways for doing this workout, but due to lack of proper response, I came up with this probable lengthier code. I am posting the answer here for people who might need this.
$uidArray = array(); // creating a blank array to feed each uid
$uidDuplicateArray = array(); // creating a blank array as container to hold duplicate uid(s) only
foreach($all_data as $key => $ad)
{
// iterate through each item of the list
/.................
.................. //
$uidArray[] = $ad['uid'];
}
foreach(array_count_values($uidArray) as $val => $c)
{
if($c > 1)
{
// if count value is more than 1, then its duplicate
// set the uid duplicate array with key as uid and unique color code as value
$uidDuplicateArray[$val] = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
}
}
foreach($all_data as $keyAgain => $adg)
{
// iterating through each item of original data
if(isset($uidDuplicateArray[$adg['uid']]))
{
// if the uid is key of the duplicate array, feed the value to original array in a new key.
$all_data[$keyAgain]['color'] = $uidDuplicateArray[$adg['uid']];
}
}
Each comment associated with each LOC is self explanatory.
The reason I wanted this, is to mark the duplicates in UI like this:-
I have an array that looks something like below, but it could potentially contain more drafts.
$multi_arr = array(
'draft1' => array (
'alloc_coeff' => 560,
'steps_count' => 2,
'order_draft' => array(
'0' => array(
'whse_id' => 4,
'quantity' => 0,
'percent' => 0
),
'1' => array(
'whse_id' => 1,
'quantity' => 10,
'percent' => 66.666666666667
)
)
),
'draft2' => array (
'alloc_coeff' => 1517,
'steps_count' => 1,
'order_draft' => array(
'0' => array(
'whse_id' => 1,
'quantity' => 10,
'percent' => 66.666666666667
)
)
),
'draft3' => array (
'alloc_coeff' => 559,
'steps_count' => 2,
'order_draft' => array(
'0' => array(
'whse_id' => 2,
'quantity' => 0,
'percent' => 0
),
'1' => array(
'whse_id' => 1,
'quantity' => 10,
'percent' => 66.666666666667
)
)
)
);
I need to sort the content by using two variables: alloc_coeff and steps_count.
First alloc_coeff needs to be considered from the highest to the lowest value.
And then as a second variable the steps_count from lowest value to highest.
usort($multi_arr, function($a, $b) {
return $a['alloc_coeff'] <=> $b['alloc_coeff'];
});
I don't need the entire array to be rewritten and stored in a temporary variable, I just need the keys sorted like this (expected outcome) draft2, draft1 and lastly draft3.
What's a way to accomplish this?
You can do it like below:-
array_multisort(array_column($multi_arr, 'alloc_coeff'), SORT_DESC,
array_column($multi_arr, 'steps_count'), SORT_ASC,
$multi_arr);
Output:- https://eval.in/924894 And https://eval.in/924892
Note:- tested in 5.6.23 and PHP 7.0.8
I'm trying to insert a couple of new Key/Value pairs into an associative array at a specific place. From other reading I've done on SO, I'm pretty sure I have to loop through the array and insert the new values when a condition is set.
Here is the current array
array(
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
)
)
)
)
)
(int) 1 => array(
'Product' => array(
'id' => '60',
'title' => 'Red Dress',
'Review' => array()
)
)
)
The key Review does not always have data, but when it does I want to insert a new key-value similar to the following excerpt
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
'some_value' => '5'
)
)
)
)
)
I've tried a few things without success.
Any help is much appreciated thanks.
You can do something like this:
if(!empty($your_array[index]['Product']['Review'])){
$your_array[index]['Product']['Review']['Review'][index]['some_value'] = 'new_value';
}
In your example it could be:
if(!empty($your_array[0]['Product']['Review'])){
$your_array[0]['Product']['Review']['Review'][0]['some_value'] = 'new_value';
}
Again, you didn't mention your code. So, it's hard to figure out what you want exactly!
You should iterate through Your array and pass current value be reference:
// Notice & sign before variable
foreach ($data as &$product)
{
if ($product['Product']['Review'])
{
// or iterate through Review array
$product['Product']['Review']['Review'][0]['some_value'] = 5;
}
}