PHP multidimentional Array syntax not clear - php

I'm using RIPS PHP scanner. In one of the php files it has a line as bellow:
$F_XSS = array('echo' => array(array(0), $F_SECURING_XSS), ......)
what is the meaning of "array(array(0)" above?

That mean that the array 'echo' will have a value an array with a value (int) 0
PHP
[
'echo' =>
[
[
0
],
$F_SECURING_XSS
],
....
]

Related

json_encode treats a single element array as object [duplicate]

This question already has answers here:
How to add square braces around subarray data inside of a json encoded string?
(3 answers)
Closed 6 months ago.
this is the json i have to produce
{
"email": "example#example.com",
"campaign": {
"campaignId": "p86zQ"
},
"customFieldValues": [
{
"customFieldId": "y8jnp",
"value": ["18-29"]
}
]
}
if i use
$data = [
"email" => $_POST['mail'],
"campaign" => [
"campaignId" => "4JIXJ"
],
"customFieldValues" => [
"customFieldId" => "y8jnp",
"value" => ["18-29"]
]
];
and i do json_encode($data)
value is an object, but it should be an array with a single element. Somehow json_encode treats it as an object. Can i force it to treat it as an array with a single element ?
Thanks in Advance
Adrian
At the moment, you have a single array with 2 elements, instead of an array with a single element of a sub-array. In order to get the json in the first section, you need to add another array level.
$data = [
"email" => $_POST['mail'],
"campaign" => [
"campaignId" => "4JIXJ"
],
"customFieldValues" => [
[
"customFieldId" => "y8jnp",
"value" => ["18-29"]
]
]
];
That will give you this:
{
"email": null,
"campaign": {
"campaignId": "4JIXJ"
},
"customFieldValues": [
{
"customFieldId": "y8jnp",
"value": ["18-29"]
}
]
}
if there is one single element in the array the json_encode will treat is as an object and the key of the object is the index of the array
to treat it as an array you can use array_values(<your_array>)

Laravel - Trying to get property of non-object - PHP

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"];

Find key in multidimensional array and change the value [duplicate]

This question already has answers here:
Get key of multidimensional array?
(5 answers)
Using a string path to set nested array data [duplicate]
(8 answers)
Closed 5 years ago.
I'm writing a PHP script in which I got a multidimensional array without fixed depth. Eg:
$myArray = [
'item01' => [
'section01' => [
'part01'
]
],
'item02' => [
'section02' => [
'part02' => [
'something01'
]
]
],
'item03' => [
'section03'
]
]
I have a string that contains the path to the value that should be changed. Eg:
$myPath = 'item02/section02/part02'
And I have the new value:
$myValue = 'somethingElse'
What I'm trying to do, is go through array $myArray following the path set in $myPath to change the value to $myValue.
The expected output, with the examples above, would be this:
$myArray = [
'item01' => [
'section01' => [
'part01'
]
],
'item02' => [
'section02' => [
'part02' => [
'somethingElse'
]
]
],
'item03' => [
'section03'
]
]
I've tried several ways, but keep getting stumped on this. In the end, it always reaches a point where the solution is, at its core, the same. Rubber duckying didn't help either. Other programmers I know, haven't been able to find a solution either.
I'm hoping someone here can at least provide some fresh ways to look into this.
PS: Everything above is in pseudo-code, as this problem doesn't seem to be language-specific.
try the following:
$myPath = 'item02/section02/part02';
$path = explode('/', $myPath);
$arr = &$myArray;
foreach($path as $key)
{
$arr = &$arr[$key];
}
$arr = "somethingElse";
var_dump($myArray);
First turn the $myPath string into an array of keys using explode.
This uses a simple loop foreach loop to loop over the array using keys, and then by passing the values by reference it replaces the deepest value. Demo Here

Update nested parameter in MongoDB?

I try to update nested parameter in document MongoDb:
$this->collection->update(
["prototype_id" => $id],
["$set" => ["parameters" => $newdata]],
["upsert" => true, "multiple" => true]);
In result I get error:
zero-length keys are not allowed, did you use $ with double quotes?
Where $newdata is:
array:1 [
"5920d086470e6cb30e3c986c" => array:1 [
"Acceleration" => "2"
]
]
Use dot notation to add/overwrite field in embedded document.
Something like
$parameter_id = "5920d086470e6cb30e3c986c";
['$set' => ["parameters.".$parameter_id => ["Acceleration" => "2"]]];
Alternatively, define parameters as embedded array and you can $push to insert whole embedded document.

How can I unset value from an array?

I'm trying to remove one value in array and I'm using Unset function, but when I'm passing array in Unset function I got error.
array:
Array ( [mid] => 8 [optionsRadios2] => 0 [optionsRadios3] => 1 [optionsRadios5] => 0 [optionsRadios6] => 0 [optionsRadios7] => 1 [optionsRadios24] => 0 [optionsRadios25] => 1 )
I want to remove mid and I'm using like this:
$module=$data['mid'];
$newdata=unset($data['mid']);
error:
Parse error: syntax error, unexpected 'unset' (T_UNSET) in C:\xampp\htdocs\Traning\application\controllers\home.php on line 77
Please help.
Use only:
unset($data['mid']);
without assign to variable.
In docs unset() return void.
at the first look you code looks ok?
can you check this example http://sandbox.onlinephpfunctions.com/code/b538f88a23433b767005f5a16ca99faa5eb4f8b0
$koko = [
'mid' => 1,
'name' => 'koko'
];
print_r($koko);
echo '---------------------------';
$toto = $koko['name'];
unset($koko['name']);
print_r($koko);
echo 'and your name is : '.$toto;

Categories