Matching array value (PHP Array) - php

Input post :
$_POST['dateSlot']
$_POST['timeStart']
$_POST['timeEnd']
$_POST['quota']
These input post will resulting the below array.
Array
(
[dateSlot] => Array
(
[0] => 2018-04-05
[1] => 2018-04-05
[2] => 2018-04-05
)
[timeStart] => Array
(
[0] => 11:06 AM
[1] => 10:06 AM
[2] => 9:06 AM
)
[timeEnd] => Array
(
[0] => 11:06 AM
[1] => 9:06 AM
[2] => 7:06 AM
)
[quota] => Array
(
[0] => 12
[1] => 10
[2] => 10
)
)
I'm trying to foreach them to match the index key and form another array with this idea. Not so sure if can get the value I want :
foreach ($_POST['dateSlot'] as $k => $val) {
foreach ($_POST['timeStart'] as $k2 => $val2) {
foreach ($_POST['timeEnd'] as $k3 => $val3) {
foreach ($_POST['quota'] as $k4 => $val4) {
if($k == $k2 && $k == $k3 && $k == $k4){
$timeslots[$k]['date_slot'] = $val;
$timeslots[$k]['time_start'] = $val2;
$timeslots[$k]['time_end'] = $val3;
$timeslots[$k]['event_quota'] = $val4;
}
}
}
}
}
By that foreach, I'm getting the error Illegal string offset for date_slot, time_start, time_end, and event_quota
Based on the rows in the array, my goal is to re-form the array so that they all will be combined together to form 3 rows.
Example :
Array
(
[0] => Array
(
[date_slot] => 2018-04-05
[time_start] => 11:06 AM
[time_end] => 11:06 AM
[event_quota] => 12
)
[1] => Array
(
[date_slot] => 2018-04-05
[time_start] => 10:06 AM
[time_end] => 9:06 AM
[event_quota] => 10
)
[2] => Array
(
[date_slot] => 2018-04-05
[time_start] => 9:06 AM
[time_end] => 7:06 AM
[event_quota] => 10
)
)

Another approach to grouping this kind of data without needing to know the key names in advance.
This works by using the first row's data current( $data ) as the main iterator, then builds an array by combining the outer keys array_keys( $data ) and the inner column value array_column( $data, $column ) with array_combine() which combines two arrays of keys and an array of value to make each row's final array structure keyed by column name.
This is absolutely reliant on each multidimensional array having the same count of elements. As such this is not suitable for forms with checkbox inputs in them. At which point I would suggest using name="row[0][ColumnName]" as your name attribute and negating the need for this array processing.
http://php.net/manual/en/function.array-column.php
http://php.net/manual/en/function.array-combine.php
http://php.net/manual/en/function.array-keys.php
$data = array(
'Column-1'=>array('Row-1a','Row-2a','Row-3a'),
'Column-2'=>array('Row-1b','Row-2b','Row-3b'),
'Column-3'=>array('Row-1c','Row-2c','Row-3c')
);
$array = array();
foreach( array_keys( current( $data ) ) as $column )
{
$array[] = array_combine( array_keys( $data ), array_column( $data, $column ) );
}
print_r( $array );
Produces
Array
(
[0] => Array
(
[Column-1] => Row-1a
[Column-2] => Row-1b
[Column-3] => Row-1c
)
[1] => Array
(
[Column-1] => Row-2a
[Column-2] => Row-2b
[Column-3] => Row-2c
)
[2] => Array
(
[Column-1] => Row-3a
[Column-2] => Row-3b
[Column-3] => Row-3c
)
)

If you know that the element keys in all 4 of those post variables will always correlate to one timeslot element, then I think this will work for you:
foreach ($_POST['dateSlot'] as $key => $value) {
$timeslots[$key] = [
'date_slot' => $_POST['dateSlot'][$key],
'time_start' => $_POST['timeStart'][$key],
'time_end' => $_POST['timeEnd'][$key],
'event_quota' => $_POST['quota'][$key],
];
}
print_r($timeslots);

