Use a json decode array without foreach - php

I have a following json:
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
Are names and Birthdays. Is there any way to use a criteria to search birthdays without foreach?
For example, "all registry with birthdays after 1993-01-01".
Thanks

I was able to do this with array_filter function
function greater_than($arr){
$date = $arr['bday'];
if(strtotime($date) > strtotime('1993-01-01')){
return $date;
}
}
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
$dec = json_decode($json,true);
print_r(array_filter($dec, "greater_than"));

Related

How to convert array to string in laravel

$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();
This return something like array format
[{"DATE":"2021-06-11 00:00:00","point_no":"SRT-358"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-359"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-360"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-357"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-356"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-355"},{"DATE":"2021-06-09 00:00:00","point_no":"SRT-348"}]
I want to isolate DATE and point_no. For example
$year=['2015','2016','2017','2018','2019','2020'];
I tried $date['DOCDATE'] to isolate date it's not working. Is there any better way to fix this??
$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();
$date_array = array();
foreach($date as $dat){
array_push($date_array, \Carbon\Carbon::parse($dat["DATE"])->format('Y');
}
return $date_array;
You can use array_map function.
$extractYear = function($date) {
return substr($date["DATE"], 0, 4);
};
$years = array_map($extractYear, $date);
try this query -
$years= gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get()->map(function ($q) {
return [Carbon::parse($q->DATE)->format('Y')];
});

Convert a string structured as a Multidimensional Array to array

I have this string:
array(array('1','name1','1','0'),array('2','name2','0','1'),array('3','name3','0','1'),array('4','name4','1','1'),array('5','name5','1','0'));
Stored in $_POST['data']
The string Im receiving is via.load` function where the structure of the string is constructed like so.
I would like to convert it to a multidimensional array via php so I can loop through it easily
So far I`ve reached a workaround by modifying both the string and the method.
Now my string looks like this :
1,name1,1,0,|2,name2,0,1,|3,name3,0,1,|4,name4,1,1,|5,name5,1,0,|
And the method is this
$data2 = $_POST['data2']; /// get data
$data2 = substr_replace($data2 ,"", -2); // eliminate ,|
$data2 = $data2."|"; // add |
$one=explode("|",$data2); // create multidimensional array
$array = array();
foreach ($one as $item){
$array[] = explode(",",$item);
}
I can keep this solution but I would like to know if there is another way of doing it as first requested
There is a better and simple way. You just need to use a foreach loop inside foreach loop.
$data = array(
array('1','name1','1','0'),
array('2','name2','0','1'),
array('3','name3','0','1'),
array('4','name4','1','1'),
array('5','name5','1','0')
);
foreach( $data as $d ) {
foreach( $d as $value ) {
echo $value;
echo '<br />';
}
}
You can check the online Demo
To parse your original string you can use eval()
$string = 'array(array('1','name1','1','0'),array('2','name2','0','1'),array('3','name3','0','1'),array('4','name4','1','1'),array('5','name5','1','0'));';
eval('$array = '.$string);
But eval can/should be disabled on the server, because it comes with security issues.
What i would do is to use JSON, where you would POST the json encoding it with:
json_ecnode( $my_array );
and then decoding it:
$array = json_decode( $_POST['data'], true );

PHP changing array format

I'm trying to change an array format from this
{"updated":"2016-01-28 02:00:02","rate":"0.1898"}
to this
[2016-01-28 02:00 , 0.1898]
I'm getting the first array format from a MySQL query and need to convert to the second format to use in a line chart.
$newVal = array();
foreach ($dbresult as $key => $value) {
$newVal[] = json_encode(array_values($value));
}
echo implode(",", $newVal);
With this new code block i get this format
["2016-01-28 02:00" , "0.1898"]
But still need to get rid of the double quotes, any ideas?
$json = '[{"updated":"2016-01-28 02:00:02","rate":"0.1898"}]';
echo json_encode(array_map(function ($data) {
return [$data->updated, $data->rate];
}, json_decode($json)));
In other words:
JSON-decode it
loop through it
create a new array with updated in the first index and rate in the second
JSON-encode it again
Step 3 is necessary since JSON objects don't guarantee any particular order, so relying on the decoded order of the keys is not reliable; and it also guarantees you get the data you want even if you add more keys to your objects.
Try this code
$string = ' {"updated":"2016-01-28 02:00:02","rate":"0.1898"}';
$array = json_decode($string, true);
$str = json_encode(array_values($array));
echo str_replace('"', '', $str);
you should try this
$str ='{"updated":"2016-01-28 02:00:02","rate":"0.1898"}';
$data=json_decode($str);
echo '<pre>';
echo 'json decode it returns ojbects<br>';
print_r($data);
echo 'convert object into array<br>';
$data=(array)$data;
print_r($data);
echo 'Your Output<br>';
echo json_encode(array_values($data));

How to separate JSON Array with space in PHP

I have a JSONArray as
{"test":
[
{"Name":"aaa","Reg/Admission Number":"001"},
{...}]}
And I can separate Name by
$read_data = array();
foreach ($data->test as $result){
$name = $result->Name;
$read_data[] = "('$name')";
}
the result of read_data as ('aaa'),('bbb')...
Anyone can suggest how to separate the array of Reg/Admission Number which has the special character as '/' and 'space'
Why Dont you use the php function
json_encode() http://php.net/manual/en/function.json-encode.php
json_decode() http://php.net/manual/en/function.json-decode.php
As per #haxxxton suggestion it works
$read_data = array();
foreach ($data->test as $result){
$name = $result->Name;
$no = $result->{'Reg/Admission Number'}`
$read_data[] = "('$name','$no')";
}
You can convert json array to PHP array first and then can do your operation.

make the data into an array and get the values

I have the data in my table like this
Arabic,Assamese,Azerbaijani,Belarusian
I want to show the data in an array so that I can use foreach and get the values for the array. So can someone tell me how to make it as an array and get values?
$string = "Arabic,Assamese,Azerbaijani,Belarusian";
$language_array= explode(',',$string);
Supposing you have perform the query to the database and already has the data you can use explode function. See this DEMO.
$data = $row['data_from_table']; //your data is Arabic,Assamese,Azerbaijani,Belarusian
$exp = explode(",", $data);
foreach ($exp as $value){
echo $value;
echo PHP_EOL;
}
?>
Use explode function
$database_value = $db['your_data'];
$value_array = explode(',',$database_value);
print_r($value_array);
foreach ($value_array as $value){
echo $value.'<br>';
}

Categories