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"];
Related
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.
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"
]
]
I would need to insert an associative array into my existing code, to make an HTTP request.
My code at the moment:
$payload =[
'method_id' => 2,
'api_key' => 5,
];
$res = $client->post('some.website', [,
'form_params' => [
foreach($this->payload as $key => $s_key) {
$key => $s_key;
}
],
]);
How to make now sure, each element of the $payload array is inserted into the form_params array?
I tried using:
foreach ($this->payload as $s_key => $key) {
//?!
}
But I don't know how to proceed inside the form_params element?
Using the payload array directly inside form elements is resulting into this:
"form_params" => [
0 => array:2 [
"method_id" => 2
"api_key" => 5
]
]
What I would need is something like this:
"form_params" => [
"method_id" => 2
"api_key" => 5
]
You should just be able to just use the $payload variable directly, like so:
$res = $client->post('some.website', [
'form_params' => $payload,
]);
I am making my own array from another one, using email field as key value. If there is more results with same email I am amking array_push to existing key.
I am getting always data in my array (with email) and here is the example
Input data
Example data
$saved_data = [
0 => ['custom_product_email' => 'test#test.com',...],
1 => ['custom_product_email' => 'test#test.com',...],
2 => ['custom_product_email' => 'bla#test.com',...],
3 => ['custom_product_email' => 'bla#test.com',...],
...
];
Code
$data = [];
foreach ($saved_data as $products) {
$curVal = $data[$products->custom_product_email];
if (!isset($curVal)) {
$data[$products->custom_product_email] = [];
}
array_push($data[$products->custom_product_email], $products);
}
Error
I am getting error Undefined index: test#test.com and if I debug my array, there is key with value of 'test#test.com', so key is defined (!)
so var $curVal key is undefined
Result
So the goal of foreach is to filter all objects in array with same email, here is the example:
$data = [
'test#test.com' => [
0 => {data},
1 => {data},
...
],
'bla#test.com' => [
0 => {data},
1 => {data},
...
],
];
this line $curVal = $data[$products->custom_product_email]; is useless and is the one provoking the error: you just initialized $data as an empty array, logically the index is undefined.
You should test directly if (!isset($data[$products->custom_product_email])) {
Then explanation: there is a fundamental difference between retreiving the value of an array's index which is undefined and the same code in an isset. The latter evaluating the existence of a variable, you can put inside something that doesn't exist (like an undefined array index access). But you can't store it in a variable before the test.
Did you not see the error message?
Parse error: syntax error, unexpected '{' in ..... from this code
$saved_data = [
0 => {'custom_product_email' => 'test#test.com',...},
1 => {'custom_product_email' => 'test#test.com',...},
2 => {'custom_product_email' => 'bla#test.com',...},
3 => {'custom_product_email' => 'bla#test.com',...},
...
];
Change the {} to [] to correctly generate the array.
$saved_data = [
0 => ['custom_product_email' => 'test#test.com',...],
1 => ['custom_product_email' => 'test#test.com',...],
2 => ['custom_product_email' => 'bla#test.com',...],
3 => ['custom_product_email' => 'bla#test.com',...],
...
];
Your next issue is in this code
$data = [];
foreach ($saved_data as $products) {
$curVal = $data[$products->custom_product_email];
// ^^^^^
$data is an empty array that you initialised 2 lines above, so it does not contain any keys or data!
Check, if $data[$products->custom_product_email] is already set in $data array
Try This code
$data = [];
foreach ($saved_data as $products) {
$curVal = isset($data[$products->custom_product_email]) ? $data[$products->custom_product_email] : null;
if (!isset($curVal)) {
$data[$products->custom_product_email] = [];
}
array_push($data[$products->custom_product_email], $products);
}
I will need to output an array something along the lines of
[
0 => ['text' => 'category'],
1 => ['text' => category']
]
So basicly an array for each category in my database. I need to output them like this because of how they will be exported to another site. (I cannot foreach inside the export)
My current code, is a foreach loop that runs through my categories. If i var_dump my field variable inside the foreach loop i get the result i want, but as mentioned i need to export everything in the format like above out of the foreach loop.
Code:
foreach ($categories as $category) {
$fieldvalue = ['text' => $category->categoryname];
}
What i have tried:
Putting the array in a string to explode outside the loop-
Result: Because of the "same identifier" issue i could only export the last result
What i need to be done:
My current code outputs the array as a text string so basicly
$fieldvalue ? "['text' => '$category->categoryname']";
And my output will be
array:4 [
0 => "['text' => value1]"
1 => "['text' => value2]"
2 => "['text' => value3]"
3 => "['text' => value4]"
]
I just need to get the string to be an array.
Try:
foreach ($categories as $category) {
$fieldvalue[] = ['text' => $category->categoryname];
}
You want the result to be an array that contains many arrays (multi-dimensional array). Using $var[] = $something adds that $something as an array object.