$dateSlot = $_POST['dateSlot']
$timeStart = $_POST['timeStart']
$timeEnd = $_POST['timeEnd']
$quota = $_POST['quota']
$all = array();
foreach($dateSlot as $key => $date) {
$all[] = array(
"data_slot" => $dateSlot[$key],
"time_start" => $timeStart[$key],
"time_end" => $timeEnd[$key],
"quota" => $quota[$key]
)
}

Input
$array = array(
'dateSlot' => array('2018-04-05','2018-04-05','2018-04-05'),
'timeStart' => array('11:06 AM','10:06 AM','9:06 AM'),
'timeEnd' => array('11:06 AM','9:06 AM','7:06 AM'),
'quota' => array(12,10,10)
);
Solution
$new = array();
for($i=0;$i<count($array['dateSlot']);$i++){
$new[] = array(
'dateSlot' => $array['dateSlot'][$i],
'timeStart' => $array['timeStart'][$i],
'timeEnd' => $array['timeEnd'][$i],
'event_quota' => $array['quota'][$i],
);
}
echo "<pre>";print_r($new);
Output
Array
(
[0] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 11:06 AM
[timeEnd] => 11:06 AM
[event_quota] => 12
)
[1] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 10:06 AM
[timeEnd] => 9:06 AM
[event_quota] => 10
)
[2] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 9:06 AM
[timeEnd] => 7:06 AM
[event_quota] => 10
)
)

Related

Group subsets of data in 3-level array by identifying column

I've this type of array in PHP:
Array(
[100] => Array(
[1] => Array (
[AVA_Date] => 2019-04-18
[ROO_Id] => 100
[RAT_Id] => 9
)
[2] => Array (
[AVA_Date] => 2019-04-20
[ROO_Id] => 100
[RAT_Id] => 10
)
[4] => Array (
[AVA_Date] => 2019-04-21
[ROO_Id] => 100
[RAT_Id] => 10
)
[7] => Array (
[AVA_Date] => 2019-04-22
[ROO_Id] => 100
[RAT_Id] => 9
)
)
)
I would like to merge items on ROO_Id and RAT_Id.
Then, for the AVA_Date, I need to list them under a new array in the current array.
So, the desired output is:
Array(
[100] => Array(
[0] => Array (
[AVA_Date] => Array (
[0] => 2019-04-18
[1] => 2019-04-22
)
[ROO_Id] => 100
[RAT_Id] => 9
)
[1] => Array (
[AVA_Date] => Array (
[0] => 2019-04-20
[1] => 2019-04-21
)
[ROO_Id] => 100
[RAT_Id] => 10
)
)
)
Here what I have tried:
$newArrOtherRooms = array_reduce($newArr, function($acc, $val) {
$room = array_search($val['ROO_Id'], array_column($acc, 'ROO_Id'));
$rate = array_search($val['RAT_Id'], array_column($acc, 'RAT_Id'));
if($rate == $room && $room > -1) {
array_push($acc[$room]['AVA_Date'], $val['AVA_Date']);
}
else {
$new_arr = $val;
$new_arr['AVA_Date'] = [$val['AVA_Date']];
array_push($acc, $new_arr);
}
return $acc;
},[]);
But it doesn't work like I want.
There are a couple of issues with your code. Firstly, you need to wrap the array_reduce with a foreach over the outer level of $newArr. Secondly, your call to array_search doesn't consider the fact that a ROO_Id or RAT_Id value might exist more than once in the array, as it only returns the first key at which it finds the value. To work around this, you can use array_keys to get an array of key values for each ROO_Id and RAT_Id value, and then take the intersection of those two arrays using array_intersect to see if both are present in the same element. If so, you update that element, otherwise you create a new one:
foreach ($newArr as $key => $array) {
$newArrOtherRooms[$key] = array_reduce($array, function($acc, $val) {
$room = array_keys(array_column($acc, 'ROO_Id'), $val['ROO_Id']);
$rate = array_keys(array_column($acc, 'RAT_Id'), $val['RAT_Id']);
$common = array_intersect($room, $rate);
if(!empty($common)) {
array_push($acc[current($common)]['AVA_Date'], $val['AVA_Date']);
}
else {
$new_arr = $val;
$new_arr['AVA_Date'] = [$val['AVA_Date']];
array_push($acc, $new_arr);
}
return $acc;
},[]);
}
print_r($newArrOtherRooms);
Output:
Array(
[100] => Array(
[0] => Array (
[AVA_Date] => Array (
[0] => 2019-04-18
[1] => 2019-04-22
)
[ROO_Id] => 100
[RAT_Id] => 9
)
[1] => Array (
[AVA_Date] => Array (
[0] => 2019-04-20
[1] => 2019-04-21
)
[ROO_Id] => 100
[RAT_Id] => 10
)
)
)
Demo on 3v4l.org
There is absolutely no reason to be making all of those iterated function calls.
Use a nested loop to iterate the subset of data for each room, group on the room "rate id", and push all "available date" values into a subarray in the respective group. When the subset of data is fully iterated, push its grouped data into the result array.
Code: (Demo)
$result = [];
foreach ($newArr as $rooId => $rows) {
$groups = [];
foreach ($rows as $row) {
if (!isset($groups[$row['RAT_Id']])) {
$row['AVA_Date'] = (array) $row['AVA_Date'];
$groups[$row['RAT_Id']] = $row;
} else {
$groups[$row['RAT_Id']]['AVA_Date'][] = $row['AVA_Date'];
}
}
$result[$rooId] = array_values($groups);
}
var_export($result);
Output:
array (
100 =>
array (
0 =>
array (
'AVA_Date' =>
array (
0 => '2019-04-18',
1 => '2019-04-22',
),
'ROO_Id' => 100,
'RAT_Id' => 9,
),
1 =>
array (
'AVA_Date' =>
array (
0 => '2019-04-20',
1 => '2019-04-21',
),
'ROO_Id' => 100,
'RAT_Id' => 10,
),
),
)

