I have two arrays.
Array 1
Array
(
[1] => 111,
[id1] => 1,
[2] => 11231,
[id2] => 2,
[3] => 12311,
[id3] => 3,
[4] => 11981,
[id4] => 4,
[5] => 11761,
[id5] => 5,
[6] => 11561,
[id6] => 6
)
Array 2
Array
(
[1] => 2,
[id1] => 1,
[2] => 2,
[id2] => 2,
[3] => 3,
[id3] => 3,
[4] => 4,
[id4] => 4,
[5] => 4,
[id5] => 5,
[6] => 6,
[id6] => 6
)
Id key is user id in both arrays
And numerical key in second array is manager id
I want to merge these two array in below format.
Merge array
Array
(
[2] => Array
(
[0] => 111
[1] => 1
),
Array
(
[0] => 11231
[1] => 2
),
[3] => Array
(
[0] => 12311
[1] => 3
),
[4] => Array
(
[0] => 11981
[1] => 4
),
Array
(
[0] => 11761
[1] => 5
),
[6] => Array
(
[0] => 11561
[1] => 6
)
)
Array inside array is the value of first array.
2,3,4,6 key is values from second array.
Those users who have same manager id will be merge in single array.
Array Walk Apply a user supplied function to every member of an array
array_walk($array2, function($value,$key) use($array1,&$result){
if(is_integer($key))
$result[$value][]=[$array1[$key],$array1['id'.$key]];
});
output
array (size=4)
2 =>
array (size=2)
0 =>
array (size=2)
0 => int 111
1 => int 1
1 =>
array (size=2)
0 => int 11231
1 => int 2
3 =>
array (size=1)
0 =>
array (size=2)
0 => int 12311
1 => int 3
4 =>
array (size=2)
0 =>
array (size=2)
0 => int 11981
1 => int 4
1 =>
array (size=2)
0 => int 11761
1 => int 5
6 =>
array (size=1)
0 =>
array (size=2)
0 => int 11561
1 => int 6
Related
So I've searched for hours but no thread seems to get me to a working solution.
My problem;
I've got 2 arrays. 1 array with the user's roles. And one array with all roles with an specific order.
$roles = Array
(
[0] => Array
(
[id] => 22465
[name] => Rank 1
[position] => 24
[color] => 16711680
)
[1] => Array
(
[id] => 59454
[name] => Rank 2
[position] => 15
[color] => 15844367
)
[2] => Array
(
[id] => 62280
[name] => Rank 3
[position] => 2
[color] => 65494
)
[3] => Array
(
[id] => 67139
[name] => Rank 4
[position] => 10
[color] => 1146986
)
[4] => Array
(
[id] => 75372
[name] => Rank 5
[position] => 25
[color] => 1146986
)
[5] => Array
(
[id] => 75373
[name] => Rank 6
[position] => 18
[color] => 1146986
)
...
)
And I have the user roles array:
$userdata = Array
(
[roles] => Array
(
[0] => 22465
[1] => 59454
[2] => 62280
[3] => 67139
[4] => 75372
[5] => 75373
)
[data] => Array
(
[0] => irrelevant
)
)
I want the user roles array to be sorted to the roles 'position' in the other array.
I think the bottleneck is in the part that the array has to be called with the subarray [roles][position] to get the order to work.
The result should be:
[0] => 75372
[1] => 22465
[2] => 75373
[3] => 59454
[4] => 67139
[5] => 62280
Could you please help me any further?
Thanks in advance!
// First, build a lookup table relating id directly to position
$ranks = [];
foreach($roles as $role) {
$ranks[$role['id']] = $role['position'];
}
var_dump($ranks);
// Now sort the table using the lookup table we just produced
//
usort($userdata['roles'], function($a, $b) use ($ranks){
return $ranks[$b] <=> $ranks[$a];
});
var_dump($userdata);
Content of userdata array after sorting
array (size=2)
'roles' =>
array (size=6)
0 => int 75372
1 => int 22465
2 => int 75373
3 => int 59454
4 => int 67139
5 => int 62280
'data' =>
array (size=1)
0 => string 'irrelevant' (length=10)
I want to convert to this array
$string = "1,[1,2,3],[2,2,4],2,3";
To
Example
0 => 1,
1 => [
0 => 1,
1 => 2,
2 => 3
],
2 => [
0 => 2,
1 => 2,
2 => 4
],
3 => 2,
4 => 3
You can use json_decode() :
$string = "1,[1,2,3],[2,2,4],2,3";
$array = json_decode("[$string]", true);
print_r($array);
Output :
Array
(
[0] => 1
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 2
[1] => 2
[2] => 4
)
[3] => 2
[4] => 3
)
Above code tested here
I'm trying to update my array but it doesn't work as expected!
I got this array:
Array
(
[0] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
)
[1] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
)
[2] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
)
)
and using var_dump()
array (size=24)
0 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
1 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
2 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
3 =>
I got this array with this:
$array = [new property_unit('one bedroom', [new propertydetails(1, 54, 0, 1)]),
new property_unit('two bedroom', [new propertydetails(1, 74, 0, 1)]),
new property_unit('three bedroom', [new propertydetails(1, 90, 0, 1)]),
new property_unit('studio', [new propertydetails(2, 22.35, 0, 1)])];
Then I perform a computation on it to get all the possible array.
function computePermutations($array) {
$result = [];
$recurse = function($array, $start_i = 0) use (&$result, &$recurse) {
if ($start_i === count($array)-1) {
array_push($result, $array);
}
for ($i = $start_i; $i < count($array); $i++) {
//Swap array value at $i and $start_i
$t = $array[$i];
$array[$i] = $array[$start_i];
$array[$start_i] = $t;
//Recurse
$recurse($array, $start_i + 1);
//Restore old order
$t = $array[$i];
$array[$i] = $array[$start_i];
$array[$start_i] = $t;
}
};
$recurse($array);
return $result;
}
//I assign to my array
$array = computePermutations($array);
I thing something maybe goes wrong here ?
but when I'm trying to update my array like :
$array[0][0]->details[0]->priority = 3;
It update all the first priority field, (see the array before) this is after the update.
Meaning:
$array[0][0]->details[0]->priority
$array[1][0]->details[0]->priority
$array[2][0]->details[0]->priority
etc... becomes 3
Any idea where this can come from, is there any other way to update a multidimensional array? Am I doing something wrong here?
I'm playing around with array functions to get a better understanding.
Below I have three arrays:
$cardNumber = array(2, 4, 5, 8, 9);
$playerName = array('Julian', 'Brad', 'Chloe', 'Laura', 'Paul');
$playerWins = array(4, 5, 1, 2, 6);
I am trying to achieve the following array structure:
array (size=5)
2 =>
array (size=2)
0 => string 'Julian' (length=6)
1 => int 4
4 =>
array (size=2)
0 => string 'Brad' (length=4)
1 => int 5
5 =>
array (size=2)
0 => string 'Chloe' (length=5)
1 => int 1
8 =>
array (size=2)
0 => string 'Laura' (length=5)
1 => int 2
9 =>
array (size=2)
0 => string 'Paul' (length=4)
1 => int 6
I decided to combine $playerName and $playerWins with array_combine(), and then combine the resulting array with $cardNumber, which does not yield the output I expect. Am I understanding array_combine() incorrectly?
You can use array_map with null as a callback and the two data arrays to give you the array structure that you want. Then combine that with the array for the keys using array_combine:
$result = array_combine($cardNumber, array_map(null, $playerName, $playerWins));
For illustration:
$result = array_map(null, $playerName, $playerWins);
Yields:
Array
(
[0] => Array
(
[0] => Julian
[1] => 4
)
[1] => Array
(
[0] => Brad
[1] => 5
)
[2] => Array
(
[0] => Chloe
[1] => 1
)
[3] => Array
(
[0] => Laura
[1] => 2
)
[4] => Array
(
[0] => Paul
[1] => 6
)
)
Then combine to get the keys:
$result = array_combine($cardNumber, $result);
Yields:
Array
(
[2] => Array
(
[0] => Julian
[1] => 4
)
[4] => Array
(
[0] => Brad
[1] => 5
)
[5] => Array
(
[0] => Chloe
[1] => 1
)
[8] => Array
(
[0] => Laura
[1] => 2
)
[9] => Array
(
[0] => Paul
[1] => 6
)
)
I am having array as follows:
Array
(
[0] => Array
(
[question_id] => 1
[question_title] => Q1
[child_question_id] => NULL
)
[1] => Array
(
[question_id] => 2
[question_title] => Q2
[child_question_id] => NULL
)
[2] => Array
(
[question_id] => 3
[question_title] => Q3
[child_question_id] => 4
)
[3] => Array
(
[question_id] => 3
[question_title] => Q3
[child_question_id] => 5
)
[4] => Array
(
[question_id] => 4
[question_title] => Q4
[child_question_id] => NULL
)
[5] => Array
(
[question_id] => 5
[question_title] => Q5
[child_question_id] => NULL
)
[6] => Array
(
[question_id] => 6
[question_title] => Q6
[child_question_id] => NULL
)
[7] => Array
(
[question_id] => 7
[question_title] => Q7
[child_question_id] => 6
)
)
And using PHP I am expecting result as:
Array
(
[0] => Array
(
[question_id] => 1
[question_title] => Q1
[child_question_id] => NULL
)
[1] => Array
(
[question_id] => 2
[question_title] => Q2
[child_question_id] => NULL
)
[2] => Array
(
[question_id] => 3
[question_title] => Q3
[children] => array(
[0] => Array
(
[question_id] => 4
[question_title] => Q4
)
[1] => Array
(
[question_id] => 5
[question_title] => Q5
)
)
)
[3] => Array
(
[question_id] => 7
[question_title] => Q7
[children] => array(
[0] => Array
(
[question_id] => 6
[question_title] => Q6
)
)
)
)
I tried to build final array using following logic:
Traversing array to collect child ids and child data
Traversing array again and match items child id with child array
formed earlier, if found then create children array
Could you please help me out to create better solution?
Did you mean something like that?
$i = 0;
$new = array();
$unsetter = array();
foreach($array as $val)
{
$new[$i] = array();
foreach($val as $key => $data)
{
if(($key == 'child_question_id') && (!is_null($data)))
{
$new[$i]['children'] = array();
$new[$i]['children']['question_id'] = $data;
$new[$i]['children']['question_title'] = $array[$data]['question_title'];
$unsetter[] = $data;
} else {
$new[$i][$key] = $data;
}
}
$i++;
}
foreach($unsetter as $uns)
{
unset($new[$uns]);
}
$new = array_values($new);
var_dump($new);
This will output
array (size=5)
0 =>
array (size=3)
'question_id' => int 1
'question_title' => string 'Q1' (length=2)
'child_question_id' => null
1 =>
array (size=3)
'question_id' => int 2
'question_title' => string 'Q2' (length=2)
'child_question_id' => null
2 =>
array (size=3)
'question_id' => int 3
'question_title' => string 'Q3' (length=2)
'children' =>
array (size=2)
'question_id' => int 4
'question_title' => string 'Q4' (length=2)
3 =>
array (size=3)
'question_id' => int 3
'question_title' => string 'Q3' (length=2)
'children' =>
array (size=2)
'question_id' => int 5
'question_title' => string 'Q5' (length=2)
4 =>
array (size=3)
'question_id' => int 7
'question_title' => string 'Q7' (length=2)
'children' =>
array (size=2)
'question_id' => int 6
'question_title' => string 'Q6' (length=2)
Live Demo