I have the following array
$cal = array(
0 => array(
0 => array(
0 => '*',
1 => 2,
2 => 3,
) ,
1 => array(
0 => 6,
1 => 7,
2 => 8,
3 => 9,
4 => 10,
) ,
2 => array(
0 => '*',
1 => '*',
2 => 15,
3 => '*',
4 => 17,
) ,
3 => array(
0 => 20,
1 => 21,
2 => 22,
3 => 23,
4 => 24,
) ,
4 => array(
0 => 27,
1 => 28,
2 => 29,
3 => 30,
4 => 31,
) ,
) ,
1 => array(
0 => array() ,
1 => array(
0 => 3,
1 => 4,
2 => 5,
3 => '*',
4 => 7,
) ,
2 => array(
0 => 10,
1 => 11,
2 => 12,
3 => 13,
4 => 14,
) ,
3 => array(
0 => 17,
1 => 18,
2 => 19,
3 => 20,
4 => 21,
) ,
4 => array(
0 => 24,
1 => 25,
2 => 26,
3 => 27,
4 => 28,
) ,
) ,
2 => array(
0 => array() ,
1 => array(
0 => 3,
1 => 4,
2 => 5,
3 => 6,
4 => '*',
) ,
2 => array(
0 => 10,
1 => 11,
2 => 12,
3 => 13,
4 => 14,
) ,
3 => array(
0 => 17,
1 => 18,
2 => 19,
3 => 20,
4 => 21,
) ,
4 => array(
0 => 24,
1 => 25,
2 => 26,
3 => 27,
4 => 28,
) ,
5 => array(
0 => 31,
) ,
) ,
3 => array(
0 => array(
0 => 1,
1 => 2,
2 => 3,
3 => 4,
) ,
1 => array(
0 => 7,
1 => 8,
2 => 9,
3 => 10,
4 => 11,
) ,
2 => array(
0 => 14,
1 => 15,
2 => 16,
3 => 17,
4 => 18,
) ,
3 => array(
0 => 21,
1 => 22,
2 => 23,
3 => 24,
4 => 25,
) ,
4 => array(
0 => 28,
1 => 29,
2 => 30,
) ,
) ,
4 => array(
0 => array(
0 => 1,
1 => 2,
) ,
1 => array(
0 => '*',
1 => '*',
2 => '*',
3 => 8,
4 => 9,
) ,
2 => array(
0 => 12,
1 => 13,
2 => 14,
3 => 15,
4 => 16,
) ,
3 => array(
0 => 19,
1 => 20,
2 => 21,
3 => 22,
4 => 23,
) ,
4 => array(
0 => 26,
1 => 27,
2 => 28,
3 => 29,
4 => 30,
) ,
) ,
5 => array(
0 => array() ,
1 => array(
0 => 3,
1 => '*',
2 => 5,
3 => 6,
) ,
2 => array(
0 => 9,
1 => 10,
2 => 11,
3 => 12,
4 => '*',
) ,
3 => array(
0 => 16,
1 => 17,
2 => 18,
3 => 19,
4 => 20,
) ,
4 => array(
0 => 23,
1 => 24,
2 => 25,
3 => 26,
4 => 27,
) ,
5 => array(
0 => 30,
) ,
) ,
6 => array(
0 => array(
0 => 1,
1 => 2,
2 => 3,
3 => 4,
) ,
1 => array(
0 => 7,
1 => 8,
2 => 9,
3 => 10,
4 => 11,
) ,
2 => array(
0 => 14,
1 => 15,
2 => 16,
3 => 17,
4 => 18,
) ,
3 => array(
0 => 21,
1 => 22,
2 => 23,
3 => 24,
4 => 25,
) ,
4 => array(
0 => 28,
1 => 29,
2 => 30,
3 => 31,
) ,
) ,
7 => array(
0 => array(
0 => 1,
) ,
1 => array(
0 => 4,
1 => 5,
2 => 6,
3 => 7,
4 => 8,
) ,
2 => array(
0 => 11,
1 => 12,
2 => 13,
3 => 14,
4 => 15,
) ,
3 => array(
0 => 18,
1 => 19,
2 => 20,
3 => 21,
4 => 22,
) ,
4 => array(
0 => 25,
1 => 26,
2 => 27,
3 => 28,
4 => 29,
) ,
5 => array() ,
) ,
8 => array(
0 => array(
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
) ,
1 => array(
0 => 8,
1 => 9,
2 => 10,
3 => 11,
4 => 12,
) ,
2 => array(
0 => 15,
1 => 16,
2 => 17,
3 => 18,
4 => 19,
) ,
3 => array(
0 => 22,
1 => 23,
2 => 24,
3 => 25,
4 => 26,
) ,
4 => array(
0 => 29,
1 => 30,
) ,
) ,
9 => array(
0 => array(
0 => 1,
1 => 2,
2 => 3,
) ,
1 => array(
0 => '*',
1 => '*',
2 => '*',
3 => '*',
4 => '*',
) ,
2 => array(
0 => '*',
1 => '*',
2 => '*',
3 => '*',
4 => '*',
) ,
3 => array(
0 => 20,
1 => 21,
2 => 22,
3 => 23,
4 => 24,
) ,
4 => array(
0 => 27,
1 => 28,
2 => 29,
3 => 30,
4 => 31,
) ,
) ,
10 => array(
0 => array() ,
1 => array(
0 => 3,
1 => '*',
2 => 5,
3 => 6,
4 => 7,
) ,
2 => array(
0 => 10,
1 => 11,
2 => 12,
3 => 13,
4 => 14,
) ,
3 => array(
0 => 17,
1 => 18,
2 => 19,
3 => 20,
4 => 21,
) ,
4 => array(
0 => 24,
1 => 25,
2 => 26,
3 => '*',
4 => 28,
) ,
5 => array() ,
) ,
11 => array(
0 => array(
0 => '*',
1 => 2,
2 => 3,
3 => 4,
4 => '*',
) ,
1 => array(
0 => 8,
1 => 9,
2 => 10,
3 => 11,
4 => 12,
) ,
2 => array(
0 => 15,
1 => 16,
2 => 17,
3 => 18,
4 => 19,
) ,
3 => array(
0 => 22,
1 => 23,
2 => 24,
3 => 25,
4 => 26,
) ,
4 => array(
0 => 29,
1 => 30,
2 => 31,
) ,
) ,
)
I'm trying to remove empty array from its parent using the following code:
for($m = 0; $m < count($cal); $m++){
for($w = 0; $w < count($cal[$m]); $w++){
if(empty($cal[$m][$w])){
array_shift($cal[$m]);
}
}
}
the result is (please see index 10)
10 => array(
0 => array(
0 => 10,
1 => 11,
2 => 12,
3 => 13,
4 => 14,
) ,
1 => array(
0 => 17,
1 => 18,
2 => 19,
3 => 20,
4 => 21,
) ,
2 => array(
0 => 24,
1 => 25,
2 => 26,
3 => '*',
4 => 28,
) ,
3 => array() ,
) ,
what i expect is remove the first and the last, it is instead remove first and second array and leave the last array still there. any idea?
Try to use array_filter() instead since array_shift() just takes care of the first element:
foreach ($cal as &$value) {
$value = array_filter($value);
}
You can use a simple recursive function to traverse each like this:
<?php
function removeEmpties(&$array)
{
foreach($array as $key=>$val)
{
if(count($val)==0)
{
unset($array[$key]);
}
else
{
removeEmpties($val);
}
}
}
$yourArray=array( ... etc );
removeEmpties($yourArray);
?>
try this code:
foreach ($cal as &$arr)
{
$arr = array_filter($arr);
}
$cal = array_filter($cal);
Use unset($cal[$m][$w]) instead of array_shift($cal[$m]).
This will result in
10 => array(
1 => array(
0 => 3,
1 => '*',
2 => 5,
3 => 6,
4 => 7,
) ,
2 => array(
0 => 10,
1 => 11,
2 => 12,
3 => 13,
4 => 14,
) ,
3 => array(
0 => 17,
1 => 18,
2 => 19,
3 => 20,
4 => 21,
) ,
4 => array(
0 => 24,
1 => 25,
2 => 26,
3 => '*',
4 => 28,
) ,
) ,
Notice the key [0] is no longer present
Related
I recently asked this question and got a working answer. However I have another question just a bit different from the previous one.
I have this $data, which holds an array of records from schedules table.
If you want to see the code that generates this array, you can see it from this file from LaravelDaily, it's the same code I'm working on.
The content of $data array below contains a schedule with the ids of 9 and 11.
Schedule Id 9 has the starting time of 07:00 AM and ending time of 08:00 AM (Wednesday).
Schedule Id 11 has the starting time of 07:00 AM (the same with Schedule Id 9 but only differs in the Day (Friday). I didn't include the ending time of this in the array.
$data = [
"06:00 AM - 06:05 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1,
],
"06:05 AM - 06:10 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:10 AM - 06:15 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:15 AM - 06:20 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:20 AM - 06:25 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:25 AM - 06:30 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:30 AM - 06:35 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:35 AM - 06:40 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:40 AM - 06:45 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:45 AM - 06:50 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:50 AM - 06:55 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:55 AM - 07:00 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"07:00 AM - 07:05 AM" => [
0 => 1,
1 => 1,
2 => [
"schedule_id" => 11,
"subject_name" => "Subject Abc",
"rowspan" => 12
],
3 => 1,
4 => [
"schedule_id" => 9,
"subject_name" => "Subject Bac",
"rowspan" => 24
]
],
"07:05 AM - 07:10 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:10 AM - 07:15 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:15 AM - 07:20 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:20 AM - 07:25 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:25 AM - 07:30 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:30 AM - 07:35 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:35 AM - 07:40 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:40 AM - 07:45 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:45 AM - 07:50 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:50 AM - 07:55 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:55 AM - 08:00 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
]
]
The previous question, what I want is to filter only the array that has another array inside it.
But now I want to remove array element if all of it's index contains only the value of 1.
Here's what it looks like:
// to be removed from the $data array
$data = [
"06:00 AM - 06:05 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:05 AM - 06:10 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:10 AM - 06:15 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:15 AM - 06:20 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:20 AM - 06:25 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:25 AM - 06:30 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:30 AM - 06:35 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:35 AM - 06:40 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:40 AM - 06:45 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:45 AM - 06:50 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:50 AM - 06:55 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
],
"06:55 AM - 07:00 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
]
]
The final form of the $data array is shown below.
$data = [
"07:00 AM - 07:05 AM" => [
0 => 1,
1 => 1,
2 => [
"schedule_id" => 11,
"subject_name" => "Subject Abc",
"rowspan" => 12
],
3 => 1,
4 => [
"schedule_id" => 9,
"subject_name" => "Subject Bac",
"rowspan" => 24
]
],
"07:05 AM - 07:10 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:10 AM - 07:15 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:15 AM - 07:20 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:20 AM - 07:25 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:25 AM - 07:30 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:30 AM - 07:35 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:35 AM - 07:40 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:40 AM - 07:45 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:45 AM - 07:50 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:50 AM - 07:55 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
],
"07:55 AM - 08:00 AM" => [
0 => 1,
1 => 1,
2 => 0,
3 => 1,
4 => 0
]
]
Q: Why I want to remove it?
A: Because I don't want to show it in the schedule table, only the time taken from the schedule will be shown.
Q: Why not stick to the answer on your previous question?
A: I realized that I need the other array of time that is doesn't have an array inside it but has the value of 0, because the 0 is telling that that time has a schedule in it, I mean it's between the start_time and end_time of a schedule.
EDIT:
I've got a workaround as of the moment, if you have better answer feel free to post it.
The code below successfully removed all elements that holds the value of like this:
[
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1
]
The code I have:
$data = [...];
$filtered = array_filter($data, fn ($el) => hasSchedule($el));
function hasSchedule($period) {
for($i = 0; $i < count($period); $i++) {
if(is_array($period[$i])) {
return true;
} else {
$sum = array_sum($period);
if ($sum == 5) {
return false;
} else {
return true;
}
}
}
return false;
}
You can use a non-loop approach using array_filter and a custom function.
To remove all 1s, we can use the array_unique function and check the result if was only one item in the result array and the first element of areay was equal to 1.
$list = [
"07:00 AM - 07:05 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 0,
4 => 1,
],
"07:05 AM - 07:10 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1,
],
"07:10 AM - 07:15 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1,
],
"07:15 AM - 07:20 AM" => [
0 => 1,
1 => 1,
2 => [
"schedule_id" => 10,
"time" => "07:15 AM - 07:20 AM",
"subject_name" => "Subject Abc",
],
3 => 1,
4 => 1,
]
];
function removeAllOnes($arr)
{
if (array_filter($arr, 'is_array'))
{
return true;
}
$_new = array_unique($arr);
return !(count($_new) == 1 && $_new[0] === 1);
}
$filtered = array_filter($list, 'removeAllOnes');
print_r($filtered);
I posted this on the reddit question, but i'm going to post it here too to see if any SO users have any comments on it.
This works because array_filter will filter out empty arrays.
$data = array_filter($data, function($period) {
return array_filter($period, function($item) {
return $item !== 1;
});
});
I have a result PHP array like this
$listResult = array(
0 => array(
0 => 1,
1 => 15,
2 => 4,
3 => 52
),
1 => array(
0 => 2,
1 => 0,
2 => 3,
3 => 2
),
2 => array(
0 => 3,
1 => 5,
2 => 9,
3 => 1
),
3 => array(
0 => 4,
1 => 10,
2 => 2,
3 => 5
),
4 => array(
0 => 1,
1 => 1,
2 => 2,
3 => 3
));
How to sum each keys? The result should be like this:
$sumResult = array(
0 => 11,
1 => 31,
2 => 20,
3 => 63
);
Already search in stackoverflow but the answer required key, in this case I just want to sum each index dynamically without a key. Sorry if my english is bad.
Use array_column() to get an array of all the elements with the same key, and array_sum() to sum them.
$sumResult = [];
foreach (array_keys($listResult[0]) as $key) {
$sumResult[$key] = array_sum(array_column($listResult, $key));
}
I tried to make table from PHP array. But here all key are dynamic and have also child array
Here is the array structure:
array (
1371618448317 =>
array (
0 =>
array (
0 => '23.77311734',
1 => '90.396355125',
2 => '23.77313316',
3 => '90.396411867187',
4 => '23.77309048',
5 => '90.396419484375',
6 => '23.77307348',
7 => '90.3963645',
),
1 => 20911,
2 =>
array (
1371618713208 =>
array (
0 => 1,
1 => 'BRAC Delivery Centre',
2 => '371/A Shahinbag',
3 => 25,
4 => 91,
5 => 221,
6 => 1,
7 => 11,
8 => 1,
9 => 99,
10 => 1,
11 => 99,
12 => 99,
13 => 99,
14 => 99,
15 => 99,
16 => 1,
),
),
),
1371619410448 =>
array (
0 =>
array (
0 => '23.77894566',
1 => '90.39968559375',
2 => '23.77916362',
3 => '90.400307765625',
4 => '23.77889887',
5 => '90.400401515625',
6 => '23.77870083',
7 => '90.399780515625',
),
1 => 24612,
2 =>
array (
1371619950162 =>
array (
0 => 1,
1 => 'EPI Centre (Govt.)',
2 => ' Mohakhali Road Mohakhali',
3 => 20,
4 => 91,
5 => 11,
6 => 1,
7 => 12,
8 => 1,
9 => 99,
10 => 1,
11 => 99,
12 => 99,
13 => 99,
14 => 99,
15 => 99,
16 => 0,
),
),
),
1371621080807 =>
array (
0 =>
array (
0 => '23.77746206',
1 => '90.399232078125',
2 => '23.77744917',
3 => '90.399623390625',
4 => '23.77712934',
5 => '90.39958940625',
6 => '23.77714809',
7 => '90.399205125',
),
1 => 24566,
2 =>
array (
1371621897771 =>
array (
0 => 1,
1 => 'Society for Assistance to Hearing Impaired Children (SAHIC)',
2 => 'N/A Sattola Road Mohakhali',
3 => 20,
4 => 91,
5 => 222,
6 => 1,
7 => 7,
8 => 1,
9 => 1,
10 => 1,
11 => 1,
12 => 99,
13 => 99,
14 => 99,
15 => 1,
16 => 0,
),
),
),
1371622305777 =>
array (
0 =>
array (
0 => '23.77357261',
1 => '90.36189965625',
2 => '23.77359605',
3 => '90.36197925',
4 => '23.77344028',
5 => '90.36201675',
6 => '23.77341802',
7 => '90.361931296875',
),
1 => 1325,
2 =>
array (
1371622497359 =>
array (
0 => 1,
1 => 'Natoinal Health Care Network',
2 => '3/Ka Pisiculture Housing Society Shamoly',
3 => 29,
4 => 91,
5 => 222,
6 => 1,
7 => 7,
8 => 1,
9 => 99,
10 => 99,
11 => 1,
12 => 99,
13 => 99,
14 => 1,
15 => 1,
16 => 0,
),
),
)
My Desire tale will be look like:
BRAC Delivery Centre 371/A Shahinbag 23.77311734 90.396355125
EPI Centre (Govt.) Mohakhali Road 23.77746206 90.399232078125
from lat long array I always try to collect first lat long.
Can any one give me solution ?
Here is your solution
Input
$array = array(
1371618448317 => array (
array ('23.77311734','90.396355125','23.77313316','90.396411867187','23.77309048','90.396419484375','23.77307348','90.3963645'),
20911,
array ('1371618713208' => array (1,'BRAC Delivery Centre','371/A Shahinbag',25,91,221,1,11,1,99,1,99,99,99,99,99,1))
),
1371619410448 => array (
array ('23.77894566','90.39968559375','23.77916362','90.400307765625','23.77889887','90.400401515625','23.77870083','90.399780515625',),
24612,
array ('1371619950162' => array (1,'EPI Centre (Govt.)',' Mohakhali Road Mohakhali',20,91,11,1,12,1,99,1,99,99,99,99,99,0))
),
1371621080807 => array(
array ('23.77746206','90.399232078125','23.77744917','90.399623390625','23.77712934','90.39958940625','23.77714809','90.399205125'),
24566,
array ('1371621897771' => array (1,
'Society for Assistance to Hearing Impaired Children (SAHIC)','N/A Sattola Road Mohakhali',20,91,222,1,7,1,1,1,1,99,99,99,1,0)
)
),
1371622305777 => array (
array ('23.77357261','90.36189965625','23.77359605','90.36197925','23.77344028','90.36201675','23.77341802','90.361931296875'),
1325,
array ('1371622497359' => array (1,
'Natoinal Health Care Network','3/Ka Pisiculture Housing Society Shamoly',29,91,222,1,7,1,99,99,1,99,99,1,1,0)
)
)
);
Solution
Coordinates are in 1st array.
Name and address is in 3rd.
Coordinates picks easily by $row[0][0].','.$row[0][1]; in given loop below.
For address and name you need to pick key of 3rd array, for this here I used $row[2][key($row[2]);
Now check the code below.
$new_array = array();
$i=0;
foreach($array as $key => $row){
//echo "<pre>";print_r(key($row[2]));
$new[$i]['coordinates'] = $row[0][0].','.$row[0][1];
$new[$i]['name'] = $row[2][key($row[2])][1];
$new[$i]['address'] = $row[2][key($row[2])][2];
$i++;
}
echo "<pre>";print_r($new);
Output
Array
(
[0] => Array
(
[coordinates] => 23.77311734,90.396355125
[name] => BRAC Delivery Centre
[address] => 371/A Shahinbag
)
[1] => Array
(
[coordinates] => 23.77894566,90.39968559375
[name] => EPI Centre (Govt.)
[address] => Mohakhali Road Mohakhali
)
[2] => Array
(
[coordinates] => 23.77746206,90.399232078125
[name] => Society for Assistance to Hearing Impaired Children (SAHIC)
[address] => N/A Sattola Road Mohakhali
)
[3] => Array
(
[coordinates] => 23.77357261,90.36189965625
[name] => Natoinal Health Care Network
[address] => 3/Ka Pisiculture Housing Society Shamoly
)
)
solved myself:
$assoc = true;
$result = json_decode ($json, $assoc);
echo "<table border='1'>";
echo "<tr><td>Hospital Name</td><td>Address</td><td>Lat1</td><td>Long1</td><td>Lat2</td><td>Long1</td><td>Lat3</td><td>Long3</td><td>Lat4</td><td>Long4</td></tr>";
foreach($result as $single_res){
$lat1 = $single_res[0][0];
$long1 = $single_res[0][1];
$lat2 = $single_res[0][2];
$long2 = $single_res[0][3];
$lat3 = $single_res[0][4];
$long3 = $single_res[0][5];
$lat4 = $single_res[0][6];
$long4 = $single_res[0][7];
$key = $single_res[2];
$key_val = array_keys($key);
$key_val = $key_val[0];
$name = $key[$key_val][1];
$address = $key[$key_val][2];
I have this array which contains 2 keys and values..
array (
23 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
),
1 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 14,
8 => 12,
9 => 1,
10 => 7,
11 => 10,
12 => 8,
13 => 3,
)
)
So How can I convert this into single query with distinct values like this
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
20 => 14,
)
That means it should be first merge and then create distinct values array without using more foreach/for loop.
This is the code I have tried http://codepad.org/x881cBt1
You can use array_walk_recursive to flatten your multidimensional array:
$flatten = [];
array_walk_recursive($array, function ($value) use (&$flatten) {
$flatten[] = $value;
});
$flatten = array_unique($flatten); //Taking Unique for the flattened array
print_r($flatten);
This should give:
Array
(
[0] => 9
[1] => 13
[2] => 2
[3] => 11
[4] => 4
[5] => 5
[6] => 6
[7] => 12
[8] => 1
[9] => 7
[10] => 10
[11] => 8
[12] => 3
[20] => 14
)
Check EVAL
<?php
$data; //Your array
$data_set = array_values($data);
$required_data = [];
for ($i=0; $i< count($data_set); $i++) {
$required_data = array_merge($required_data, $data_set[$i]);
unset($data_set[$i]);
}
var_dump($required_data);
This will solve the issue
$arr;//Your array
$final_arr=array();
foreach($arr as $r){
$r=array_unique($r);
$final_arr=array_merge($final_arr,$r);
}
$final_arr=array_unique($final_arr);
print_r($final_arr);
see it live ideone
//Here 1st you have to Merge array & then remove duplicate entry
<?php
$main_array=array("0"=>array(1,2,3,4),"1"=>array(2,6,4),"2"=>array(2,8,5));
$temp_array=array();
for($i=0;$i<count($main_array);$i++)
{
$temp_array=array_merge($temp_array,$main_array[$i]);
}
//var_dump($temp_array);
$final_array=array_unique($temp_array);
echo "array:";
var_dump($final_array);
?>
you can use this code as you said this code is without any loop
$myArray = array (
23 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
),
1 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 14,
8 => 12,
9 => 1,
10 => 7,
11 => 10,
12 => 8,
13 => 3,
)
);
$objTmp = (object) array('array' => array());
array_walk_recursive($myArray, create_function('&$v, $k, &$t', '$t->array[] = $v;'), $objTmp);
print_r (array_unique($objTmp->array));
/* output
Array
(
[0] => 9
[1] => 13
[2] => 2
[3] => 11
[4] => 4
[5] => 5
[6] => 6
[7] => 12
[8] => 1
[9] => 7
[10] => 10
[11] => 8
[12] => 3
[20] => 14
)
*/
Thank you..
I need to append this array in php script
.Kindly help to do it perfectly .I tried multiple solution but none resulted to correct solution.
Array(
[Donn] => 0
[Lamo] => 0
[Otis] => 0
[Stev] => 0
[Matt] => 0
[Samm] => 0
[Andr] => 0
[Jerr] => 0
[Simm] => 0
[Steph] => 0
[Fredd] => 0
[Willi] => 0
)
to the following array
Array(
[Don] => Array
(
[Ab] => 1
[Ang] => 1
[Ant] => 2
[Bo] => 1
[Ch] => 1
[Chri] => 2
[Chri] => 4
[Deau] => 1
[Der] => 1
[Sylveste] => 1
)
[Lam] => Array
(
[Ab] => 2
[Ch] => 22
[Dona] => 1
[Irw] => 1
[Kou] => 1
[Llo] => 1
[Ro] => 1
[Shumy] => 1
)
[Oti] => Array
(
[Ab] => 1
[Arla] => 1
[Kour] => 1
[Osh] => 1
[Roy ] => 1
[Tim] => 1
[War] => 1
//add the given array here
))
So that the result is
Array([Don] => Array
(
[Ab] => 1
[Ang] => 1
[Ant] => 2
[Bo] => 1
[Ch] => 1
[Chri] => 2
[Chri] => 4
[Deau] => 1
[Der] => 1
[Sylveste] => 1
)
[Lam] => Array
(
[Ab] => 2
[Ch] => 22
[Dona] => 1
[Irw] => 1
[Kou] => 1
[Llo] => 1
[Ro] => 1
[Shumy] => 1
)
[Oti] => Array
(
[Ab] => 1
[Arla] => 1
[Kour] => 1
[Osh] => 1
[Roy ] => 1
[Tim] => 1
[War] => 1
[Donn] => 0
[Lamo] => 0
[Otis] => 0
[Stev] => 0
[Matt] => 0
[Samm] => 0
[Andr] => 0
[Jerr] => 0
[Simm] => 0
[Steph] => 0
[Fredd] => 0
[Willi] => 0
//added here
))
Please use least loops as much as possible.So as to get time feasible solution.Use php array function if possible.
Use foreach to add into the oti index:
<?php
$firstArr = Array(
"Don" => Array
(
"Ab" => 1,
"Ang" => 1,
"Ant" => 2,
"Bo" => 1,
"Ch" => 1,
"Chri" => 2,
"Chri" => 4,
"Deau" => 1,
"Der" => 1,
"Sylveste" => 1,
),
"Lam" => Array
(
"Ab" => 2,
"Ch" => 22,
"Dona" => 1,
"Irw" => 1,
"Kou" => 1,
"Llo" => 1,
"Ro" => 1,
"Shumy" => 1,
),
"Oti" => Array
(
"Ab" => 1,
"Arla" => 1,
"Kour" => 1,
"Osh" => 1,
"Roy " => 1,
"Tim" => 1,
"War" => 1,
//add the given array here
));
$secondArr = Array(
"Donn" => 0,
"Lamo" => 0,
"Otis" => 0,
"Stev" => 0,
"Matt" => 0,
"Samm" => 0,
"Andr" => 0,
"Jerr" => 0,
"Simm" => 0,
"Steph" => 0,
"Fredd" => 0,
"Willi" => 0,
);
foreach ($secondArr as $key => $value) {
$firstArr["Oti"][$key] = $value;
}
print_r($firstArr);
Demo
You can also use array_merge as #Mark Baker said in comments:
$firstArr["Oti"] = array_merge($firstArr["Oti"], $secondArr);
This should solve it, you use the array_merge function to merge the 2 arrays :)
$secondArray['Oti'] = array_merge($secondArray['Oti'], $firstArray);