Combining duplicate keys in a multidimensional array in PHP [duplicate]

This question already has answers here:
Group subarrays by one column, make comma-separated values from other column within groups
(2 answers)
Closed last month.
here's how it looks in the PHP code:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
print_r($array);
?>
Example of print_r() function output:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml
)
[1] => Array
(
[name] => filter_amount
[value] => 200-ml
)
[2] => Array
(
[name] => page_size
[value] => 7
)
)
I need to combine duplicates of filter_amount values from the array.
The values of these duplicates must be commas separated and the result should be the following code:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml,200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
[2] => Array
(
[name] => orderby
[value] => rating
)
[3] => Array
(
[name] => paged
[value] => 1
)
)
Since you want value to be concatenated by a comma, you'll have to make a cycle of it
<?php
//Allow me to change this variable name, just to not create confusion
$content = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
//$content is your initial array
//$outputArray is the final worked-up array
$outputArray = [];
//Let's make a cycle going for every array inside $content
foreach ($content as $innerArray) {
//Does this $innerArray['name'] (filter_ammount) exist in $outputArray in an array
//consisting in key => value where the key is 'name' and equals
//what we look for that is(filter_ammount)?
$key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//If not, let's place this array in the $output array
if ($key === false) {
array_push($outputArray, $innerArray);
} else {
//If exists, then $key is the $key of the $outputArray and let's add to its value
//our current value, that is in our $innerArray, concatenated with a comma
$outputArray[$key]['value'] .= ",". $innerArray['value'];
}
}
//Boom, magic
print_r($outputArray);
//Note: This is going to affect every duplicate it finds, as in:
//If you got 3 arrays with name 'filter_ammount' and 2 arrays with name
//'page_size', it's going to concatenate the filter_ammount and the 'page_size'.
//If you specifically just want filter_ammount,
//replace this -> $key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//with this -> $key = array_search('filter_ammount', array_column($outputArray , 'name'));
?>
References
http://php.net/manual/en/function.array-search.php
http://php.net/manual/en/function.array-column.php
How to combine two items by 2 duplicate columns?
[root#localhost TEST]# php R00.php
Array
(
[0] => Array
(
[0] => S01
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445
)
[1] => Array
(
[0] => S02
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[2] => Array
(
[0] => S03
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 23
)
[4] => Array
(
[0] => S05
[1] => 100.100.100.100
[2] => 192.168.100.100
[3] => 22
)
[5] => Array
(
[0] => S06
[1] => 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[6] => Array
(
[0] => S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[7] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[8] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[9] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
[root#localhost TEST]#
combine 1 or 2 items by 2 column duplicate below is result I need
Array
(
[0] => Array
(
[0] => S01 , S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445 , 23
)
[1] => Array
(
[0] => S02 , S03 , S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S05 , S06
[1] => 100.100.100.100 , 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[4] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[5] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[6] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
Try this:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$tmp = array();
foreach($array as $val) {
$tmp[$val['name']]['values'][] = $val['value'];
}
foreach($tmp as $k => $v) {
$item = implode(',', array_unique(explode(',', implode(',',$v['values']))));
$newArr[] = array('name' => $k, 'value' => $item);
}
echo '<pre>';
print_r($newArr);
echo '</pre>';
got it with the following crazy mess:
$name = array_column($array, 'name');
$value = array_column($array, 'value');
foreach($name as $nk=>$nv)
foreach($value as $vk=>$vv)
if($nk == $vk)
$a[$nv][] = $vv;
foreach($a as $k=>$v)
$b[$k] = implode(',', $v);
$z = 0;
foreach($b as $k=>$v)
{
$c[$z]['name'] = $k;
$c[$z]['value'] = $v;
$z++;
}
$c is the resulting array
Or using a medley of array functions:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$names = array_column($array, 'name');
$values = array_column($array, 'value');
$result = [];
foreach (array_unique($names) as $k)
$result[$k] = implode(", ", array_filter($values,
function($v, $indx) use ($names, $k) {
return $names[$indx] == $k;
}, ARRAY_FILTER_USE_BOTH));
print_r($result);
$result2 = [];
foreach ($result as $k=>$v) $result2[] = ['name'=>$k, 'value'=>$v];
print_r($result2);
Results in:
Array
(
[filter_amount] => 100-ml, 200-ml
[page_size] => 7
)
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml, 200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
)
All of the other answers up to now are using two or more iterating techniques for this task. There only needs to be one loop.
Build an associative output array based on the name values as you iterate. If the associative key isn't set, then save the whole row. If it is set, then just append a comma then the new value data to the stored value element.
Using temporary keys allows isset() to swiftly check for existence. It will always outperform array_search() and in_array() because of how php treats arrays (as hash maps).
Remove the temporary keys when the loop is finished by calling array_values().
Code: (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['name']])) {
$result[$row['name']] = $row;
} else {
$result[$row['name']]['value'] .= ',' . $row['value'];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'name' => 'filter_amount',
'value' => '100-ml,200-ml',
),
1 =>
array (
'name' => 'page_size',
'value' => '7',
),
)

Array inside array or nested array

I have an array like below: all the values I am getting one array only, but I don't want this way. This is the best way to do so, in php or jQuery both languages are ok for me
Array
(
[0] => Array
(
[val] => facebook
)
[1] => Array
(
[val] => snapchat
)
[2] => Array
(
[val] => instagram
)
[3] => Array
(
[expenses] => 986532
)
[4] => Array
(
[expenses] => 45456
)
[5] => Array
(
[expenses] => 56230
)
[6] => Array
(
[social_id] => 15
)
[7] => Array
(
[social_id] => 16
)
[8] => Array
(
[social_id] => 17
)
)
and I want to output like this..
$result = array(
array(
"val" => "Facebook",
"expenses" => "84512",
"social_id" => 1
),
array(
"val" => "Instagram",
"expenses" => "123",
"social_id" => 2
)
);
but this should be dynamic, the length of the above array can be varies
You can merge the arrays to one and use array_column to get all vals or expenses in a separate array.
Then foreach one array and build the new array with the key.
//$a = your array
// Grab each column separate
$val = array_column($a, "val");
$expenses= array_column($a, "expenses");
$social_id = array_column($a, "social_id");
Foreach($val as $key => $v){
$arr[] = [$v, $expenses[$key], $social_id[$key]];
}
Var_dump($arr);
https://3v4l.org/nVtDf
Edit updated with the 'latest' version of the array. My code works just fine with this array to without any changes.
to get that array style you can do it by hand for each array
function test(array ...$arr)
{
$result = array();
foreach ($arr as $key => $pair) {
foreach ($pair as $item => $value) {
$result[$item] = $value;
}
}
return $result;
}
$arr1 = test( ['facebook' => 1], ['expenses' => 100]);
print_r($arr1);
/* Array (
[facebook] => 1,
[expenses] => 100
)
*/
$arr2 = test( $arr1, ['more info' => 'info'] );
print_r($arr2);
/* Array (
[facebook] => 1,
[expenses] => 100,
[more info] => info
)
*/
you can pass it any number of
['Key' => 'Pair']
array('Key' => 'Pair')
or even a previous made array and it will add them up into 1 big array and return it

Read from JSON file with PHP and Multiple foreach statements

I tried to read from a JSON file using PHP. But I am stuck now.
This is my JSON file :
Array
(
[date] => 25-1-2017
[leagues] => Array
(
[0] => Array
(
[league_id] => 0
[league_name] => كأس امم افريقيا
[league_logo] => http://3.bp.blogspot.com/-4iampWUCLto/VmldL2XTz7I/AAAAAAAAD0I/eZFfzSxRbnE/s60/africa.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 1
[team1logo] => http://2.bp.blogspot.com/-mycTzHXuzJA/Ugbb-UG_3JI/AAAAAAAAEv0/_mzaSHnRedE/s60/egypt+(4).png
[team2logo] => http://1.bp.blogspot.com/-kYrkU4jahZY/UgbeVpHf8RI/AAAAAAAAFIQ/TBgM5fvVW14/s60/ghana+(3).png
[match_time] => 19:00
[channels_id] => Array
(
[0] => 18
)
[team1] => مصر
[team2] => غانا
)
[1] => Array
(
[match_id] => 2
[team1logo] => http://3.bp.blogspot.com/-_y1MazEn-vg/UgbeoIWUpII/AAAAAAAAFPI/JTRGGpG3GiQ/s60/mali+(4).png
[team2logo] => http://2.bp.blogspot.com/-GTxKONxvKy4/Ugt6AKXOmyI/AAAAAAAAF8Y/pPcM0O58cQI/s60/uganda_2.png
[match_time] => 19:00
[channels_id] => Array
(
[0] => 19
)
[team1] => مالي
[team2] => أوغندا
)
)
)
[1] => Array
(
[league_id] => 1
[league_name] => كأس ملك أسبانيا
[league_logo] => http://1.bp.blogspot.com/-zAzWT2Vpbe0/Vmle9TUWDlI/AAAAAAAAD04/-BKgPzRCT1k/s60/Copa_del_Rey_logo_since_2012.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 3
[team1logo] => http://4.bp.blogspot.com/-6sfdkbboNdk/VBxVKisRkcI/AAAAAAAADG8/oBKDHmBW5xc/s60/eibar+fc.png
[team2logo] => http://2.bp.blogspot.com/-S7p2yMaLywM/UhCz6GqNCRI/AAAAAAAAGFE/CfxzSQk8bgQ/s60/Atletco+Madrid2.Png
[match_time] => 18:15
[channels_id] => Array
(
[0] => 1
)
[team1] => إيبار
[team2] => أتلتيكو مدريد
)
[1] => Array
(
[match_id] => 4
[team1logo] => http://1.bp.blogspot.com/--6y77FXuPLI/UjeVcpJ-AJI/AAAAAAAAGok/QyZhwahPamo/s60/Celta+Vigo.png
[team2logo] => http://2.bp.blogspot.com/-tncIAL_U6mI/UgbnQw75V8I/AAAAAAAAFhA/8-9Xpw83GKY/s60/Real+Madrid+(3).png
[match_time] => 20:15
[channels_id] => Array
(
[0] => 3
)
[team1] => سيلتا فيغو
[team2] => ريال مدريد
)
)
)
[2] => Array
(
[league_id] => 2
[league_name] => كأس الرابطة الإنجليزية
[league_logo] => http://4.bp.blogspot.com/-31G65FeskFs/VmlezHMPm_I/AAAAAAAAD0w/9OMjh8AQP-M/s60/TheFA_CapitalOneCup.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 5
[team1logo] => http://3.bp.blogspot.com/-D0lb4b-qN5U/UgbeDosqjYI/AAAAAAAAFBY/Qg7kvoodvFY/s60/Liverpool+(2).png
[team2logo] => http://2.bp.blogspot.com/-prH3jgmfewQ/Ug8ZgNxPlcI/AAAAAAAAGAU/HLmbFvGuDB8/s60/Southampton.png
[match_time] => 20:00
[channels_id] => Array
(
[0] => 2
)
[team1] => ليفربول
[team2] => ساوثهامبتون
)
)
)
[3] => Array
(
[league_id] => 3
[league_name] => كاس ايطاليا
[league_logo] => http://2.bp.blogspot.com/-p9Kjb2_GZPU/VnAdQcHV9_I/AAAAAAAAD7E/6R-P54Upui4/s60/tim-cup.png
[league_matches] => Array
(
[0] => Array
(
[match_id] => 6
[team1logo] => http://1.bp.blogspot.com/-POxAfSSnlW0/UhCt7oQsdLI/AAAAAAAAGDo/rMRXx2mqvUI/s60/Juventus[1].Png
[team2logo] => http://3.bp.blogspot.com/-_Qj_GaxVaDE/UgaEPtt7EsI/AAAAAAAAEA8/redsOTj7F4Q/s60/Ac+Milan+(3).png
[match_time] => 20:00
[channels_id] => Array
(
[0] => 103
)
[team1] => يوفنتوس
[team2] => ميلان
)
)
)
)
)
And this is my PHP so far:
$str = file_get_contents($url);
$json = json_decode($str, true);
$date = $json['date'];
$leagues = $json['leagues'][0];
foreach ($leagues as $key => $value) {
for ($i=0; $i<count($value); $i++) {
foreach ($value[$i] as $key1 => $value1) {
$sql = "INSERT INTO table_name (column1,column2,column3,column4) VALUES (...,...,...,...)";
for ($c=0; $c<count($value1); $c++) {
foreach ($value1[$c] as $key2 => $value2) {
echo $value2;
}
}
}
}
}
the problem how can read all each array by foreach and store every value to table in database.
I would appreciate an example for this.
Basic structure to loop all nodes in your json
//leagues level
foreach ($json['leagues'] as $key => $league) {
$league_id = $league['league_id'];
//leagues -> league_matches level
foreach ($league['league_matches'] as $key => $match) {
$match_id = $match['match_id'];
//channel ids
foreach ($match['channels_id'] as $channels_id) {
//$channels_id;
//create an sql
$sql = "INSERT INTO xy (col1,col2,col3) values ($league_id,$match_id,$channels_id)";
$db->query($sql);
}
}
}
About the SQL You dont show any structure of your databse, so i cant help creating the sql.
I hope your JSON file sends the value using json_encode() method. You need to separate the array elements with a comma (,). Also you need to write the array indexes within quotes ('). Here is a corrected sample of your JSON page.
$retArr = Array
(
['date'] => 25-1-2017,
['leagues'] => Array
(
[0] => Array
(
['league_id'] => 0,
['league_name'] => كأس امم افريقيا,
['league_logo'] => http://3.bp.blogspot.com/-4iampWUCLto/VmldL2XTz7I/AAAAAAAAD0I/eZFfzSxRbnE/s60/africa.png,
['league_matches'] => Array
(
[0] => Array
(
['match_id'] => 1,
['team1logo'] => http://2.bp.blogspot.com/-mycTzHXuzJA/Ugbb-UG_3JI/AAAAAAAAEv0/_mzaSHnRedE/s60/egypt+(4).png,
['team2logo'] => http://1.bp.blogspot.com/-kYrkU4jahZY/UgbeVpHf8RI/AAAAAAAAFIQ/TBgM5fvVW14/s60/ghana+(3).png,
['match_time'] => 19:00
['channels_id'] => Array
(
[0] => 18
)
['team1'] => مصر ,
['team2'] => غانا
)
)
)
)
);
echo json_encode($retArr);
On the receiving page, you need to set a second parameter to TRUE in the json_decode() method so that you will get the result as an array rather than as a JSON Object.
$str = file_get_contents($url);
$json = json_decode($str, true);
$date = $json['date'];
$leagues = $json['leagues'][0];
foreach ($leagues as $key => $value) {
for ($i=0; $i<count($value); $i++) {
foreach ($value[$i] as $key1 => $value1) {
$sql = "INSERT INTO table_name (column1,column2,column3,column4) VALUES (...,...,...,...)";
for ($c=0; $c<count($value1); $c++) {
foreach ($value1[$c] as $key2 => $value2) {
echo $value2;
}
}
}
}
}
Try this way.

how to group a multidimensional array using a condition in php?

I have the following array
Array
(
[0] => Array
(
[shop] => 3
[price] => 332.00
)
[1] => Array
(
[shop] => 1
[price] => 3335.00
)
[2] => Array
(
[shop] => 3
[price] => 235.00
)
[3] => Array
(
[shop] => 1
[price] => 402.50
)
[4] => Array
(
[shop] => 3
[price] => 332.00
)
)
I need to group using shop and get get the minimum price of each shop in the array.
The expected result is as follows
Array
(
[0] => Array
(
[shop] => 3
[price] => 235.00
)
[1] => Array
(
[shop] => 1
[price] => 402.50
)
)
How will I do it?
You need to use additional variable
<?php
$arr = Array
(
0 => Array
(
'shop' => 3,
'price' => 332.00
),
1 => Array
(
'shop' => 3,
'price' => 232.00
),
2 => Array
(
'shop' => 1,
'price' => 232.00
),
3 => Array
(
'shop' => 3,
'price' => 432.00
),
4 => Array
(
'shop' => 1,
'price' => 132.00
),
);
$filtered = array();
foreach($arr as $prices){
if(FALSE === isset($filtered[$prices['shop']]) || $filtered[$prices['shop']]['price'] > $prices['price']){
$filtered[$prices['shop']] = $prices;
}
}
$filtered = array_values($filtered);
print_r($filtered);
This is very fast example how you can achieve this
It's rather simple.
Create a new array where you will host your stores as keys, and prices as values. What you want to do is to go through each element and first if the key does not exists in your new array, add it and its value. If however the key already exists, check if the current value is lower, and save it if true.
$grouped = [];
foreach ($arr as $k => $v) {
foreach ($k as $key => $value) {
if (isset($grouped[$key])) {
if ($value < $grouped[$key]) {
$grouped[$key] = $value;
}
} else {
$grouped[$key] = $value;
}
}
}
Your new array will look like this (store => price):
[
1 => 402.50,
3 => 235.00
]

Categories