Hello i am trying to make an php application, but i am having some trouble with arrays
Actually,
I m trying to compare array key & if found replace its value & merge whole array into one.
For example
Array ONE-
Array
(
[0] => Array
(
[label] => July 20 2020
[y] => 3
)
[1] => Array
(
[label] => July 18 2020
[y] => 1
)
)
Array TWO-
Array
(
[0] => Array
(
[label] => July 18 2020
[y] => 0
)
[1] => Array
(
[label] => July 19 2020
[y] => 0
)
[2] => Array
(
[label] => July 20 2020
[y] => 0
)
[3] => Array
(
[label] => July 21 2020
[y] => 0
)
)
To
desired output array
Array
(
[0] => Array
(
[label] => July 18 2020
[y] => 1
)
[1] => Array
(
[label] => July 19 2020
[y] => 0
)
[2] => Array
(
[label] => July 20 2020
[y] => 3
)
[3] => Array
(
[label] => July 21 2020
[y] => 0
)
)
I have tried
array_merge($arrayone, $arraytwo);
but it doesn't work.
Is it possible to get the desired output ?
This is how array looks like - https://imgur.com/a/HUQUmZz
Please share your thoughts on this.
Thankyou
With your arrays, I do not think there is a smart way to do it, but this should work :
<?php
$datesArray = [
[
'label' => 'July 18 2020',
'y' => 0,
],
[
'label' => 'July 19 2020',
'y' => 0,
],
[
'label' => 'July 20 2020',
'y' => 0,
],
[
'label' => 'July 21 2020',
'y' => 0,
],
];
$valuesArray = [
[
'label' => 'July 20 2020',
'y' => 3,
],
[
'label' => 'July 18 2020',
'y' => 1,
],
];
foreach($datesArray as &$dateArray) {
foreach($valuesArray as $valueArray) {
// use ?? if php version allows it, else do isset($valueArray['label']) ? $valueArray['label'] : null;
$currentLabel = $valueArray['label'] ?? null;
if ($currentLabel === $dateArray['label']) {
$dateArray['y'] = $valueArray['y'] ?? 0;
}
}
}
print_r($datesArray);
return $datesArray;
Result :
Array
(
[0] => Array
(
[label] => July 18 2020
[y] => 1
)
[1] => Array
(
[label] => July 19 2020
[y] => 0
)
[2] => Array
(
[label] => July 20 2020
[y] => 3
)
[3] => Array
(
[label] => July 21 2020
[y] => 0
)
)
This will take quadratic time, if you need something faster you will have to change the indices of your arrays to make a faster lookup.
snippet
Related
I suspect this question has been answered before but i have dug and dug this great forum for an answer in vain.....
I have 3 arrays that looks like this:
Array
(
[1] => 19
[2] => 2
[3] => 2018
)
Array
(
[1] => 19
[2] => 1
[3] => 2017
)
Array
(
[1] => 18
[2] => 2
[3] => 2016
)
I would like to convert this 3 arrays into a multidimensional array to look something like this:
$mynewArray = Array(
[0] =>array(
[1] => 19
[2] => 2
[3] => 2018
)
[1] =>array(
[1] => 19
[2] => 1
[3] => 2017
)
[2] => array(
[1] => 18
[2] => 2
[3] => 2016
)
)
How do i achieve this in Php?
Demo Link.
You just need to add it in parent array as below,
$arr1 = [1 => 19, 2 => 2, 3 => 2018];
$arr2 = [1 => 19, 2 => 1, 3 => 2017];
$arr3 = [1 => 18, 2 => 2, 3 => 2016];
$mynewArray = [$arr1,$arr2,$arr3];
print_r($mynewArray);
Output
Array
(
[0] => Array
(
[1] => 19
[2] => 2
[3] => 2018
)
[1] => Array
(
[1] => 19
[2] => 1
[3] => 2017
)
[2] => Array
(
[1] => 18
[2] => 2
[3] => 2016
)
)
Also you can append your child arrays to the parent by
$array1 = array("1"=>"1","2"=>"2","3"=>"3");
$array2 = array("1"=>"1","2"=>"2","3"=>"3");
$newarray = array($array1,$array2);
I'm trying to display the total amount of installs each campaign number has had since the beginning using this array.
Array
(
[27] => Array
(
[0] => Array
(
[date] => 2018-04-09
[hour] => 7
[campaign] => 27
[installs] => 2
[trials] => 0
)
[1] => Array
(
[date] => 2018-04-09
[hour] => 3
[campaign] => 39
[installs] => 1
[trials] => 0
)
[2] => Array
(
[date] => 2018-04-09
[hour] => 5
[campaign] => 39
[installs] => 1
[trials] => 0
)
[3] => Array
(
[date] => 2018-04-09
[hour] => 6
[campaign] => 39
[installs] => 1
[trials] => 0
)
[4] => Array
(
[date] => 2018-04-09
[hour] => 8
[campaign] => 39
[installs] => 2
[trials] => 0
)
)
[53] => Array
(
[0] => Array
(
[date] => 2018-04-10
[hour] => 3
[campaign] => 53
[installs] => 1
[trials] => 0
)
[1] => Array
(
[date] => 2018-04-10
[hour] => 9
[campaign] => 53
[installs] => 1
[trials] => 0
)
[2] => Array
(
[date] => 2018-04-10
[hour] => 5
[campaign] => 55
[installs] => 1
[trials] => 0
)
[3] => Array
(
[date] => 2018-04-10
[hour] => 12
[campaign] => 55
[installs] => 1
[trials] => 0
)
)
[61] => Array
(
[0] => Array
(
[date] => 2018-04-15
[hour] => 0
[campaign] => 61
[installs] => 27
[trials] => 1
)
[1] => Array
(
[date] => 2018-04-15
[hour] => 1
[campaign] => 61
[installs] => 18
[trials] => 0
)
[2] => Array
(
[date] => 2018-04-15
[hour] => 1
[campaign] => 27
[installs] => 10
[trials] => 0
)
My array is shown above. You can see some of the campaign numbers are repeated on different dates. My result should look like:
Campaign 27: 12
Campaign 39: 5
Campaign 53: 2
Campaign 55: 2
Campaign 61: 45
So far I've got this, which doesn't seem to be printing all of the data.
$newarray = array();
$last = count($res) - 1;
foreach ($res as $i => $new){
foreach ($new as $x => $row){
$isFirst = ($i == 0);
$isLast = ($i == $last);
$grabbedvalue = $row['campaign'].",".$row['installs'];
array_push($newarray,$grabbedvalue);
}
}
print_r($newarray);
Hope that someone can help me out with this. Thanks!
Just loop through the array and count:
$data = [
[
'campaign' => 1,
'installs' => 20,
],
[
'campaign' => 1,
'installs' => 5,
],
[
'campaign' => 2,
'installs' => 1,
],
[
'campaign' => 2,
'installs' => 10,
],
[
'campaign' => 3,
'installs' => 9,
],
];
$count=array();
// Loop through the campaigns
foreach($data as $camp){
// Check if we initiated the counting before
if(isset($count[$camp['campaign']])){
// Add installs count to previous value
$count[$camp['campaign']] += $camp['installs'];
} else {
// Initiate the counting
$count[$camp['campaign']] = $camp['installs'];
}
}
var_dump($count);
This will give:
array (size=3)
1 => int 25
2 => int 11
3 => int 9
You should create a for loop inside of a for loop to solve this.
I can try explaining the steps as clearly as possible:
Create an new array ($totalInstalls)
Loop trough the array you specified
Loop trough each element in the array you specified (for loop inside a for loop)
Check if the campaign index of the current element already exists in $totalInstalls
If not set it to the installs
If it does exists increment it by one
I am looking for last three month state wise data.I have all state code array and last three month array with the following values,
$stateArray = array("Nj","va","Ca","BS","TS");
$MonthArray =array("Nov 2016","Dec 2016","Jan 2017");
Below is my result array fetch from the database,
Array (
[0] => Array (
[month] => Nov 2016
[count] => 150
[state] => NJ
)
[1] => Array (
[month] => Nov 2016
[count] => 100
[state] => va
)
)
I want result like below,
Array(
[Nj] => Array(
[0] => Array(
[month] => Nov 2016
[count] => 150
)
[1] => Array(
[month] => Dec 2016
[count] => 0
)
[2] => Array(
[month] => jan 2017
[count] => 0
)
)
[Ca] => Array(
[0] => Array(
[month] => Nov 2016
[count] => 0
)
[1] => Array(
[month] => Dec 2016
[count] => 0
)
[2] => Array(
[month] => jan 2017
[count] => 0
)
)
[va] => Array(
[0] => Array(
[month] => Nov 2016
[count] => 100
)
[1] => Array(
[month] => Dec 2016
[count] => 0
)
[2] => Array(
[month] => jan 2017
[count] => 0
)
)
)
and so on for all states.
I am trying to array array_search() and in_array() functions for each but it is not working.What I would like to accomplish to loop each state wise array.Below I am trying for looping,
foreach ( $result_array as $val ) {
$month = array_search ( $val ['month'], $monthArray );
$state = array_search ( $val ['state'], $stateArray );
if ($val ['count'] == '' || $val ['count'] == 'NULL') {
$countValue = 0;
} else {
$countValue = $val ['count'];
}
$final_array [] = $countValue;
}
Take a look at this simple example:
<?php
$input = [
[
'month' => "Nov 2016",
'count' => "150",
'state' => "NJ"
],
[
'month' => "Nov 2016",
'count' => "100",
'state' => "va"
],
[
'month' => "Dec 2016",
'count' => "270",
'state' => "NJ"
],
];
$output = [];
foreach (["Nj", "va", "Ca", "BS", "TS"] as $state) {
$output[strtoupper($state)] = [];
};
array_walk($input, function($entry) use (&$output) {
$output[strtoupper($entry['state'])][] = [
'month' => $entry['month'],
'count' => $entry['count']
];
});
print_r($output);
The result of the above code is:
Array
(
[NJ] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 150
)
[1] => Array
(
[month] => Dec 2016
[count] => 270
)
)
[VA] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 100
)
)
[CA] => Array
(
)
[BS] => Array
(
)
[TS] => Array
(
)
)
This appears to be the exact output structure you ask for.
$stateArray = array("Nj","va","Ca","BS","TS");
$MonthArray =array("Nov 2016","Dec 2016","Jan 2017");
$fromDB = Array (
Array (
'month' => 'Nov 2016',
'count' => 150,
'state' => 'NJ'
),
Array (
'month' => 'Nov 2016',
'count' => 100,
'state' => 'va'
)
);
$info = array();
foreach ($fromDB as $row){
$info[strtoupper($row['state'])][$row['month']] = $row['count'];
}
$result = array();
foreach ($stateArray as $state){
foreach ($MonthArray as $month){
$result[$state][]=array('month'=>$month, 'count'=>isset($info[strtoupper($state)][$month])?$info[strtoupper($state)][$month]:0);
}
}
print_r($result);
will output
Array
(
[Nj] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 150
)
[1] => Array
(
[month] => Dec 2016
[count] => 0
)
[2] => Array
(
[month] => Jan 2017
[count] => 0
)
)
[va] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 100
)
[1] => Array
(
[month] => Dec 2016
[count] => 0
)
[2] => Array
(
[month] => Jan 2017
[count] => 0
)
)
[Ca] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 0
)
[1] => Array
(
[month] => Dec 2016
[count] => 0
)
[2] => Array
(
[month] => Jan 2017
[count] => 0
)
)
[BS] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 0
)
[1] => Array
(
[month] => Dec 2016
[count] => 0
)
[2] => Array
(
[month] => Jan 2017
[count] => 0
)
)
[TS] => Array
(
[0] => Array
(
[month] => Nov 2016
[count] => 0
)
[1] => Array
(
[month] => Dec 2016
[count] => 0
)
[2] => Array
(
[month] => Jan 2017
[count] => 0
)
)
)
I have in my example 2 arrays of court bookings for a particular date and time on 5 courts, with date, court number, member and type.
I want to remove items from the second array where there is a matching date/court in the first array.
Here is one timeslot. The first array shows the 2 bookings in this timeslot.
Array
(
[0] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 1
[2] => Annie
[3] => Regular
)
[1] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 3
[2] => Meredith
[3] => Regular
)
)
The second array is all the timeslots available on that date/time.
Array
(
[2] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 1
[2] =>
[3] => UNBOOKED
)
[3] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 2
[2] =>
[3] => UNBOOKED
)
[4] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 3
[2] =>
[3] => UNBOOKED
)
[5] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 4
[2] =>
[3] => UNBOOKED
)
[6] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 5
[2] =>
[3] => UNBOOKED
)
)
What I want to do is remove items in the second array which have a match on elements [0] and [1]. Every solution I've tried just searches arrays for strings, not another array.
The result would ideally look like this (the outer numeric keys are not important to me).
Array
(
[0] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 1
[2] => Annie
[3] => Regular
)
[1] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 3
[2] => Meredith
[3] => Regular
)
[3] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 2
[2] =>
[3] => UNBOOKED
)
[5] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 4
[2] =>
[3] => UNBOOKED
)
[6] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 5
[2] =>
[3] => UNBOOKED
)
)
This is how you would use array filter to remove entries in the list of available times that exist in the list of booked times.
You can see this in action here: http://codepad.viper-7.com/VVbnys
$regular = Array(
0 => Array (
0 => 'Jul 6, 2014 7:00 AM',
1 => 1,
2 => 'Annie',
3 => 'Regular',
),
1 => Array (
0 => 'Jul 6, 2014 7:00 AM',
1 => 3,
2 => 'Meredith',
3 => 'Regular',
)
);
$unbooked = Array (
2 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 1,
2 => '',
3 => 'UNBOOKED',
),
3 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 2,
2 => '',
3 => 'UNBOOKED',
),
4 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 3,
2 => '',
3 => 'UNBOOKED',
),
5 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 4,
2 => '',
3 => 'UNBOOKED',
),
6 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 5,
2 => '',
3 => 'UNBOOKED',
),
);
//filter the unbooked array
$unbooked = array_filter($unbooked, function($availSlot) use ($regular){
//loop over each taken slot
foreach($regular as $takenSlot){
//if keys 0 and 1 are the same in both taken and available...
if($availSlot[0] == $takenSlot[0] && $availSlot[1] == $takenSlot[1]){
//return false to remove this element from the array
return false;
}
}
//if we get here, no matches were found.
//return true so the element stays in the array
return true;
});
Ideally though, the format would be changed to a multidimensional array that allows you to nest stuff under the timeslots and numbers that are available without having to loop over every entry looking for one that matches.
I have two arrays like this :
Array 1 is having x-axis values in array y-axis values in array
x-array: ["04 Feb","05 Feb","06 Feb","07 Feb","08 Feb","09 Feb","10 Feb"]
y-array: [3.27,3.34,3.27,3.2,3.28,3.17,3.15]
Array2 is having x-axis values in array y-axis values in array
x-array2: ["11 Feb", "12 Feb"]
y-array2: [3.19, 3.36]
How to create multidimensional array with key value pair.
Like below in php
Array
(
[0] => Array
(
[x-axis] => Array
(
[0] => 04 Feb
[1] => 05 Feb
[2] => 06 Feb
[3] => 07 Feb
[4] => 08 Feb
[5] => 09 Feb
[6] => 10 Feb
)
[y-axis] => Array
(
[0] => 3.27
[1] => 3.34
[2] => 3.27
[3] => 3.2
[4] => 3.28
[5] => 3.17
[6] => 3.15
)
)
[1] => Array
(
[x-axis] => Array
(
[0] => 11 Feb
[1] => 12 Feb
)
[y-axis] => Array
(
[0] => 3.19
[1] => 3.36
)
)
)
$new_array = array(array("x-axis"=>$x-axis-array,
"y-axis"=>$y-axis-array
),
array("x-axis"=>$x-axis-array2,
"y-axis"=>$y-axis-array2
)
);
echo "<pre>";
print_r($new_array);
$array = array(
0 => array(
"x-axis" => $x_array,
"y-axis" => $y_array),
1 => array(
"x-axis" => $x_array2,
"y-axis" => $y_array2)
) ;
var_dump($array) ;