Array combination with custom style [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have this kind of array.
Array
(
[0] => Array
(
[supplierID] => 1
[parkTypeID] => 1
[cost] => 17
)
[1] => Array
(
[supplierID] => 2
[parkTypeID] => 1
[cost] => 65
)
[2] => Array
(
[supplierID] => 2
[parkTypeID] => 2
[cost] => 30
)
)
I want to combine elements which has same value for supplierID like this. Each key name's value has set in new array's keys.
Array
(
[1] => Array
(
[1] => 17
)
[2] => Array
(
[1] => 65
[2] => 30
)
)

Try
$r = array();
foreach ($array1 as $x) {
if (array_key_exists($x['supplierID'], $r)
$r[$x['supplierID']][$x['parkTypeID']] = $x['cost'];
else
$r[$x['supplierID']] = array($x['parkTypeID'] => $x['cost']);
}
This will iterate over the initial array $array1 and create a new array $r with the desired information.
For each element in the original array we check whether supplierID exists in $r.
If so, we just append the new correlation between the parkTypeID and the cost to the existing values.
If not, we add the supplierID to $r and assign it an array with only this pair of parkTypeID and cost.
A simplified version of the above code that relies on php to initialize an unset array index to an empty array when necessary is
$r = array();
foreach ($array1 as $x)
$r[$x['supplierID']][$x['parkTypeID']] = $x['cost'];

$arr_res = array_reduce($arr, function($t, $v) {
if (!isset($t[$v['supplierID']]))
$t[$v['supplierID']] = array();
$t[$v['supplierID']][] = $v['cost'];
return $t;
} , array());

Related

How to limit sub array count to 5 for all id values in multi dimensional array php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a multi dimensional output array with following structure :
Array
(
[0] => Array
(
[id] => 5925
[fb_id] => 123
[description] =>
[video] =>
[thum] =>
)
[1] => Array
(
[id] => 7060
[fb_id] => 2344
[description] =>
[video] =>
[thum] =>
)
[2] => Array
(
[id] => 6579
[fb_id] => 123
[description] =>
[video] =>
)
)
Here, I want to limit the sub array count to 5 for all users with same 'fb_id'. Only 5 sub arrays for each fb_id should be in final output array. Any help would be appreciable.
The original array being used here is called $original and this code will reduce it down and produce a new array called $new. Feel free to change the variable names as appropriate.
$new = [];
$count = [];
foreach ($original as $element) {
$fbid = $element['fb_id'];
if (!isset($count[$fbid])) {
$count[$fbid] = 0;
}
if ($count[$fbid] < 5) {
$new[] = $element;
$count[$fbid]++;
}
}
// New array with limited sub-arrays
print_r($new);

converting array of objects to simple array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an array of objects, each object consists of an id and an organisation. It looks like this
Array (
[0] => stdClass Object (
[id] => 2 [organisation] => org1
)
[1] => stdClass Object (
[id] => 4 [organisation] => org2
)
[2] => stdClass Object (
[id] => 1 [organisation] => org3
)
)
I need to convert it into a simple associative array ([id]=>organisation,...) so the above example would look like this
Array (
[2] => org1
[4] => org2
[1] => org3
)
Greatful for any thoughts
Loop through it using a foreach statement and append it to another array.
$finished = [];
foreach($array as $arr) {
$finished[$arr->id] = $arr->organisation;
}
$result = array();
foreach($array as $arr) {
$result[$arr->id] = $arr->organisation;
}
echo "<pre>";print_r($result);

Rearrange php Array structure [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there any simple solution to convert php mysql result change array structure with foreach or any method ?
Current result
Array
(
[1033] => stdClass Object
(
[id] => 1033
[plugin] => kooyke
[name] => kooyduration
[value] => 13
)
[1029] => stdClass Object
(
[id] => 1029
[plugin] => kooyke
[name] => kooyendpoint
[value] => http://localhost/public/data/
)
[1030] => stdClass Object
(
[id] => 1030
[plugin] => kooyke
[name] => kooylogin
[value] => ryrtrtr68fds876fdsf876fsd87fd
)
)
Expecting result
Array
(
[kooyduration] => 13
[kooyendpoint] => http://localhost/public/data/
[kooylogin] => ryrtrtr68fds876fdsf876fsd87fd
)
the code trying to convert
foreach($result as $value){
$expresult[] = $value['value'];
}
print_r($expresult);
Bellow code blocks may help you.
$i = 0;
$last_arr = array();
$new_arr = array();
foreach($my_array as $key=>$val)
{
$new_arr[$val->name] = $val->value;
}
array_push($last_arr,$new_arr);
You can not use like $value['value'], should be $value->value because of object. And push new array your name index. Try following:
$expresult = array();
foreach($result as $value)
{
$expresult[$value->name] = $value->value;
}

foreach loop begins at last element [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My foreach loop begins at last element. I have an array $gridRows within object $grid that is currently set up like so:
$grid
gridRows
2
gridCells
1
2
3
3
gridCells
1
2
gridRow 2 has 3 gridCells in it and gridRow 3 has 2 gridCells.
However, when I iterate through it, the first foreach goes to gridRow 3 first. It still iterates over gridRow 2 but it does it second.
$gridRows = $grid->getGridRows();
foreach ($gridRows as $rowKey => $rowValue) {
$gridCells = $rowValue->getGridCells();
foreach ($gridCells as $cellKey => $cellValue) {
// do something
}
}
What gives? I tried using sort($grid, SORT_NUMERIC) but I get the same effect.
Result of print_r($gridRows):
Array (
[3] => GridRow Object (
[gridCells:GridRow:private] => Array (
[2] => GridCell Object ( ... )
[1] => GridCell Object ( ... )
)
)
[2] => GridRow Object (
[gridCells:GridRow:private] => Array (
[1] => GridCell Object ( ... )
[2] => GridCell Object ( ... )
[3] => GridCell Object ( ... )
)
)
)
It seems to being an object not an array. Use a numerically indexed array to maintain the order you want.

How can I merge two arrays into one [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I've looked on this site and have found a few examples of merging arrays but to be honest, I'm not sure which one is right for me.
I am having a difficult time trying to merge these arrays. I've tried array_combine and array_merge but I am not having any luck.
What I need is a single array with the output like this:
Array
(
[0] => Array
(
[UnitNo] => 91
[Name] => Receiving
[ActivityNo] =>
[Active] => 2
[CallNo] =>
[CallStatusNo] =>
[Assigned] =>
[UnitId] => 2
)
[1] => Array
(
[UnitNo] => 83
[Name] => Shipping
[ActivityNo] =>
[Active] =>
[CallNo] =>
[CallStatusNo] =>
[Assigned] =>
[UnitId] => 1
)
)
These are the two arrays that I need to merge together based on the UnitId that should form a complete array like the one above. I just don't know how to do this. If someone could direct me a bit, I would be grateful.
// Array #1
Array
(
[0] => Array
(
[UnitId] => 2
[UnitNo] => 91
[Name] => Receiving
[Active] => 4
)
[1] => Array
(
[UnitId] => 1
[UnitNo] => 83
[Name] => Shipping
[Active] => 4
)
)
// Array #2
Array
(
[0] => Array
(
[UnitId] => 2
[ActivityNo] => 1
[CallNo] => 1
[CallStatusNo] => 1
[Assigned] => 1
)
[1] => Array
(
[UnitId] => 1
[ActivityNo] => 11
[CallNo] => 2
[CallStatusNo] => 1
[Assigned] => 1
)
)
This isn't a case where you can just use a php function and call it good - you will have to do some custom logic of your own.
This would work, but only if both $array1 and $array2 have the same items at each index.
$array1;
$array2;
$mergedArray;
for ($i=0; $i++; $i < count($array1) - 1) {
//first add all the values from array1
$mergedArray[$i] = $array1[$i];
foreach($array2[$i] as $array2Key=>$array2Value){
//now check for missing key/values that are in array2 but not in array1
if (array_key_exists($array2Key, $array1[$i]) == false) {
// the key does not exist in array1, so add it to mergedArray
$mergedArray[$i][$array2Key] = $array2Value;
}
}
}
It would be safer if your arrays were associative arrays, indexed by a unique value.

Categories