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;
Related
This question already has answers here:
Using an If-else within an array
(10 answers)
Closed 4 years ago.
This is a bit of an odd one. I am building an array in PHP and then encoding it to JSON before I spit it out.
$arr = array (
'reportDescription' =>
array (
if ($queryType == "realtime")
{
'source' => 'realtime',
}
'reportSuiteID' => 'rbsglobretailprod',
'dateGranularity' => $queryGran,
'dateFrom' => $queryFrom,
'dateTo' => $queryTo,
'elements' =>
array (
0 =>
array (
'id' => $queryElement,
),
),
'metrics' =>
array (
0 =>
array (
'id' => $queryMetric,
),
),
),
);
I am trying to get it to add a line to the array if the query type is realtime. This is what I tried but I'm not sure if it's possible to do this, and if it's not, I'm not sure how I should approach it. The error I get below suggests it may not be possible:
Parse error: syntax error, unexpected 'if' (T_IF), expecting ')'
You should do it as two separate calls:
$array = ['your', 'array', 'with', 'fields']
if ($queryType === 'realtime') {
$array[] = ['source' => 'realtime'];
}
Obviously change out the values with your expected values, but that should do the job. You could also append like so if you wanted it at root level:
if ($queryType === 'realtime') {
$array['source'] = 'realtime';
}
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);
}
In my controller I have the following lines
$request = Yii::$app->request;
print_r($request->post());
echo "version_no is ".$request->post('version_no',-1);
The output is given below
Array
(
[_csrf] => WnB6REZ6cTAQHD0gAkoQaSsXVxB1Kh5CbAYPDS0wOGodSRANKBImVw==
[CreateCourseModel] => Array
(
[course_name] => test
[course_description] => kjhjk
[course_featured_image] =>
[course_type] => 1
[course_price] => 100
[is_version] => 1
[parent_course] => test
[version_no] => 1
[parent_course_id] => 3
[course_tags] => sdsdf
)
)
version_no is -1
So here the return value of post() contains the version_no.But when it is called as $request->post("version_no"), it is not returning anything (or $request->post("version_no",-1) returns the default value -1).
As per Yii 2.0 docs, the syntax is correct and should return the value of post parameter.
But why is it failing in my case.The post array has the parameter in it.But the function is not returning when called for an individual parameter value.
your parameters are in $_POST['CreateCourseModel']['version_no'] etc. with $request->post('version_no',-1) you trying to get $_POST['version_no'] which is not defined so it returns you -1. So to get version_no use
$data = $request->post('CreateCourseModel');
print_r($data['version_no']);
You can access nested $_POST array elements using dot notation:
\Yii::$app->request->post('CreateCourseModel.version_no', -1);
Model properties are grouped like that for massive assignment that is done via $model->load(Yii::$app->request->post()).
Depending on your needs maybe it's better use default value validator like that:
['version_no', 'default', 'value' => -1],
I have an array like:
$profile_typeid [] = custom_profile(
0 => "44258",
1 => "44259",
);
and another array $meta_data[], I want to push this array $profile_typeid []. Before that it should check in $meta_data[] whether it contains $profile_typeid [] or not. If not then add other wise it should overwrite.
How can I get the things in PHP
I tried like:
if(!in_array($meta_data,$profile_typeid,true)){
array_push($meta_data, $profile_typeid);
}
I have var_dump for two arrays like
`array
'custom_profile_type' =>
array
0 => string '39242' (length=5)
null`
Change:
in_array($meta_data,$profile_typeid, true)
to
in_array($profile_typeid,$meta_data)
In your syntax you are searching for the haystack in the needle. (so to speak)
http://php.net/manual/en/function.in-array.php
Update:
To add in array use array_push.
if(!in_array($profile_typeid,$meta_data)){
array_push($meta_data, $profile_typeid);
}
else{
$meta_data['custom_profile_type'] = $profile_typeid;
}
Remove the strict clause. I do not think it is needed in this context.
This should work.
You can do it using below given code ...
$profile_typeid = array(
0 => "44258",
1 => "44259",
);
$meta_data = array(
0 => "34567",
1 => "67890",
2 => "44258"
);
foreach($profile_typeid as $pro_type){
if(!in_array($pro_type,$meta_data)){
array_push($meta_data, $pro_type);
}
}
print_r($meta_data);
The in_array(array(), $array, true); // false you are using which is totally wrong please check the manual for more details http://php.net/manual/en/function.in-array.php
Good luck with that ..
I would like to add an array to within an existing array.
I am tryin to use array_push which works as long as i dont try to assign a key to the array (if i try to add a key i get a syntax error... :-()
This is my initial array:
$ResultArray = array(
"TransactionDate" => "$TransactionDate",
"tx"=>array(
"0"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" => "$PaymentConfirmedCount"
),
"1"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" => "$PaymentConfirmedCount"
)
)
);
i would then like to add:
$ArrayTOAdd = array(
"0"=>array(
"TxIndex" => "$TxIndex",
"value" => "$Value",
"PaymentConfirmedCount" =>
"$PaymentConfirmedCount"
)
);
if I try:
array_push($ResultArray->tx, $ArrayTOAdd);
BUT this does not work and results in a warning of "array_push() [function.array-push]: First argument should be an array"
if i try this :
array_push($ResultArray, $ArrayTOAdd);
it just adds the array but not to $ResultArray->tx
Any suggestions would be greatly welcomed!
You have to access the element in the array with $ResultArray["tx"] and not $ResultArray->tx. The second one is for the access to members in a php class. So an
array_push($ResultArray["tx"], $ArrayTOAdd);
should work.