array {
[0]=> array { [0]=> "1" [1]=> "7" [2]=> "5" [3]=> "0" [4]=> "0" }
[1]=> array { [0]=> "2" [1]=> "3" [2]=> "7" [3]=> "0" [4]=> "0" }
[2]=> array { [0]=> "3" [1]=> "5" [2]=> "10" [3]=> "0" [4]=> "0" }
[3]=> array { [0]=> "4" [1]=> "11" [2]=> "4" [3]=> "0" [4]=> "0" }
[4]=> array { [0]=> "5" [1]=> "12" [2]=> "9" [3]=> "0" [4]=> "0" }
[5]=> array { [0]=> "6" [1]=> "6" [2]=> "12" [3]=> "0" [4]=> "0" }
[6]=> array { [0]=> "7" [1]=> "8" [2]=> "6" [3]=> "0" [4]=> "0" }
[7]=> array { [0]=> "8" [1]=> "0" [2]=> "14" [3]=> "0" [4]=> "0" }
[8]=> array { [0]=> "9" [1]=> "25" [2]=> "8" [3]=> "0" [4]=> "0" }
[9]=> array { [0]=> "10" [1]=> "30" [2]=> "7" [3]=> "0" [4]=> "0" }
}
i would like to ask how can i use list() function to list out
array{7,3,5,11,12,6,8,0,25,30}
thank you very much.
$yourarray = array();
foreach($array as $arr)
{
$yourarray[] = $arr[1];
}
print_r($yourarray);
list() doesn't work that way, it assigns a one-dimensional array of size n to n variables. The way this data is structured, the only way I can see to extract that data is a foreach, as others have suggested.
The "modern" / functional way to isolate a single "column" of data from a two multidimensional array is to call array_column() and write the desired column's key as the second parameter.
Code: (Demo)
$array = [
["1", "7", "5", "0", "0"],
["2", "3", "7", "0", "0"],
["3", "5", "10", "0", "0"],
["4", "11", "4", "0", "0"],
["5", "12", "9", "0", "0"],
["6", "6", "12", "0", "0"],
["7", "8", "6", "0", "0"],
["8", "0", "14", "0", "0"],
["9", "25", "8", "0", "0"],
["10", "30", "7", "0", "0"],
];
var_export(array_column($array, 1));
Output:
array (
0 => '7',
1 => '3',
2 => '5',
3 => '11',
4 => '12',
5 => '6',
6 => '8',
7 => '0',
8 => '25',
9 => '30',
)
Related
This question already has answers here:
Group multidimensional array data based on two column values and sum values of one column in each group
(5 answers)
Closed 7 months ago.
I have 2D array (showed bellow) - output of wp_query (Wordpress).
I need to group the arrays by index 0, 1, 2 values (lets think that the primary key is composed from these values and it should be unique in result table) and sum the index 3 values.
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(6) "Jack"
[2]=> string(6) "Daniels"
[3]=> string(2) "10"
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(3) "Jan"
[2]=> string(6) "Novak"
[3]=> string(2) "33"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "John"
[2]=> string(3) "Doe"
[3]=> string(2) "11"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "Jane"
[2]=> string(3) "Doe"
[3]=> string(2) "18"
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(6) "Jack"
[2]=> string(6) "Daniels"
[3]=> string(1) "5"
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(3) "Jan"
[2]=> string(6) "Novak"
[3]=> string(2) "33"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "John"
[2]=> string(3) "Doe"
[3]=> string(2) "11"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "Jane"
[2]=> string(3) "Doe"
[3]=> string(2) "18"
}
ETC...
Output should be someting like:
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(6) "Jack"
[2]=> string(6) "Daniels"
[3]=> string(2) "15" (5+10)
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(3) "Jan"
[2]=> string(6) "Novak"
[3]=> string(2) "66" (33+33)
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "John"
[2]=> string(3) "Doe"
[3]=> string(2) "22" (11+11)
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "Jane"
[2]=> string(3) "Doe"
[3]=> string(2) "36" (18+18)
}
}
I tried to do this by recursive for loops, but I cannot find the reason why is it not working properly.
$vysledna_tabulka = array();
$aktualni;
foreach($seznam_vsech_hracu_vsech_tymu as $radek){
if(empty($vysledna_tabulka)){
array_push($vysledna_tabulka,$radek);
} else{
foreach($vysledna_tabulka as $vysledny_radek){
if($vysledny_radek[0]==$radek[0] && $vysledny_radek[1]==$radek[1] && $vysledny_radek[2]==$radek[2]){
$vysledny_radek[3]+=$radek[3];
} else {
$aktualni = $radek;
}
}
array_push($vysledna_tabulka,$aktualni);
}
}
I think there should be a better way to do this. Is there anyone who could help me?
Thanks.
I would create a key from the first three values of your array elements and then create a new array using this key. Like this:
<?php
$teamArray = [
[
"Team 1",
"Jack",
"Daniels",
"10",
],
[
"Team 1",
"Jan",
"Novak",
"33",
],
[
"Team 2",
"John",
"Doe",
"11",
],
[
"Team 2",
"Jane",
"Doe",
"18",
],
[
"Team 1",
"Jack",
"Daniels",
"5",
],
[
"Team 1",
"Jan",
"Novak",
"33",
],
[
"Team 2",
"John",
"Doe",
"11",
],
[
"Team 2",
"Jane",
"Doe",
"18",
]
];
$result = [];
foreach ($teamArray as $team) {
$key = $team[0] . $team[1] . $team[2];
if (isset($result[$key])) {
$result[$key][3] += $team[3];
}
else {
$result[$key] = $team;
}
}
var_dump($result);
var_dump(array_values($result));
This gives you:
array(4) {
["Team 1JackDaniels"]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(4) "Jack"
[2]=>
string(7) "Daniels"
[3]=>
int(15)
}
["Team 1JanNovak"]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(3) "Jan"
[2]=>
string(5) "Novak"
[3]=>
int(66)
}
["Team 2JohnDoe"]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "John"
[2]=>
string(3) "Doe"
[3]=>
int(22)
}
["Team 2JaneDoe"]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "Jane"
[2]=>
string(3) "Doe"
[3]=>
int(36)
}
}
array(4) {
[0]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(4) "Jack"
[2]=>
string(7) "Daniels"
[3]=>
int(15)
}
[1]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(3) "Jan"
[2]=>
string(5) "Novak"
[3]=>
int(66)
}
[2]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "John"
[2]=>
string(3) "Doe"
[3]=>
int(22)
}
[3]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "Jane"
[2]=>
string(3) "Doe"
[3]=>
int(36)
}
}
If you need to remove the keys from your $result array you could use array_value as shown above.
I have a problem, I would like to view that a value (id_person) of a multidimensional array exists in other multidimensional array or not. (id_person is the value what I would like to compare in these two arrays - Eddie Taylor: id_person 302 and Jack Jones: id_person 311 exists in second array ). I hope someone could help for me. Many thanks.
Array one: ($homesquad)
array(4) {
[0]=>
array(9) {
["id"]=>
string(3) "277"
["id_club"]=>
string(2) "44"
["id_season"]=>
string(1) "2"
["position"]=>
string(1) "8"
["id_person"]=>
string(3) "306"
["name"]=>
string(10) "Mark Jones"
["type"]=>
string(1) "2"
["captain"]=>
string(1) "0"
["pos"]=>
string(1) "1"
}
[1]=>
array(9) {
["id"]=>
string(3) "282"
["id_club"]=>
string(2) "44"
["id_season"]=>
string(1) "2"
["position"]=>
string(2) "11"
["id_person"]=>
string(3) "311"
["name"]=>
string(10) "Jack Jones"
["type"]=>
string(1) "2"
["captain"]=>
string(1) "0"
["pos"]=>
string(1) "4"
}
[2]=>
array(9) {
["id"]=>
string(3) "273"
["id_club"]=>
string(2) "44"
["id_season"]=>
string(1) "2"
["position"]=>
string(1) "4"
["id_person"]=>
string(3) "302"
["name"]=>
string(12) "Eddie Taylor"
["type"]=>
string(1) "2"
["captain"]=>
string(1) "0"
["pos"]=>
string(1) "6"
}
[3]=>
array(9) {
["id"]=>
string(3) "270"
["id_club"]=>
string(2) "44"
["id_season"]=>
string(1) "2"
["position"]=>
string(1) "2"
["id_person"]=>
string(3) "299"
["name"]=>
string(13) "Jonas Haverla"
["type"]=>
string(1) "2"
["captain"]=>
string(1) "0"
["pos"]=>
string(2) "10"
}
}
Array two ($homeabsences)
array(5) {
[0]=>
array(7) {
["id"]=>
string(3) "265"
["id_club"]=>
string(2) "44"
["id_season"]=>
string(1) "2"
["position"]=>
string(1) "1"
["id_person"]=>
string(3) "294"
["name"]=>
string(13) "Harry Jackson"
["type"]=>
string(1) "1"
}
[1]=>
array(7) {
["id"]=>
string(3) "269"
["id_club"]=>
string(2) "44"
["id_season"]=>
string(1) "2"
["position"]=>
string(1) "2"
["id_person"]=>
string(3) "311"
["name"]=>
string(10) "Jack Jones"
["type"]=>
string(1) "1"
}
[2]=>
array(7) {
["id"]=>
string(3) "288"
["id_club"]=>
string(2) "44"
["id_season"]=>
string(1) "2"
["position"]=>
string(2) "14"
["id_person"]=>
string(3) "302"
["name"]=>
string(12) "Eddie Taylor"
["type"]=>
string(1) "1"
}
[3]=>
array(7) {
["id"]=>
string(3) "286"
["id_club"]=>
string(2) "44"
["id_season"]=>
string(1) "2"
["position"]=>
string(2) "14"
["id_person"]=>
string(3) "315"
["name"]=>
string(12) "Terry Barnes"
["type"]=>
string(1) "2"
}
[4]=>
array(7) {
["id"]=>
string(3) "277"
["id_club"]=>
string(2) "44"
["id_season"]=>
string(1) "2"
["position"]=>
string(1) "8"
["id_person"]=>
string(3) "366"
["name"]=>
string(14) "Jerry O'Donald"
["type"]=>
string(1) "7"
}
}
My current (UPDATED) code (which not working):
{foreach item=player from=$homesquad}
{if $player.pos == $count} (filtering the first array based on pos value)
{if in_array("id_person",$homeabsences)}
Do X.
{else}
Do Y.
{/if}
{/if}
{/foreach}
Use the PHP function array_intersect to get values found in both the first array and the second array. Try this code:
<?php
//First array
$homesquad = array(
array(
"id" => "270",
"id_club" => "44",
"id_season" => "2",
"position" => "8",
"id_person" => "306",
"name" => "Mark Jones",
"type" => "2",
"captain" => "0",
"pos" => "1"
),
array(
"id" => "273",
"id_club" => "44",
"id_season" => "2",
"position" => "4",
"id_person" => "302",
"name" => "Eddie Taylor",
"type" => "2",
"captain" => "0",
"pos" => "6"
)
);
//Second array
$homeabsences = array(
array(
"id" => "277",
"id_club" => "44",
"id_season" => "2",
"position" => "8",
"id_person" => "366",
"name" => "Jerry O'Donald",
"type" => "2",
"captain" => "0",
"pos" => "1"
),
array(
"id" => "273",
"id_club" => "44",
"id_season" => "2",
"position" => "4",
"id_person" => "302",
"name" => "Eddie Taylor",
"type" => "2",
"captain" => "0",
"pos" => "6"
)
);
//get intersection between $homesquad and $homeabsences
$id_persons = array_intersect(
array_column($homesquad, 'id_person'),
array_column($homeabsences, 'id_person')
);
if($id_persons)
print_r($id_persons); //this will display the intersection of two arrays
else
echo "no matches"
?>
To find all values of id_person in an array use the array_column function. Then you can find if it overlaps:
array_intersect(
array_column($homesquad, 'id_person'),
array_column($homeabsences, 'id_person')
)
Or for a single value
in_array($player['id_person'], array_column($homeabsences, 'id_person'))
array(
[0]=> array(3)
{
[0]=> array(3)
{
["name"]=> string(1) "a" ,
["code"]=> string(3) "416" ,
["id"]=> string(2) "a1" ,
},
[1]=> array(3)
{
["name"]=> string(1) "a",
["code"]=> string(3) "522" ,
["id"]=> string(2) "a2",
},
[2]=> array(3)
{
["name"]=> string(1) "b" ,
["code"]=> string(3) "580" ,
["id"]=> string(2) "b1" ,
}
},
[1]=> array(3)
{
[0]=> array(3)
{
["name"]=> string(1) "a" ,
["code"]=> string(3) "416" ,
["id"]=> string(2) "a1" ,
},
[1]=> array(3)
{
["name"]=> string(1) "a" ,
["code"]=> string(3) "522" ,
["id"]=> string(2) "a2" ,
},
[2]=> array(3)
{
["name"]=> string(1) "b" ,
["code"]=> string(3) "899" ,
["id"]=> string(2) "b2",
}
}
);
I have array like this. All I need is for each array (e.g [0]=>array())
I will search for the arrays inside and get the array['code'] only for array set that has distinct name value.
Example for array[0]: I will get the array[0][2]['code'] because the array set of this particular array[0][2]['code'] has a unique 'name'
Assume in the below code $array is $arr[0] from your example:
$array = array(
array(
"name" => "a",
"code" => "416",
"id" => "a1"
),
array(
"name" => "a",
"code" => "522",
"id" => "a2"
),
array(
"name" => "b",
"code" => "580",
"id" => "b1"
)
);
$counts = array_count_values(
array_map(function (array $entry) { return $entry['name']; }, $array)
// or array_column($array, 'name') in PHP 5.5+
);
$uniqueNames = array_keys(
array_filter($counts, function ($count) { return $count == 1; })
);
$result = array_filter($array, function (array $entry) use ($uniqueNames) {
return in_array($entry['name'], $uniqueNames);
});
Not necessarily the super most efficient method, but straight forward and functional. It filters the array down to the entries where name exists only once. This may or may not fit your definition of "unique", it's rather unclear what variations in the input data you may have.
Hi I have a array with lets say 3 arrays with another few arrays inside those like this:
array(3) {
[0]=>
array(2) {
["disease_id"]=>
array(13) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(2) "15"
[6]=>
string(1) "4"
[7]=>
string(1) "3"
[8]=>
string(2) "18"
[9]=>
string(1) "9"
[10]=>
string(1) "2"
[11]=>
string(2) "20"
[12]=>
string(1) "1"
}
["params"]=>
object(stdClass)#160 (39) {
["disease_cpe_5"]=>
string(1) "1"
["disease_mbr_5"]=>
string(4) "1234"
["disease_mtr_5"]=>
string(2) "12"
["disease_cpe_14"]=>
string(1) "1"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_17"]=>
string(1) "4"
["disease_mbr_17"]=>
string(1) "1"
["disease_mtr_17"]=>
string(1) "1"
["disease_cpe_16"]=>
string(1) "4"
["disease_mbr_16"]=>
string(2) "21"
["disease_mtr_16"]=>
string(3) "122"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "1"
["disease_mbr_4"]=>
string(1) "1"
["disease_mtr_4"]=>
string(1) "9"
["disease_cpe_3"]=>
string(1) "7"
["disease_mbr_3"]=>
string(1) "8"
["disease_mtr_3"]=>
string(1) "9"
["disease_cpe_18"]=>
string(1) "1"
["disease_mbr_18"]=>
string(1) "1"
["disease_mtr_18"]=>
string(1) "1"
["disease_cpe_9"]=>
string(1) "3"
["disease_mbr_9"]=>
string(1) "3"
["disease_mtr_9"]=>
string(1) "3"
["disease_cpe_2"]=>
string(1) "3"
["disease_mbr_2"]=>
string(1) "3"
["disease_mtr_2"]=>
string(1) "3"
["disease_cpe_20"]=>
string(2) "10"
["disease_mbr_20"]=>
string(2) "11"
["disease_mtr_20"]=>
string(2) "12"
["disease_cpe_1"]=>
string(1) "1"
["disease_mbr_1"]=>
string(1) "3"
["disease_mtr_1"]=>
string(1) "3"
}
}
[1]=>
array(3) {
["disease_id"]=>
array(8) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(1) "8"
[6]=>
string(2) "15"
[7]=>
string(1) "4"
}
["risk_id"]=>
array(1) {
[0]=>
string(1) "4"
}
["params"]=>
object(stdClass)#235 (27) {
["disease_cpe_5"]=>
string(1) "2"
["disease_mbr_5"]=>
string(1) "1"
["disease_mtr_5"]=>
string(1) "1"
["disease_cpe_14"]=>
string(1) "2"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_17"]=>
string(1) "1"
["disease_mbr_17"]=>
string(1) "1"
["disease_mtr_17"]=>
string(1) "1"
["disease_cpe_16"]=>
string(1) "1"
["disease_mbr_16"]=>
string(1) "5"
["disease_mtr_16"]=>
string(1) "6"
["disease_cpe_8"]=>
string(2) "11"
["disease_mbr_8"]=>
string(1) "1"
["disease_mtr_8"]=>
string(1) "1"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "7"
["disease_mbr_4"]=>
string(1) "8"
["disease_mtr_4"]=>
string(1) "9"
["risk_cpe_4"]=>
string(1) "1"
["risk_mbr_4"]=>
string(1) "2"
["risk_mtr_4"]=>
string(1) "3"
}
}
[2]=>
array(3) {
["disease_id"]=>
array(6) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(1) "8"
[4]=>
string(2) "15"
[5]=>
string(1) "4"
}
["risk_id"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["params"]=>
object(stdClass)#184 (24) {
["disease_cpe_5"]=>
string(1) "2"
["disease_mbr_5"]=>
string(1) "1"
["disease_mtr_5"]=>
string(1) "1"
["disease_cpe_14"]=>
string(1) "2"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_8"]=>
string(2) "11"
["disease_mbr_8"]=>
string(1) "1"
["disease_mtr_8"]=>
string(1) "1"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "7"
["disease_mbr_4"]=>
string(1) "8"
["disease_mtr_4"]=>
string(1) "9"
["risk_cpe_4"]=>
string(1) "1"
["risk_mbr_4"]=>
string(1) "2"
["risk_mtr_4"]=>
string(1) "3"
["risk_cpe_3"]=>
string(1) "5"
["risk_mbr_3"]=>
string(1) "5"
["risk_mtr_3"]=>
string(1) "5"
}
}
}
Now I need to merge these arrays in to one and where the values match up in the ["disease_id"] and ["risk_id"] sub array I should delete the duplicate, yet when the keys match up in the ["params"] object I need to keep both values and yet dump the duplicated key in the object, so that this is kind of the result:
array(1) {
[0]=>
array(2) {
["disease_id"]=>
array(13) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(2) "15"
[6]=>
string(1) "4"
[7]=>
string(1) "3"
[8]=>
string(2) "18"
[9]=>
string(1) "9"
[10]=>
string(1) "2"
[11]=>
string(2) "20"
[12]=>
string(1) "1"
[13]=>
string(1) "8"
}
["risk_id"]=>
array(1) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["params"]=>
object(stdClass)#160 (39) {
["disease_cpe_5"]=>
string(7) "1, 1, 2"
["disease_mbr_5"]=>
string(10) "1234, 1, 1"
["disease_mtr_5"]=>
string(8) "12, 1, 1"
["disease_cpe_14"]=>
string(7) "1, 2, 2"
["disease_mbr_14"]=>
string(7) "2, 2, 2"
["disease_mtr_14"]=>
string(7) "2, 2, 2"
["disease_cpe_12"]=>
string(10) "12, 12, 12"
["disease_mbr_12"]=>
string(10) "12, 12, 12"
["disease_mtr_12"]=>
string(10) "12, 12, 12"
["disease_cpe_17"]=>
string(4) "4, 1"
["disease_mbr_17"]=>
string(4) "1, 1"
["disease_mtr_17"]=>
string(4) "1, 1"
["disease_cpe_16"]=>
string(4) "4, 1"
["disease_mbr_16"]=>
string(5) "21, 5"
["disease_mtr_16"]=>
string(3) "122,6"
["disease_cpe_8"]=>
string(6) "11, 11"
["disease_mbr_8"]=>
string(1) "1, 1"
["disease_mtr_8"]=>
string(1) "1, 1"
["disease_cpe_15"]=>
string(13) "132, 132, 132"
["disease_mbr_15"]=>
string(13) "132, 132, 132"
["disease_mtr_15"]=>
string(7) "1, 1, 1"
["disease_cpe_4"]=>
string(7) "1, 7, 7"
["disease_mbr_4"]=>
string(7) "1, 8, 8"
["disease_mtr_4"]=>
string(7) "9, 9, 9"
["disease_cpe_3"]=>
string(1) "7"
["disease_mbr_3"]=>
string(1) "8"
["disease_mtr_3"]=>
string(1) "9"
["disease_cpe_18"]=>
string(1) "1"
["disease_mbr_18"]=>
string(1) "1"
["disease_mtr_18"]=>
string(1) "1"
["disease_cpe_9"]=>
string(1) "3"
["disease_mbr_9"]=>
string(1) "3"
["disease_mtr_9"]=>
string(1) "3"
["disease_cpe_2"]=>
string(1) "3"
["disease_mbr_2"]=>
string(1) "3"
["disease_mtr_2"]=>
string(1) "3"
["disease_cpe_20"]=>
string(2) "10"
["disease_mbr_20"]=>
string(2) "11"
["disease_mtr_20"]=>
string(2) "12"
["disease_cpe_1"]=>
string(1) "1"
["disease_mbr_1"]=>
string(1) "3"
["disease_mtr_1"]=>
string(1) "3"
["risk_cpe_4"]=>
string(1) "1, 1"
["risk_mbr_4"]=>
string(1) "2, 2"
["risk_mtr_4"]=>
string(1) "3, 3"
["risk_cpe_3"]=>
string(1) "5"
["risk_mbr_3"]=>
string(1) "5"
["risk_mtr_3"]=>
string(1) "5"
}
}
}
I have tried many thing but have not found an answer that effectively solves this issue. Here is one of my attempts:
$newArray = array();
if( !is_object($newArray["parpams"]) ){
$newArray["parpams"] = new StdClass();
}
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$has = (count(array_intersect($value["disease_id"], $value_i["disease_id"]))) ? true : false;
if($has){
foreach ($value["disease_id"] as $pointer => $disease){
if (in_array($disease, $value["disease_id"])){
$newArray["disease_id"][] = $selected[$key]["disease_id"][$pointer];
unset($selected[$key]["disease_id"][$pointer]);
$cpe = "disease_cpe_".$disease;
$mbr = "disease_mbr_".$disease;
$mtr = "disease_mtr_".$disease;
if ($newArray["parpams"]->$cpe){
$newArray["parpams"]->$cpe = $newArray["parpams"]->$cpe.', '. $selected[$key]["params"]->$cpe;
} else {
$newArray["parpams"]->$cpe = $selected[$key]["params"]->$cpe;
}
if ($newArray["parpams"]->$mbr){
$newArray["parpams"]->$mbr = $newArray["parpams"]->$mbr.', '. $selected[$key]["params"]->$mbr;
} else {
$newArray["parpams"]->$mbr = $selected[$key]["params"]->$mbr;
}
if ($newArray["parpams"]->$mtr){
$newArray["parpams"]->$mtr = $newArray["parpams"]->$mtr.', '. $selected[$key]["params"]->$mtr;
} else {
$newArray["parpams"]->$mtr = $selected[$key]["params"]->$mtr;
}
unset($selected[$key]["params"]->$cpe);
unset($selected[$key]["params"]->$mbr);
unset($selected[$key]["params"]->$mtr);
}
}
}
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$has = (count(array_intersect($value["risk_id"], $value_i["risk_id"]))) ? true : false;
if($has){
foreach ($value["risk_id"] as $pointer => $risk){
if (in_array($risk, $value["risk_id"])){
$newArray["risk_id"][] = $selected[$key]["risk_id"][$pointer];
unset($selected[$key]["risk_id"][$pointer]);
$cpe = "risk_cpe_".$risk;
$mbr = "risk_mbr_".$risk;
$mtr = "risk_mtr_".$risk;
if ($newArray["parpams"]->$cpe){
$newArray["parpams"]->$cpe = $newArray["parpams"]->$cpe.', '. $selected[$key]["params"]->$cpe;
} else {
$newArray["parpams"]->$cpe = $selected[$key]["params"]->$cpe;
}
if ($newArray["parpams"]->$mbr){
$newArray["parpams"]->$mbr = $newArray["parpams"]->$mbr.', '. $selected[$key]["params"]->$mbr;
} else {
$newArray["parpams"]->$mbr = $selected[$key]["params"]->$mbr;
}
if ($newArray["parpams"]->$mtr){
$newArray["parpams"]->$mtr = $newArray["parpams"]->$mtr.', '. $selected[$key]["params"]->$mtr;
} else {
$newArray["parpams"]->$mtr = $selected[$key]["params"]->$mtr;
}
unset($selected[$key]["params"]->$cpe);
unset($selected[$key]["params"]->$mbr);
unset($selected[$key]["params"]->$mtr);
}
}
}
}
}
}
Here is another attempt:
$newArray = array();
$newArray["disease_id"] = array();
$newArray["risk_id"] = array();
if( !is_object($newArray["parpams"]) ){
$newArray["parpams"] = new StdClass();
}
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$has = (count(array_intersect($value["disease_id"], $value_i["disease_id"]))) ? true : false;
if($has){
$newArray["disease_id"] = array_merge($value["disease_id"], $value_i["disease_id"],$newArray["disease_id"]);
}
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$has = (count(array_intersect($value["risk_id"], $value_i["risk_id"]))) ? true : false;
if($has){
$newArray["risk_id"] = array_merge($value["risk_id"], $value_i["risk_id"],$newArray["risk_id"]);
}
}
}
$newArray["disease_id"] = array_unique($newArray["disease_id"]);
$newArray["risk_id"] = array_unique($newArray["risk_id"]);
}
}
out putt was:
array(3) {
["disease_id"]=>
array(14) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(1) "8"
[4]=>
string(2) "15"
[5]=>
string(1) "4"
[21]=>
string(2) "17"
[22]=>
string(2) "16"
[39]=>
string(1) "3"
[40]=>
string(2) "18"
[41]=>
string(1) "9"
[42]=>
string(1) "2"
[43]=>
string(2) "20"
[44]=>
string(1) "1"
}
["risk_id"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["parpams"]=>
object(stdClass)#236 (0) {
}
}
But the ['params'] are still not included.
I have read many treads on stackoverflow, but non actuality address this complexity. If I missed a thread please point me in that direction. Please do remember that these arrays can become as much as twenty arrays. Tanks!
I think this is the answer if it can be of any help to any one else.
$newArray = array();
$newArray["disease_id"] = array();
$newArray["risk_id"] = array();
$newArray["params"] = array();
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$newArray["disease_id"] = array_merge($value["disease_id"], $value_i["disease_id"],$newArray["disease_id"]);
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$newArray["risk_id"] = array_merge($value["risk_id"], $value_i["risk_id"],$newArray["risk_id"]);
}
}
if(is_object($value["params"])){
$newArray["params"] = array_merge_recursive((array) $value["params"], (array) $newArray["params"]);
}
}
$newArray_risk = array_unique($newArray["risk_id"]);
if(sort($newArray_risk)){
$newArray["risk_id"]= array();
foreach ($newArray_risk as $risk_id){
$newArray["risk_id"][] = $risk_id;
}
}
$newArray_disease = array_unique($newArray["disease_id"]);
if(sort($newArray_disease)){
$newArray["disease_id"] = array();
foreach ($newArray_disease as $disease_id){
$newArray["disease_id"][] = $disease_id;
}
}
foreach ($newArray["params"] as $key => $value){
if (is_array($value)){
$i=0;
foreach ($value as $val){
if (!is_array($val)){
if ($i == 0){
$newArray["params"][$key] = $val;
} else {
$newArray["params"][$key] .= ', '.$val;
}
$i++;
}
}
}
}
$newArray["params"] = (object)$newArray["params"];
}
This question already has answers here:
Recursive function to generate multidimensional array from database result
(5 answers)
Closed 9 years ago.
I have an array that looks something like:
$someArray = array(
array(
"id"=> 1,
"name"=> "somename1",
"parent"=> 0
),
array(
"id"=> 53,
"name"=> "somename2",
"parent"=> 1
),
array(
"id"=> 921,
"name"=> "somename3",
"parent"=> 53,
)
.
.
.
.
.
.
);
Of course, there are more cells in the array this is just a small portion.
I am trying to turn this array to something like:
$someArray = array(
array(
"id"=> 1,
"name"=> "somename1",
"parent"=> 0,
"children" => array(
array(
"id"=> 53,
"name"=> "somename2",
"parent"=> 1,
"children" => array(
array(
"id"=> 921,
"name"=> "somename3",
"parent"=> 53,
"children" => array(
)
)
)
)
)
)
.
.
.
.
.
.
);
/*
Gets reversed array,
Returns multidimensional tree array.
*/
function buildTree($parts) {
if (count($parts) == 1) {
return $parts[0];
}
$last_item = array_pop($parts);
$last_item[] = buildTree($parts);
return $last_item;
}
Testing:
$parts = array(
array('1','2','3',5),
array('3','8','3',1),
array('1', 5,'2','3'),
array('D','2','3',5),
array('A','2','3',5)
);
var_dump(buildTree(array_reverse($parts)));
Output:
array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> int(5) [4]=> array(5) {
[0]=> string(1) "3" [1]=> string(1) "8" [2]=> string(1) "3" [3]=> int(1) [4]=> array(5) {
[0]=> string(1) "1" [1]=> int(5) [2]=> string(1) "2" [3]=> string(1) "3" [4]=> array(5) {
[0]=> string(1) "D" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> int(5) [4]=> array(4) {
[0]=> string(1) "A" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> int(5)
} } } } }