Sir,
I am confused what should be exact title of this issues. Below is my problem
I have a multidimensional array like below
Array
(
[0] => Array
(
[0] => 2017-11-01
[1] => 9 Am
)
[1] => Array
(
[0] => 2017-11-02
[1] => 07 Pm
)
[2] => Array
(
[0] => 2017-11-03
[1] => 11 Pm
)
[3] => Array
(
[0] => 2017-11-04
[1] => 03 Pm
)
[4] => Array
(
[0] => 2017-11-01
[1] => 11 Am
)
[5] => Array
(
[0] => 2017-11-02
[1] => 05 Pm
)
)
Now, I want to make unique date beside time should be into another array.
I want it as below...
Array
(
[0] => Array
(
[0] => 2017-11-01
[1] => Array
(
[0] => 9 Am
[1] => 11 Am
)
)
[1] => Array
(
[0] => 2017-11-02
[1] => Array
(
[0] => 07 Pm
[1] => 05 Pm
)
)
[2] => Array
(
[0] => 2017-11-03
[1] => Array
(
[0] => 11 Pm
)
)
[3] => Array
(
[0] => 2017-11-04
[1] => Array
(
[0] => 03 Pm
)
)
)
Thanks in advance..
Regards,
Anwar
Something of that fashion should do the trick...
Its nowhere near the fastest way to do so but it should get you going and it is pretty easy to read/understand
// putting the times in a new array where the date is the key
$byDate = [];
foreach ($firstArray as [$date, $time]) {
$byDate[$date][] = $time;
}
// going thru it again to have it the way you needed it
$newArray = [];
foreach ($byDate as $date => $times) {
$newArray[] = [$date, $times];
}
Related
I have an array-
$arraydelivered =
Array
(
[0] => Array
(
[0] => 1
[1] => pending
[2] => January
)
[1] => Array
(
[0] => 4
[1] => pending
[2] => April
)
[2] => Array
(
[0] => 7
[1] => pending
[2] => July
)
[3] => Array
(
[0] => 10
[1] => pending
[2] => October
)
)
I want to resort this array dynamically to
Array
(
[0] => Array
(
[0] => 4
[1] => pending
[2] => April
)
[1] => Array
(
[0] => 7
[1] => pending
[2] => July
)
[2] => Array
(
[0] => 10
[1] => pending
[2] => October
)
[3] => Array
(
[0] => 1
[1] => pending
[2] => January
)
)
I have tried to find out the subarray based on which I know exactly from where to re-sort
foreach ($arraydelivered as $keyD => $valueD) {
if($valueD[0] == $cycle){
print_r($valueD);
}
}
This has given me the output -
Array ( [0] => 4 [1] => pending [2] => April )
Now I want to use this sub-array as the identifier to resort to the main array. Basically this sub-array will be the resort starting point for the big array.
Here is the solve array
$keyD1=0;
if(!empty($arraydelivered)){
foreach ($arraydelivered as $keyD => $valueD) {
if($valueD[0] == $cycle){
$keyD1= $keyD;
}
}
$outputsec = array_slice($arraydelivered, $keyD1);
$outputfirst = array_slice($arraydelivered, -4, $keyD1);
$finalarray= array_merge($outputsec,$outputfirst);
$arraydelivered=$finalarray;
I have Two array both array have same key but I want same key in one array inside multiple value.
Input:
[coming_new_date] => Array
(
[0] => 2021-03-06
[1] => 2021-03-07
[2] => 2021-03-08
[3] => 2021-03-09
)
[coming_Day_name] => Array
(
[0] => Sat
[1] => Sun
[2] => Mon
[3] => Tue
)
Output:
array(
[0] => array(
[0] =>2021-03-06
[1] =>Sat
)
[1] => array(
[0] =>2021-03-07
[1] =>Sun
)
[2] => array(
[0] =>2021-03-08
[1] =>Mon
)
)
try below code:
$output[][]='';
for($i=0; $i<4; $i++)
{
$output[$i][0]=$coming_new_date[0];
$output[$i][1]=$coming_day_name[0];
}
I tried my best to solve it but I could not. It would be great if you could help me on this.
I have these two arrays..
$firstarray — it contains a date [0] and the product cost [1] on that day.
[0] => Array (
[0] => 2020-12-01
[1] => 24.00
)
[1] => Array (
[0] => 2020-12-05
[1] => 16.00
)
[2] => Array (
[0] => 2020-12-07
[1] => 12.00
)
[3] => Array (
[0] => 2020-12-15
[1] => 0.00
)
[4] => Array (
[0] => 2020-12-16
[1] => 0.00
)
[5] => Array (
[0] => 2020-12-16
[1] => 100.00
)
$secondarray — contains multiple date ranges [0] and [1], where the cost [2] is exceptionally different within the given period.
[0] => Array (
[0] => Array (
[0] => 2020-12-01
[1] => 2020-12-05
[2] => 42.00
)
[1] => Array (
[0] => 2020-12-06
[1] => 2020-12-08
[2] => 35.00
)
[2] => Array (
[0] => 2020-12-09
[1] => 2020-12-12
[2] => 76.00
)
)
[1] => Array (
[0] => Array (
[0] => 2020-12-01
[1] => 2020-12-05
[2] => 42.00
)
[1] => Array (
[0] => 2020-12-06
[1] => 2020-12-08
[2] => 35.00
)
[2] => Array (
[0] => 2020-12-09
[1] => 2020-12-12
[2] => 76.00
)
)
[2] => Array (
[0] => Array (
[0] => 2020-12-04
[1] => 2020-12-09
[2] => 10.00
)
[1] => Array (
[0] => 2020-12-10
[1] => 2020-12-13
[2] => 45.00
)
)
[3] => Array (
[0] => Array (
[0] => 2020-12-04
[1] => 2020-12-09
[2] => 10.00
)
[1] => Array (
[0] => 2020-12-10
[1] => 2020-12-13
[2] => 45.00
)
)
[4] => Array (
[0] => Array (
[0] => 2020-12-04
[1] => 2020-12-09
[2] => 10.00
)
[1] => Array (
[0] => 2020-12-10
[1] => 2020-12-13
[2] => 45.00
)
)
[5] => Array (
[0] => Array (
[0] => 2020-12-01
[1] => 2020-12-05
[2] => 42.00
)
[1] => Array (
[0] => 2020-12-06
[1] => 2020-12-08
[2] => 35.00
)
[2] => Array (
[0] => 2020-12-09
[1] => 2020-12-12
[2] => 76.00
)
)
I want to check if each date in $firstarray is in between one of the date ranges in the $secondarray, if so — echo the value at index [2] from the $secondarray from that date range, if not — echo the value at index [1] from the $firstarray.
Both arrays will always have the same number, which is 5 in this case (the foreach loop should happen always for that same index only) but the number of the arrays or the date ranges inside could be different.
thank you very much!!
You can use a foreach loop over the values in $firstarray, setting the default price as the value from that array. You can then use the key from each value to index into $secondarray and compare the date from $firstarray with each of the ranges in $secondarray. If the date is in the range, set the price to the value from that range.
$prices = array();
foreach ($firstarray as $key => $value) {
$price = $value[1];
foreach ($secondarray[$key] as $range) {
if ($value[0] >= $range[0] && $value[0] <= $range[1]) {
$price = $range[2];
break;
}
}
$prices[] = $price;
}
print_r($prices);
Output (for your sample data):
Array
(
[0] => 42
[1] => 42
[2] => 10
[3] => 0
[4] => 0
[5] => 100
)
Demo on 3v4l.org
This checks if date of first array is between any of the date-ranges of the second array. If you want to check other indexes just increment (or decrement) $index.
To return the correct price just uncomment the
return $firstarray[1]
and
return $secondarray[2]
Output
Is between
Is not there
Is not there
Code
<?php
/*
================ Firstarray
[0] => Array (
[0] => 2020-12-01
[1] => 24.00
)
================ Secondarray
[0] => Array (
[0] => Array (
[0] => 2020-12-01
[1] => 2020-12-05
[2] => 42.00
)
[1] => Array (
[0] => 2020-12-06
[1] => 2020-12-08
[2] => 35.00
)
[2] => Array (
[0] => 2020-12-09
[1] => 2020-12-12
[2] => 76.00
)
*/
$firstarray = array(array('2020-12-01', '24.00'));
$secondarray = array(array(array('2020-12-01', '2020-12-05', '42.00'), array('2020-12-06', '2020-12-08', '35.00'), array('2020-12-09', '2020-12-12', '76.00')));
$index = 0;
echo check($firstarray[$index], $secondarray[$index]);
function check($firstarray, $secondarray)
{
foreach ($secondarray as $key => $value) {
if (
// Check if date is between two dates
(date('Y-m-d', strtotime($firstarray[0])) >= date('Y-m-d', strtotime($value[0]))) &&
(date('Y-m-d', strtotime($firstarray[0])) <= date('Y-m-d', strtotime($value[1]))))
{
echo "Is between".PHP_EOL;
// return $secondarray[2];
} else {
echo "Is not there".PHP_EOL;
}
}
// return $firstarray[1];
}
I new to PHP, I need to convert single dimension array into multi dimension array in php
I have data like this, need to minimize as below.
Array
(
[0] => Array
(
[0] => David
[1] => School
[2] => 19
[3] => 29
)
[1] => Array
(
[0] => Paul
[1] => Home
[2] => 19
[3] => 29
)
[2] => Array
(
[0] => Paul
[1] => Cinema
[2] => 19
[3] => 29
)
[3] => Array
(
[0] => Paul
[1] => Park
[2] => 19
[3] => 29
)
[4] => Array
(
[0] => Rossie
[1] => Playground
[2] => 19
[3] => 29
)
[5] => Array
(
[0] => Rossie
[1] => Hotel
[2] => 19
[3] => 29
)
[6] => Array
(
[0] => Rossie
[1] => Hospital
[2] => 19
[3] => 29
)
)
And I want convert it to multidimensional
Array
(
[0] => Array
(
[0] => Array
(
[0] => David
(
[0] => School
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Paul
(
[0] => Home
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Cinema
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Park
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
[2] => Array
(
[0] => Array
(
[0] => Rossie
(
[0] => Playground
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Hotel
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Hospital
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
)
I hope you get an idea. But my function doesn't do this correctly or maybe there are other ways to do this easier ?
I would be grateful for any help.
Thanks
Below is one way to do it:
<?php
$arr = [array('David','School',19,29),
array('Paul','Home',19,29),
array('Paul','Cinema',19,29),
array('Paul','Park',19,29),
array('Rossie','Playground',19,29),
array('Rossie','Hotel',19,29),
array('Rossie','Hospital',19,29)];
// Get all names.
$names = array_unique(array_map(function($value){return $value[0];}, $arr));
$places = [];
// Create the multidimensional array, grouping by name.
foreach($names as $key => $name){
$tempArr = [];
foreach($arr as $record){
if($record[0] === $name){
$tempArr[][$record[1]] = array_slice($record,2);
}
}
$places[][][$name] = $tempArr;
}
print("<pre>".print_r($places,true)."</pre>");
This will return the following result:
Array
(
[0] => Array
(
[0] => Array
(
[David] => Array
(
[0] => Array
(
[School] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
[1] => Array
(
[0] => Array
(
[Paul] => Array
(
[0] => Array
(
[Home] => Array
(
[0] => 19
[1] => 29
)
)
[1] => Array
(
[Cinema] => Array
(
[0] => 19
[1] => 29
)
)
[2] => Array
(
[Park] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
[2] => Array
(
[0] => Array
(
[Rossie] => Array
(
[0] => Array
(
[Playground] => Array
(
[0] => 19
[1] => 29
)
)
[1] => Array
(
[Hotel] => Array
(
[0] => 19
[1] => 29
)
)
[2] => Array
(
[Hospital] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
)
I have an array as below:
Array
(
[time] => Array
(
[0] => 6:00 PM
[1] => 4:00 PM
)
[id] => Array
(
[0] => #1 Hits of the 60's
[1] => 3 Men and a Lady
)
[url] => Array
(
[0] => hits-of-the-60
[1] => 3-men-and-a-lady
)
[ev_evc_id] => Array
(
[0] => 2
[1] => 2
)
[address] => Array
(
[0] => Caravelle Theatre
[1] => God and Country Theatre
)
[phone] => Array
(
[0] => 1-800-4Branson (800-427-2676)
[1] => 1-800-4Branson (800-427-2676)
)
)
I want to sort the array based on time ascending.
Desired output is as below:
Array
(
[time] => Array
(
[0] => 4:00 PM
[1] => 6:00 PM
)
[id] => Array
(
[0] => 3 Men and a Lady
[1] => #1 Hits of the 60's
)
[url] => Array
(
[0] => 3-men-and-a-lady
[1] => hits-of-the-60
)
[ev_evc_id] => Array
(
[0] => 2
[1] => 2
)
[address] => Array
(
[0] => God and Country Theatre
[1] => Caravelle Theatre
)
[phone] => Array
(
[0] => 1-800-4Branson (800-427-2676)
[1] => 1-800-4Branson (800-427-2676)
)
)
I have tried asort, usort. But could not make it work. Any help / suggestion is appreciated.
You can do it with array_multisort, it will sort all arrays based on the first array given:
array_multisort(
$myArray["time"],
$myArray["id"],
$myArray["url"],
$myArray["ev_evc_id"],
$myArray["address"],
$myArray["phone"]
);
Your array is in the wrong order. Try if you can change it to this and usort will work
Array
(
[0] => Array
(
[time] => 6:00 PM
[id] => 1
...
)
[1] => Array
(
[time] => 4:00 PM
[id] => 2
...
)
)