$json_str1 = '[{"13:00":"1"},{"14:00":"1"},{"15:30":"1"},{"16:30":"1"},{"1:00":"1"}]';
$json_str2 = '[{"13:00":"1"},{"14:00":"1"},{"15:30":"1"},{"12:30":"1"}]';
These are two JSON strings and I want the result like difference between them like {"1:00":"1"} and one more {"12:30":"1"}
The solution of this has a many aspects, cause there's 3 different values:
{"16:30":"1"},{"1:00":"1"}
{"12:30":"1"}
Firstly, you can convert your JSON string into array with json_decode($str,true):
json_decode($json_str1, true);
json_decode($json_str2, true);
Outputs like:
Array
(
[0] => Array
(
[13:00] => 1
)
...
Then create combined array with values like JSON object elements with foreach loop:
$str1 = [];
foreach($data1 as $row){
$str1[] = json_encode($row);
}
Outputs like:
Array
(
[0] => {"13:00":"1"}
[1] => {"14:00":"1"}
[2] => {"15:30":"1"}
[3] => {"16:30":"1"}
[4] => {"1:00":"1"}
)
...
Then you can find the difference between this two arrays, but, you need to do it in both ways (compare $array1 with $array2 and $array2 with $array1):
$dif1 = array_diff($str1, $str2);
$dif2 = array_diff($str2, $str1);
Outputs:
Array
(
[3] => {"16:30":"1"}
[4] => {"1:00":"1"}
)
Array
(
[3] => {"12:30":"1"}
)
Finally you can merge the result with:
$fin = array_merge($dif1, $dif2);
Outputs:
Array
(
[0] => {"16:30":"1"}
[1] => {"1:00":"1"}
[2] => {"12:30":"1"}
)
All of this you can put in a separate function like:
function getDifference2JSONstrings($jsonStr1, $jsonStr2){
$data1 = json_decode($jsonStr1, true);
$data2 = json_decode($jsonStr2, true);
$str1 = [];
$str2 = [];
foreach($data1 as $row){
$str1[] = json_encode($row);
}
foreach($data2 as $row){
$str2[] = json_encode($row);
}
return array_merge(array_diff($str1, $str2), array_diff($str2, $str1));
}
$res = getDifference2JSONstrings($json_str1, $json_str2);
print_r($res);
Outputs an array:
Array
(
[0] => {"16:30":"1"}
[1] => {"1:00":"1"}
[2] => {"12:30":"1"}
)
Demo
Related
I am new to PHP an needed little help. It may be easy for some but giving me a tough time.
I have an array
Array ( [0] => page-18 [1] => page-20 )
Which I would like to explode further by '-':
$mainStringBrk = array('page-18', 'page-20');
$finalArray = array();
foreach($mainStringBrk as $bString){
$mainStringBrkBrk = explode('-', $bString);
$finalArray[$mainStringBrkBrk[0]] = $mainStringBrkBrk[1];
}
echo '<pre>'; print_r($finalArray);
When I do, it outputs only the last key and value of array.
Array ( page => 20 )
My desired output is:
Array ( page => 18, page => 20 )
I am wondering if anyone can guide me in right direction.
You can't achieve the result you want as it is not possible to have an array with identical keys; this is why you only have one result in your output. You could change your output structure to a 2-dimensional array to work around this e.g.
$mainStringBrk = array('page-18', 'page-20');
$finalArray = array();
foreach($mainStringBrk as $bString){
$mainStringBrkBrk = explode('-', $bString);
$finalArray[$mainStringBrkBrk[0]][] = $mainStringBrkBrk[1];
}
print_r($finalArray);
Output:
Array
(
[page] => Array
(
[0] => 18
[1] => 20
)
)
Or you can adopt this structure if it is better suited to your needs:
$finalArray = array();
foreach($mainStringBrk as $bString){
$mainStringBrkBrk = explode('-', $bString);
$finalArray[] = array($mainStringBrkBrk[0] => $mainStringBrkBrk[1]);
}
print_r($finalArray);
Output:
Array
(
[0] => Array
(
[page] => 18
)
[1] => Array
(
[page] => 20
)
)
Demo on 3v4l.org
I'm having trouble converting a string to a multi-dimensional array in php. This is my string:
$String = a,b,c|d,e,f|g,h,y|
This is what I'm trying:
$one=explode("|",$String);
foreach ($one as $item)
{
$one=explode(",",$one);
}
I'd like to create this array:
$array={ {a,b,c}, {d,e,f}, {g,h,y} };
Try with -
$one=explode("|",$String);
$array = array();
foreach ($one as $item){
$array[] = explode(",",$item);
}
Try this code:
$string = 'a,b,c|d,e,f|g,h,y|';
$arr = array_map(function($iter){ return explode(',',$iter);},explode('|',$string));
Hope it help a bit.
You have almost done it right, except for the cycle part. Try this
$result = [];
$String = 'a,b,c|d,e,f|g,h,y|';
$firstDimension = explode('|', $String); // Divide by | symbol
foreach($firstDimension as $temp) {
// Take each result of division and explode it by , symbol and save to result
$result[] = explode(',', $temp);
}
print_r($result);
Try this-
$String = 'a,b,c|d,e,f|g,h,y|';
$one = array_filter(explode("|", $String));
print_r($one); //Array ( [0] => a,b,c [1] => d,e,f [2] => g,h,y )
$result = array_map('v', $one);
function v($one) {
return explode(',',$one);
}
print_r($result); // Array ( [0] => Array ( [0] => a [1] => b [2] => c ) [1] => Array ( [0] => d [1] => e [2] => f ) [2] => Array ( [0] => g [1] => h [2] => y ) )
Use this code
$String= 'a,b,c|d,e,f|g,h,y|';
$one=explode("|",$String);
print_r(array_filter($one));
Output will be
Array
(
[0] => a,b,c
[1] => d,e,f
[2] => g,h,y
)
I have an array which looks like this:
Array
(
[0] => Array
(
[0] => TE=140414100000 cd =AB1234 ggg =1234567 gbh =2
[7] => nd: DA1AAAAAAAAAA: TD = 140414:
)
[1] => Array
(
[0] => TE=140414100000 cd =AB1234 ggg =1234567 ghb =2
[7] => nd: DA1AAAAAAAAAA: TD = 140414:
)
)
what I am trying to acomplish is to parse data within each sub array and create a new multidimensional array with the parsed data.
Example: the data in parentheses below is what should be returned in new multidimensional array
Array
(
[0] => Array
(
[0] => te=(140414100000) cd =AB(1234) ggg =1234567 ghb =2
[7] => nd: DA(1)(AAAAAAAAAA): TD = (140414):
)
[1] => Array
(
[0] => te=(140414100000) cd =AB(1234) ggg =1234567 ghb =2
[7] => nd: DA(2)(BBBBBBBBBB): TD = (140414):
)
)
What I want to return:
Array
(
[0] => Array
(
[0] => 140414100000
[1] => 1234
[2] => 1
[3] => AAAAAAAAAA
[4] => 140414
)
[1] => Array
(
[0] => 140414100000
[1] => 1234
[2] => 2
[3] => BBBBBBBBBB
[4] => 140414
)
).
So my question is what would be the best way to acomplish this?
This is what I have come up with. It works, however is seems very inefficient as it adds a lot of empty arrays which have to be cleaned up.
foreach($new as $key => $val){
foreach($val as $res){
preg_match_all('%te=([0-9]{12})\s%',$res,$matches);
$out[$key][] = $matches[1][0];
preg_match_all('%cd\s+=AB([0-9]{4})%',$res,$matches);
$out[$key][] = $matches[1][0];
preg_match_all('%nd:\sDA([0-9]{1})%',$res,$matches);
$out[$key]['node'] = $matches[1][0];
preg_match_all('%nd:\sDA[0-9]{1}([a-zA-Z]{10,14}):%',$res,$matches);
$out[$key]['rset'] = $matches[1][0];
preg_match_all('%td\s=\s([0-9]{6}):%',$res,$matches);
$out[$key]['trdt'] = $matches[1][0];
}
}
foreach($out as $v){
$v = array_values(array_filter($v));
$return[] = $v;
}
return $return;
Thanks in advance.
UPDATED:
This worked and is much more efficient. Thanks for the example Shankar
foreach($new as $key => $val){
$v = implode('', $val);
preg_match_all("%te=([0-9]{12})|cd\s+=AB([0-9]{4})|nd:\sDA([0-9]{1})|([A-Z]{3,7}):|td=\s([0-9]{6}):%",$v,$matches);
$new_array[$key]['time'] = $matches[1][0];
$new_array[$key]['code'] = $matches[2][1];
$new_array[$key]['sp'] = $matches[3][2];
$new_array[$key]['rset'] = $matches[4][3];
$new_array[$key]['trfdt'] = $matches[5][4];
}
echo "<pre>";
print_r($new_array);
echo "</pre>";
Loop through your array and implode each array element and use a preg_match_all() to capture all the entries bewteen ( and ) and then pass those matches to your new array.
foreach($arr as $k=>$arr1)
{
$v = implode('',$arr1);
preg_match_all('^\((.*?)\)^', $v, $matches);
$new_arr[]=$matches[1];
}
print_r($new_arr);
Working Demo
Actual array as below is basically the array of $_POST.
One can understand what is coming from the form. three controls with same name different value. What i need is below this array.
Array
(
[mytext] => Array
(
[0] => aaaa
[1] => bbbb
[2] => ddd
)
[mysel] => Array
(
[0] => one
[1] => two
[2] => two
)
[submit] => Submit
)
I need the array in row format below but be dynamic based of $_POST data.
Array
(
[0] => Array
(
[0] => aaaa
[1] => one
)
[1] => Array
(
[0] => bbbb
[1] => two
)
[2] => Array
(
[0] => dddd
[1] => two
)
)
Try this:
$out = Array();
foreach($_POST['mytext'] as $k=>$v) {
$out[$k] = Array($v,$_POST['mysel'][$k]);
}
var_dump($out);
// Code To Get controls value in row wise
$count=0;
foreach($_POST as $key=>$val){
foreach($_POST[$key] as $val2){
$row[$count][]=$val2;
$count++;
}
$count=0;
}
print_r($row);
Loop through one of the fields, and then pull the corresponding value from the other field.
$result = array();
foreach($_POST['mytext'] as $k=>$v){
$result[] = array($v, $_POST['mysel'][$k]);
}
You can also use array_map to do this:
// PHP 5.3+
$result = array_map(function($a, $b){
return array($a, $b);
}, $_POST['mytext'], $_POST['mysel']);
// PHP <= 5.2
$result = array_map(create_function('$a,$b', 'return array($a,$b);'), $_POST['mytext'], $_POST['mysel']);
I have an array like this:
Array (
[0] => ing_1_ing
[1] => ing_1_amount
[2] => ing_1_det
[3] => ing_1_meas
[4] => ing_2_ing
[5] => ing_2_amount
[6] => ing_2_det
[7] => ing_2_meas
)
And I want to group the values into an array like this:
Array (
[0] => Array(
[0] => ing_1_ing
[1] => ing_1_amount
[2] => ing_1_det
[3] => ing_1_meas
)
[1] => Array(
[0] => ing_2_ing
[1] => ing_2_amount
[2] => ing_2_det
[3] => ing_2_meas
)
)
There may be many other items named like that: ing_NUMBER_type
How do I group the first array to the way I want it? I tried this, but for some reason, strpos() sometimes fails:
$i = 1;
foreach ($firstArray as $t) {
if (strpos($t, (string)$i)) {
$secondArray[--$i][] = $t;
} else {
$i++;
}
}
What is wrong? Can you advice?
It depends what you are trying to achieve, if you want to split array by chunks use array_chunk method and if you are trying to create multidimensional array based on number you can use sscanf method in your loop to parse values:
$result = array();
foreach ($firstArray as $value)
{
$n = sscanf($value, 'ing_%d_%s', $id, $string);
if ($n > 1)
{
$result[$id][] = $value;
}
}
<?php
$ary1 = array("ing_1_ing","ing_1_amount","ing_1_det","ing_1_meas","ing_2_ing","ing_2_amount","ing_2_det","ing_2_meas");
foreach($ary1 as $val)
{
$parts = explode("_",$val);
$ary2[$parts[1]][]=$val;
}
?>
This creates:
Array
(
[1] => Array
(
[0] => ing_1_ing
[1] => ing_1_amount
[2] => ing_1_det
[3] => ing_1_meas
)
[2] => Array
(
[0] => ing_2_ing
[1] => ing_2_amount
[2] => ing_2_det
[3] => ing_2_meas
)
)
What I'd do is something like this:
$result = array();
foreach ($firstArray as $value)
{
preg_match('/^ing_(\d+)_/', $value, $matches);
$number = $matches[1];
if (!array_key_exists($number, $result))
$result[$number] = array();
$result[$number][] = $value;
}
Basically you iterate through your first array, see what number is there, and put it in the right location in your final array.
EDIT. If you know you'll always have the numbers start from 1, you can replace $number = $matches[1]; for $number = $matches[1] - 1;, this way you'll get exactly the same result you posted as your example.