So I'm trying to change an array that I have;
+rows: array:31 [▼
0 => array:2 [▼
0 => "20190101"
1 => "5"
]
1 => array:2 [▼
0 => "20190102"
1 => "15"
]
2 => array:2 [▼
0 => "20190103"
1 => "17"
]
To modify the index 0 of each row to a Carbon DateTime Object for further processing.
Now I'd like to have a json output, but I currently even can't get rid of the Illegal offset type error....
The code I'm using to retreive the data (from GoogleAnalytics) is the follwing:
$get_stats = Analytics::performQuery(
Period::create(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()),
'ga:users',
[
'dimensions' => 'ga:date',
'samplingLevel' => 'HIGHER_PRECISION',
'include-empty-rows' => true
]
);
This returns alot of code, but I just grab the $get_stats->rows (see above for that output).
I then make a new array, defined as $stats = [];.
A loop iterates over the $get_stats->rows, so I can 'modify' the data in the array and append it to the newly created array.
foreach ($get_stats->rows as $value)
{
$stats[] = [Carbon::createFromFormat('Ymd', $value[0]) => $value[1]];
}
However I get (always) the Illegal offset type error, on the line with $stats[] = [Carbon::createFromFormat('Ymd', $value[0]) => $value[1]];
Can someone help me out with this? I have no clue why it does this, when I try to append an array to an array. I tried array_push(), gives the same issue, also array_merge has the same issue...
This is happening because Carbon::createFromFormat('Ymd', $value[0]) returns a Carbon object, and objects are not valid array indexes. I'm not sure what you're trying to achieve by doing this conversion on the key, it seems a simple
$stats[] = [$value[0] => $value[1]];
or even
$stats[$value[0]] = $value[1];
might be more useful to you. You can always do the conversion to a Carbon object when you want to display the data.
Related
i wanted access the value of content_type_id .i tried like $courseChapters['chapter_content']['content_type_id'] , $courseChapters['content_type_id'] , $courseChapters[0]['content_type_id'] & $courseChapters['chapterContent']['content_type_id'].All these shows the error ErrorException: Undefined index: content_type_id . I also tried everything in the foreach loop i've commented below. Nothing works. Can someone tell me how to fix this?
/*foreach($courseChapters as $key){
// $contentTypeId=$key->first(); //Call to a member function first() on int
// $contentTypeId=$key->first()->chapter_content; //Call to a member function first() on int
// $contentTypeId=$key['chapter_content']['content_type_id']; //error - Illegal string offset
// $contentTypeId=$key[0]['content_type_id']; ////error - Illegal string offset
// $contentTypeId=$key['content_type_id']; //error - Illegal string offset 'content_type_id'
}*/
$courseChapters = courseChapter::with(['chapterContent' => function ($q) use ($id) { $q->where('course_chapter_id', $id)->select('course_chapter_id','file_id','content_type_id');}])
->select('id','courseId', 'chapter_title', 'isExam', 'points')
->get()->toArray()[0];
Heading ## dd($courseChapters); shows array like given below:
array:6 [
"id" => 4
"courseId" => 24
"chapter_title" => "chapter1"
"isExam" => false
"points" => 8
"chapter_content" => array:1 [
0 => array:3 [
"course_chapter_id" => 4
"file_id" => 1
"content_type_id" => 1
]
]
]
Like this to access the first
echo $courseChapters['chapter_content'][0]['content_type_id'];
or like this to access all of them
foreach ($courseChapters['chapter_content'] as $chapter) {
echo $chapter['content_type_id'];
}
I need to compare arrays, if element from first or second array has duplicates in another one I need to exclude it. I know it sound simply and I'm sure it is but i cant handle with that problem :(
So i have first array like this:
Array:3 [
6 => blog/something
4 => blog/somethingElse
5 => blog/else
]
Second array almost identical:
Array:3 [
1 => /blog
2 => /comments
3 => /posts
]
And the last array:
(integer on the left is id of elements in second array, in this example
comments and posts)
Array:2 [
0 => array:2 [
'page_id' => 2
'value' => noindex
]
1 => array:2 [
'page_id' => 3
'value' => noindex
]
]
So if I have element in array first or second which exist in array thrid too AND have value = noindex i need to exclude it.
I have tried do this by foreach recursive, by array_walk_recursive but I still can't get satisfied result
First get all the indices you need to exclude and then exclude them:
$excludeIndices = array_column(array_filter($array3, function ($entry) {
return $entry['value'] === 'noindex';
}), 'page_id');
$keepArray1 = array_diff_key($array1, array_flip($excludeIndices));
$keepArray2 = array_diff_key($array2, array_flip($excludeIndices));
Sandbox
You can filter using the first two arrays directly.
$result = array_filter($last, function($item) use ($first, $second) {
return !($item['value'] == 'noindex' &&
(isset($first[$item['page_id']]) || isset($second[$item['page_id']]))
);
});
I'm having a looping mind-breaking issue which I can't seem to solve myself. Currently working on saving a form input in a webshop. The data:
"personalisation" => array:3 [▼
0 => "embroidery"
1 => "printing"
2 => "embroidery"
]
"repeat" => array:2 [▼
0 => "true"
1 => "true"
]
"selectedColors" => array:1 [▼
0 => "3"
]
The problem which I have here: I need to loop through the personalisation array to add to my DB. With the embroidery, the repeat value is linked and for the printing the selectedColors is linked. How can I loop through the personlisation array and match the values from the other array?
I really wouldn't recommend designing forms like this, you're basically just sending a jumbled mess to your backend with no association.
You can "correct" the association by filtering the personalisation array and reindexing it so the keys match the other arrays.
$embroderies = array_values(array_filter($array['personalisation'], function($item) {
return $item === 'embroidery';
}));
foreach($emborderies as $key => $value) {
// get value from $array['repeat'][$key];
}
I can't think of any other way than to use a helper array for example.
It could be
array('embroidery' => 'repeat', 'printing' => 'selectedColors')
And you start looping through personalization, depending on the value you use it as a key in the helper array, then finally get the wanted value from the array.
1st iteration: 0/embroidery -> embroidery/repeat -> repeat/true
2nd iteration: 1/printing -> printing/selectedColors -> selectedColors/
...
I am currently trying to learn Laravel and PHP in general.
I need to be able to import an Excel file, then get the data from that file. Currently, the import part works and I can see the data from the file. However, I am having trouble accessing the data.
I've read about the toArray() function in Laravel, and is using that as below:
$data = Excel::load($path, function($reader) {})->skipColumns(2)->get();
$data = $data->toArray();
foreach ($data as $key => $value) {
//We only need some of the available data.
echo $value->next_milestone_desc_cur._comp._incl_rltd;
echo $value->shipment_id;
}
Above code gives me below error:
Trying to get property 'next_milestone_desc_cur' of non-object
Below is an output from the array, which I have generated using dd($value):
array:543 [▼
0 => array:20 [▼
"next_milestone_desc_cur._comp._incl_rltd" => "005. DK - Add DropMode/Local Trp Org in Pickup Tab"
"milestone_cur._comp._sequence_no" => "005"
"cur._comp._export_validation" => "NOT OK"
"shipment_id" => "SBRY0162091"
"consol_id" => "CDK327188" ]
1 => array:20 [▼
"next_milestone_desc_cur._comp._incl_rltd" => "005. DK - Add DropMode/Local Trp Org in Pickup Tab"
"milestone_cur._comp._sequence_no" => "005"
"cur._comp._export_validation" => "NOT OK"
"shipment_id" => "SBRY0162124"
"consol_id" => "CDK327221"
]
What am I doing wrong here? I have also tried
echo $value[0]->next_milestone_desc_cur._comp._incl_rltd;, however this doesn't work either.
You are trying to access an array as an object hence your error and to address you second point, you need to use a nested loop to then access the sub-array.
Your array looks something like this:
$array = [
0 => [
0 => [
'test' => 'value'
],
1 => [
'test' => 'something'
]
],
1 => [
0 => [
'test' => 'value'
],
1 => [
'test' => 'something'
]
]
];
You need the following loop:
foreach ($array as $key => $nested) {
foreach ($nested as $nested_value) {
// You have access to your inner arrays.
echo $nested_value['test'] . ' | ';
}
}
Where your nested loops $nested would be $value.
Live Example
Repl
Your output from dd($value) shows that $value is an array. So you cannot use arrow notation that is for objects. Change these lines:
echo $value->next_milestone_desc_cur._comp._incl_rltd;
echo $value->shipment_id;
To:
echo $value[0]["next_milestone_desc_cur._comp._incl_rltd"];
echo $value[0]["shipment_id"];
this might be a bit of a generic title, but I will try to explain my problem the best way I can.
So I've got this type of associative array:
Array
(
[ICO_0] => checked
[Investment_0] => checked
[Investment_1] => checked
[Marketing_1] => checked
)
What I would like to do is to divide it into multiple arrays based on numbers that are attached to the end of the key ( 0, 1 ... ). Basically I'd like to get a new array to look like this:
Array(
Array(
[ICO_0] => checked
[Investment_0] => checked
[Token Sale_0] => checked
),
Array(
[Investment_1] => checked
[Marketing_1] => checked
)
)
I've tried approaching this issue with array_chunk but couldn't get it to work.
I'll need this output since I want to sort those nested arrays based on items that they're holding, starting from the highest number of items.
Thanks!
You can use several different methods to accomplish this. One method is to loop through and explode the key name if you know there will always be an underscore, grab the last number, and use that as your index:
$results = [
"ICO_0" => "checked",
"Investment_0" => "checked",
"Investment_1" => "checked",
"Marketing_1" => "checked",
"Investment_2" => "checked",
"Marketing_2" => "checked"
];
foreach($results as $key => $value){
$ex = explode('_', $key);
$new_result[end($ex)][$key] = $value;
}
Which for me returns the following:
array:3 [▼
0 => array:2 [▼
"ICO_0" => "checked"
"Investment_0" => "checked"
]
1 => array:2 [▼
"Investment_1" => "checked"
"Marketing_1" => "checked"
]
2 => array:2 [▼
"Investment_2" => "checked"
"Marketing_2" => "checked"
]
]