I have variabel date as array like $date = [1,2,3,....,30]
I want to looping this array like in gridview like
column = [
for($i = 0; $i <= count($date); $i++){
[
label => $date[$i]
value => $date[$i]
]
}
]
Column arrange something like that
$columns =[];
$columns[] = "name";
$columns[] = "email";
for($i = 0; $i <= count($date); $i++){
$columns[] = [
'label' => $date[$i]
'value' => function ($model) use($date,$id){
// Your magic code
}
]
}
And pass it on GridView
columns => $columns
Related
i looping 2 array a date and the data
$rdate = $request->month; //<-- Requested month
$days = Carbon::create();
$days = $days->daysInMonth; //<-- Finding how much days in month
for ($i = 1; $i < $days; $i++) { //Looping Date
$date = $rdate . '-' . $i; //Making full Date with formate 'Y-m-d'
foreach ($games as $game) { //Looping game array have 3 collection ['game1', 'game2', 'game3']
$result = Result::whereDate('created_at', $date)->where('game', $game->name)->where('admin_id', $admin->id)->first(); // Query Results for getting perticular game with date
if ($result) { //Checking if current date haven't result
$r[] = [
'game' => $game->name,
'number' => $result->number
];
} else {
$r[] = [
'game' => $game->name,
'number' => '-'
];
}
}
$resultd[] = [
'date' => $date,
'results' => $r // i want to stop adding old data here
];
}
i am expecting this result
{"results":[{"date":"2020-08-1","results":[{"game":"game1","number":"-"},{"game":"game2","number":"-"},{"game":"game3","number":"-"}]},{"date":"2020-08-2","results":[{"game":"game1","number":"-"},{"game":"game2","number":"-"},{"game":"game3","number":"-"}]
What actually getting
Adding old $r to results array
How to fix it i am trying to break it but can't figure it out
I have some untested code, as I do not have your data but I think this code below should work, you may need to modify some code as needed.
$rdate = '2020-8';
$days = collect([1,2,3]);
$games = collect(['game1', 'game2', 'game3']);
return $days->map(function ($item) use ($rdate,$games) {
$date = $rdate.'-'.$item;
return [
"date" => $date,
"results" => $games->map(function ($g) use ($date) {
$result = Result::whereDate('created_at', $date)
->where('game', $g->name)
->where('admin_id', $admin->id)
->first();
return ['game' => $g->name,
'number' => $result ? $result->number : '-',
];
})
];
});
Basically, what I'm trying to do is loop inside a loop and in arrays. I have no idea how to do it (that's why I'm here) and I've been testing many things.
As it sounds very confused I write down the code how it would be, but clearly that's not correct.
This below is what I've tried.
$whatever->insertOne(
['name' => 'whatever',
'data' => array(
for ($i = 0 ; $i < 50 ; $i++) { // <-first loop
'something' => array(
for ($j = 0 ; $j < 50 ; $j++) { // <-second loop
'somevalue' => array(
'date' => $date,
'value' => mt_rand(0,200)
)
}
)
}
)
]);
Try this out:
$data = array();
for ($j = 0 ; $j < 50 ; $j++) {
for ($i = 0 ; $i < 50 ; $i++) {
$data[$j]['something'][$i]['date'] => $date;
$data[$j]['something'][$i]['value'] => mt_rand(0,200);
}
}
$whatever->insertOne(['name' => 'whatever','data' => $data]);
Loop need to be outside to create final array. Don't add loop inside an array.
You probably need something like below:-
$data = ['name' => 'whatever'];
for ($i = 0 ; $i < 50 ; $i++) {
for ($j = 0 ; $j < 50 ; $j++) {
$data[$i]['something'][$j]=['somevalue' => array('date' => $date,'value' => mt_rand(0,200));
}
}
$whatever->insertOne($data);
Note:- you can print your array before insertOne() to check that you are getting array of correct format or some more manipulation needed.Thanks
I'm working in PHP.
I have a dynamic number of arrays called 'placeholder0, placeholder1, placeholder2....' and I want to json_encode them
$encode = json_encode(array('tabs' => $tabs, 'placeholder0' => $placeholder0, 'placeholder1' => $placeholder1, 'placeholder2' => $placeholder2 ..... ));
The number of values inside the 'tabs' array is the same as the number of 'placeholder' arrays.
How can I do this?
$result = array('tabs' => $tabs);
for ($i = 0; $i < count($tabs); $i++) {
$result['placeholder' . $i] = ${'placeholder' . $i};
}
file_put_contents($jsonLocation, json_encode($result));
I need a proper JSON file with "geo_langitude", "geo_latitude", "adress" & "url" values
I have wp database with post_meta "property" - "geo_langitude" "geo_latitude" & "address" in there.
my file is smth like that
<?php
function return_markers($count) {
$query = new WP_Query('post_type=property&posts_per_page=-1&orderby=menu_order&order=DESC&post_status=publish');
$array = array();
$i = 0;
for ($i = 0; $i< $count; $i++) {
$elem = array(
't' => 'string '.$i,
'x' => get_post_meta($post->ID,'geo_latitude',true),
'y' => get_post_meta($post->ID,'geo_longitude',true),
'z' => $i
);
$array[] = $elem;
}
echo json_encode($elem);
};
return_markers($i);
?>
It's my first time using JSON so I have troubles and I don't know where. =(
with random markers work perfectly:
<?php
function return_markers($count) {
$array = array ();
for ($i = 0; $i< $count; $i++) {
$elem = array(
't' => 'string '.$i,
'x' => 23 + (rand ( 0 , 10000 ) / 10000) * 5,
'y' => 56.2 + (rand ( 0 , 10000 ) / 10000) * 0.8,
'url' => '#map_redirect_'.$i,
'z' => $i
);
$array[] = $elem;
}
echo json_encode($array);
};
return_markers(23);
?>
I want to create an array in a for-loop so i can adjust the size ($i) of the array.
I've tried this:
$array = array();
for($i = 1; $i <= 5; $i++) {
array_push($array,
$i => array(
"id" => "",
"option" => ""
)
);
}
But I get the following error:
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in ...
I'v also tried to make it a string by doing $i."" on line 4 but that doesn't seem to work either. Does anyone know why?
More idiomatic would be:
$array = array();
for($i = 1; $i <= 5; $i++) {
$array[$i] = array(
"id" => "",
"option" => "") ;
}
However note that this will give you array indexes from 1-5. Arrays are usually indexed from 0:
$array = array();
for($i = 0; $i < 5; $i++) {
$array[$i] = array(
"id" => "",
"option" => "") ;
}
But this can be done without specifying the key:
$array = array();
for($i = 1; $i <= 5; $i++) {
$array[] = array(
"id" => "",
"option" => "") ;
}
try this:
$array = array();
for($i = 1; $i <= 5; $i++) {
$array[$i] = array(
"id" => "",
"option" => ""
);
}
Remove the $i =>
$array = array();
for($i = 1; $i <= 5; $i++) {
array_push($array, array(
"id" => "",
"option" => ""
)
);
}