I tried in many ways, but none is my specific problem, I tried to use what I know, but to no avail. I'm trying to use file_get_contents to get the json from 1 file.json on my server, and calculate a specific array, but after the "array_column" it returns an empty array.
<?php
$json_url = "http://myserver/file.json";
$json = file_get_contents($json_url);
$data = json_decode($json, true);
$price = array_column($data, 'valor');
print_r($price);
//output = array() | empyty array
the $json return the correct json,
in the $data return the corret decoded json,
but in $price, return empty array()
the $json content of my json file in my server is similar to it:
{
"1228421731": [
{
"valor": 10,
"datelimite": 1644024834,
"date": 1643420034,
"codigo": "ALYCGKU76S"
}
],
"1111925985": [
{
"valor": 25,
"datelimite": 1644066350,
"date": 1643461550,
"codigo": "HK8NQROGBW"
}
],
"1413051871": [
{
"valor": 50,
"datelimite": 1644084293,
"date": 1643479493,
"codigo": "4NGJ523J6H"
}
],
"1209626299": [
{
"valor": 10,
"datelimite": 1644091732,
"date": 1643486932,
"codigo": "W7LRGY3FHZ"
}
],
"1803561706": [
{
"valor": 10,
"datelimite": 1644257719,
"date": 1643652919,
"codigo": "GE8IDPZN9H"
},
{
"valor": 19865091226,
"datelimite": 1644288984,
"date": 1643684184,
"codigo": "F5Q9TPE43SWAFX"
}
],
"1710879952": [
{
"valor": 19865091226,
"datelimite": 1644274668,
"date": 1643669868,
"codigo": "P5M4E6RP1ZPEUR"
}
],
"1907375762": [
{
"valor": 10,
"datelimite": 1644377257,
"date": 1643772457,
"codigo": "E8OKPVCYVWRHUI"
}
],
"1863764959": [
{
"valor": 25,
"datelimite": 1644427955,
"date": 1643823155,
"codigo": "L78EZKJZJ93UYW"
}
],
"1831713303": [
{
"valor": 10,
"datelimite": 1644442109,
"date": 1643837309,
"codigo": "3J84VTS5FRS6OI"
}
],
"5193759120": [
{
"valor": 10,
"datelimite": 1644453308,
"date": 1643848508,
"codigo": "88942HFHZ5JJ56"
}
],
"1785872541": [
{
"valor": 10,
"datelimite": 1644504518,
"date": 1643899718,
"codigo": "LBML4O31RLC7DW"
}
],
"1666986497": [
{
"valor": 1,
"datelimite": 1644667829,
"date": 1644063029,
"codigo": "HXHPK6XLNBD89E"
}
]
}
how can i use array_column for this json type? for the array 'valor'?
$data is in this format
Array
(
[1228421731] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644024834
[date] => 1643420034
[codigo] => ALYCGKU76S
)
)
[1111925985] => Array
(
[0] => Array
(
[valor] => 25
[datelimite] => 1644066350
[date] => 1643461550
[codigo] => HK8NQROGBW
)
)
[1413051871] => Array
(
[0] => Array
(
[valor] => 50
[datelimite] => 1644084293
[date] => 1643479493
[codigo] => 4NGJ523J6H
)
)
[1209626299] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644091732
[date] => 1643486932
[codigo] => W7LRGY3FHZ
)
)
[1803561706] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644257719
[date] => 1643652919
[codigo] => GE8IDPZN9H
)
[1] => Array
(
[valor] => 19865091226
[datelimite] => 1644288984
[date] => 1643684184
[codigo] => F5Q9TPE43SWAFX
)
)
[1710879952] => Array
(
[0] => Array
(
[valor] => 19865091226
[datelimite] => 1644274668
[date] => 1643669868
[codigo] => P5M4E6RP1ZPEUR
)
)
[1907375762] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644377257
[date] => 1643772457
[codigo] => E8OKPVCYVWRHUI
)
)
[1863764959] => Array
(
[0] => Array
(
[valor] => 25
[datelimite] => 1644427955
[date] => 1643823155
[codigo] => L78EZKJZJ93UYW
)
)
[1831713303] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644442109
[date] => 1643837309
[codigo] => 3J84VTS5FRS6OI
)
)
[5193759120] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644453308
[date] => 1643848508
[codigo] => 88942HFHZ5JJ56
)
)
[1785872541] => Array
(
[0] => Array
(
[valor] => 10
[datelimite] => 1644504518
[date] => 1643899718
[codigo] => LBML4O31RLC7DW
)
)
[1666986497] => Array
(
[0] => Array
(
[valor] => 1
[datelimite] => 1644667829
[date] => 1644063029
[codigo] => HXHPK6XLNBD89E
)
)
)
so I think you can try to get the first array element in each array and then operate on those arrays, like this
$price = array_column(array_column($data, 0),'valor');
This will give the results
You can try this
array_column(array_merge(...(json_decode($json,true))),"valor")
This will return an array with all the values of valor
If you want an array of all valor values:
$flatArray = array_reduce(
json_decode($json, true),
fn($carry, $item) => $carry = array_merge($carry, array_column($item, 'valor')),
[]
);
If you want an array grouped by valors belonging together:
$groupedArray = array_reduce(
json_decode($json, true),
fn($carry, $item) => $carry = array_merge($carry, [ array_column($item, 'valor') ]),
[])
;
Related
I have the following php code that produces a json data set
$json_data = array(
"code"=>"200",
"name"=>"My Name",
"serial_number"=>"serial"
);
$result = json_encode($json_data);
The dataset is only one level. As I am creating this data set with a php loop. I want to be able to have multiple people but the code element be outside the users. So basically I want the json data to look like this:
{
"code": "404",
"people": [
{
"name": "Person 1",
"serial_number": "xyz"
},
{
"name": "Person Two",
"serial_number": "123"
}
]
}
Basically you want to group array of objects by property "code". There are many questions about this. Here's one of the possible methods using array_reduce.
$json_data = [
[
"code" => "200",
"name" => "My Name1",
"serial_number" => "serial1",
], [
"code" => "200",
"name" => "My Name2",
"serial_number" => "serial2",
], [
"code" => "400",
"name" => "My Name3",
"serial_number" => "serial3",
],
];
$result = array_values(array_reduce($json_data, function ($agg, $item) {
if (!isset($agg[$item['code']])) {
$agg[$item['code']] = [
"code" => $item['code'],
"people" => [],
];
}
$agg[$item['code']]['people'][] = [
"name" => $item["name"],
"serial_number" => $item["serial_number"],
];
return $agg;
}, []));
print_r($result);
Output:
Array
(
[0] => Array
(
[code] => 200
[people] => Array
(
[0] => Array
(
[name] => My Name1
[serial_number] => serial1
)
[1] => Array
(
[name] => My Name2
[serial_number] => serial2
)
)
)
[1] => Array
(
[code] => 400
[people] => Array
(
[0] => Array
(
[name] => My Name3
[serial_number] => serial3
)
)
)
)
I want to change the laravel collection because I want to pass this collection to generate report.
How to change collection on the basis of the value in the column.
Previous:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 23
[form_type] => radio
[details] => [{"id":55 title:"AA" name:"BB"}, {"id":56 title:"CC" name:"DD"}]
)
[1] => stdClass Object
(
[id] => 24
[form_type] => checkbox
[details] => [{"id":57 title:"PP" name:"QQ"}, {"id":58 title:"RR" name:"SS"}]
)
)
)
--details column has array of objects, I want it as below format:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 23
[form_type] => radio
[title] => "AA"
[name] => "BB"
)
[1] => stdClass Object
(
[id] => 23
[form_type] => radio
[title] => "CC"
[name] => "DD"
)
[2] => stdClass Object
(
[id] => 24
[form_type] => checkbox
[title] => "PP"
[name] => "QQ"
)
[4] => stdClass Object
(
[id] => 24
[form_type] => checkbox
[title] => "RR"
[name] => "SS"
)
)
)
The only way is to use each method of Laravel collection. I expect the data in the following format and you will get the desired output.
$data = collect([
(object)["id" => 23, "form_type" => "radio", "details" => [["id" => 55, 'title' => "AA", 'name' => "BB"], ["id" => 56, 'title' => "CC", 'name' => "DD"]]]
]);
$result = collect();
$data->each(function($d) use ($result) {
$d = collect($d);
unset($t['id']);
$tmp = collect($d['details']);
$tmp->each(function($t) use (&$d, $result) {
$result->push($d->forget('details')->merge($t));
});
});
echo "<pre>";
print_r($result);
try this with ->map() function then ->push() into new collection
$data = [
[
"id" => 23,
"form_type" => "radio",
"details" => [["id" => 55, "title" => "AA", "name" => "BB"], ["id" => 56, "title" => "CC", "name" => "DD"]]
],
[
"id" => 24,
"form_type" => "checkbox",
"details" => [["id" => 57, "title" => "PP", "name" => "QQ"], ["id" => 58, "title" => "RR", "name" => "SS"]]
],
];
$data = collect($data);
$final = collect();
$data->map(function ($row) use ($data, $final) {
return collect($row['details'])->map(function ($item) use ($data, $row, $final) {
$parentData = $data->where('id', $row['id'])->first();
$final->push([
'id' => $parentData['id'],
'title' => $item['title'],
'form_type' => $parentData['form_type'],
'name' => $item['name']
]);
});
});
return $final;
I'm trying to create a timeline with an apex chart.
I can't literally print the Json data.
$temp[$r["Durum"]]['data'][] = array(
'x' => $r['Basladı'],
'y' => array(
strtotime($r['Saat']),
strtotime($r['Bitis'])
)
);
output
{
"P": {
"data": [
{
"x": "Basladi",
"y": [
1602418875,
1602418884
]
},
{
"x": "Basladi",
"y": [
1602418887,
1602418902
]
},
]
}
}
output I want
{
{
"name" : "P",
"data": [
{
"x": "Basladi",
"y": [
1602418875,
1602418884
]
},
{
"x": "Basladi",
"y": [
1602418887,
1602418902
]
},
]
}
}
Here is a code example with some sample input that will work for you.
Sample data (in your case the data from your database):
$yourdata = array(
array(
'Durum' => 'p',
'Basladı' => 'Basladı',
'Saat' => '12.12.2020',
'Bitis' => '12.12.2020'
),
array(
'Durum' => 'p',
'Basladı' => 'Basladı',
'Saat' => '12.12.2020',
'Bitis' => '12.12.2020'
),
array(
'Durum' => 's',
'Basladı' => 'Basladı',
'Saat' => '12.12.2020',
'Bitis' => '12.12.2020'
)
);
Code snippet:
// create empty temp array for the result
$temp = [];
// loop through your data (only an example - replace with your data)
foreach($yourdata as $r) {
// defines a temp data array to add
$temp_data = array(
'x' => $r['Basladı'],
'y' => array(
strtotime($r['Saat']),
strtotime($r['Bitis'])
)
);
// check if there is already an entry with that name
if(($key = array_search($r["Durum"], array_column($temp, 'name'))) !== false) {
// if there is, only add the data to it
$temp[$key]['data'][] = $temp_data;
} else {
// otherwise push a new array with the name and data to temp
$temp[] = array(
'name' => $r["Durum"],
'data' => array($temp_data)
);
}
}
var_dump($temp); // process with the data
The content of the variable $temp:
Array
(
[0] => Array
(
[name] => p
[data] => Array
(
[0] => Array
(
[x] => Basladı
[y] => Array
(
[0] => 1607731200
[1] => 1607731200
)
)
[1] => Array
(
[x] => Basladı
[y] => Array
(
[0] => 1607731200
[1] => 1607731200
)
)
)
)
[1] => Array
(
[name] => s
[data] => Array
(
[0] => Array
(
[x] => Basladı
[y] => Array
(
[0] => 1607731200
[1] => 1607731200
)
)
)
)
)
I have a array like this :
Array
(
[0] => Array
(
[id] => 1
[child_name] => "Emma"
[parent_name] => "Mr Brown"
)
[1] => Array
(
[id] => 2
[child_name] => "John"
[parent_name] => "Mr Brown"
)
[2] => Array
(
[id] => 3
[child_name] => "Joseph"
[parent_name] => "Mr Thomas"
)
[3] => Array
(
[id] => 4
[child_name] => "Pretty"
[parent_name] => "Mr Thomas"
)
[4] => Array
(
[id] => 5
[child_name] => "Raphel"
[parent_name] => "Mr Brown"
)
[5] => Array
(
[id] => 6
[child_name] => "Tommy"
[parent_name] => "Mr Thomas"
)
[6] => Array
(
[id] => 7
[child_name] => "Tim"
[parent_name] => "Mr Thomas"
)
)
From this array I want to generate a view like this :
The parent_name field becomes the MainCategory and the child_name becomes the subcategory. The names have checkboxes in front of them.
How do I achieve this? I don't have much experience in php. I code in node js but this task needs this to be done in php.
How do I do this?
Try this, Here i use simple index() method and pass data to view file as an example.
You can use below code and test in your codeigniter.
Hope this will work for you.
Welcome.php (Controller)
public function index()
{
$array = [
[
'id' => 1,
'child_name' => "Emma",
'parent_name' => "Mr Brown",
],
[
'id' => 2,
'child_name' => "John",
'parent_name' => "Mr Brown",
],
[
'id' => 3,
'child_name' => "Joseph",
'parent_name' => "Mr Thomas",
],
[
'id' => 4,
'child_name' => "Pretty",
'parent_name' => "Mr Thomas",
],
[
'id' => 5,
'child_name' => "Raphel",
'parent_name' => "Mr Brown",
],
[
'id' => 6,
'child_name' => "Tommy",
'parent_name' => "Mr Thomas",
],
[
'id' => 7,
'child_name' => "Tim",
'parent_name' => "Mr Thomas",
],
[
'id' => 8,
'child_name' => "William",
'parent_name' => "",
],
];
$resultArray = [];
foreach ($array as $key => $value) {
if($value['parent_name'] != ''){
$resultArray[$value['parent_name']][] = $value;
}else{
$resultArray[$value['child_name']] = [];
}
}
$data = ['resultArray' => $resultArray];
$this->load->view('welcome_message', $data);
}
welcome_message.php (view)
<div>
<form name='sample'>
<?php
foreach ($resultArray as $parentKey => $parentValue) {
echo '<input type="checkbox" name="'.$parentKey.'" value="'.$parentKey.'">'.$parentKey.'<br>';
foreach ($parentValue as $childKey => $childValue) {
echo ' <input type="checkbox" name="'.$childValue['child_name'].'" value="'.$childValue['child_name'].'">'.$childValue['child_name'].'<br>';
}
}
?>
</form>
</div>
Output
Check this,
Loop your array, it will capture all values with same parent_name.
$result = [];
foreach ($array as $key => $value) {
$result[$value['parent_name']][] = $value['child_name']; // loop your array
}
It should work.
Hi i have the following array and i want to pull the array with productId 100343
Array(
[_id] => MongoId Object
(
[$id] => 5388a02c8ead0ebf048b4569
)
[cartId] => 14ce496ac194d5d8ee8d0cd11bd3ef5a
[products] => Array
(
[100343] => Array
(
[productId] => 100343
[quantity] => 13
[name] => a
)
[100344] => Array
(
[productId] => 100344
[quantity] => 3
[name] => ab
)
[100345] => Array
(
[productId] => 100345
[quantity] => 1
[name] => abc
)
)
)
I've tried like this but it doesn't work
$c->update(
$aQuery,
array(
'$pull' => array('products'=>array('productId'=>'100343'))
)
);
That is not an array. Arrays in MongoDB are different in concept to what PHP calls an array, so the structure you have there is the same as this JSON representation:
{
"products": {
"100343": {
"productId": 100343,
"quantity": 13,
"name": "a"
},
"100344": {
"productId": 100344,
"quantity": 3,
"name": "ab"
},
"100345": {
"productId": 100345,
"quantity": 1,
"name": "abc"
}
}
}
That sort of structure is just a use of sub-documents and is not an array in the sense as used by MongoDB. To remove the entry as you want with this structure you use the $unset operator.
$c->update(
$aQuery,
array(
'$unset' => array('products.productId.100343' => "")
)
);
But really you need to change your data, so to serialize a proper array for MongoDB you need to structure code like this:
$data = array(
"products" => array(
array(
"productId" => 100343,
"quantity" => 13,
"name" => "a"
),
array(
"productId" => 100344,
"quantity" => 3,
"name" => "ab"
),
array(
"productId" => 100345,
"quantity" => 1,
"name" => "abc"
)
)
);
That produces a JSON structure that looks like this:
{
"products":[
{
"productId":100343,
"quantity":13,
"name":"a"
},
{
"productId":100344,
"quantity":3,
"name":"ab"
},
{
"productId":100345,
"quantity":1,
"name":"abc"
}
]
}
That is what MongoDB calls an array, so now you can use the $pull operator correctly:
$c->update(
$aQuery,
array(
'$pull' => array( 'products.productId' => 100343 )
)
);