So I am writing something to add on to my website.
I have this value stated above:
$settings[] = array(
"name" => "torblock_redirecturl",
"title" => $lang->redirecturl,
"optionscode" => "text",
"disporder" => 1,
"value" => denied.php,
"gid" => $gid
There will be a setting that where someone can enter another url or page. But I have this later in the php file:
die('$torblock_redirecturl');
I want that to change to the value in the setting once the setting is changed.
Aka once a value is entered in the setting, I want the value to change to whatever was entered right in the die.
Thanks for your answers!
You were assigning multidimensional array.
$settings[] = array( "value" => denied.php);
So to get the value access like $settings[0]['value']; the index 0 changes according to number of arrays stored in the $settings
To get expected
die($settings[0]['value']);
Update :
If assigning as single dimensional array.
$settings = array( "value" => denied.php);
Then access it like die($settings['value']);
Try this:
die($settings[0]['name']);
It would be easier to help if you supplied more context, but your syntax error could imply that you wrap an array variable in quotes without escaping, like so:
die('$settings['name']'); // this will break
If you prefer wrapping variables in quotes, you can do it like so:
die("{$settings[0]['name']}");
You may be aware of this, but in your code you're not just assigning an array to the $settings variable:
// this:
$settings[] = array('name' => 'foo');
// is the same as doing this:
$settings = array();
array_push($settings, array('name' => 'foo'));
Related
I'm trying to construct an array where there only strings and the array would look like this
key->key->value
To explain it I attached two screenshots below.
I start with this:
After my code below I'm 90% there, yet there is an array in value on the third level instead of simple value.
Here is some code:
$theme = ThemeHandler::with('sections.settings')->find($activeTheme);
$themeSettings = $theme->sections;
$themeSettings = collect($themeSettings->toArray());
// dd($themeSettings);
$themeSections = [];
foreach ($themeSettings as $key => $value) {
$settings = collect($value['settings']);
$settings = $settings->mapToGroups(function ($item) {
return [$item['key'] => $item['value']];
});
$themeSections[$value['key']] = $settings->toArray();
}
dd($themeSections);
I would like to end up with this structure
key->key->value
and not
key->key->single_element_array->value
I'm not sure how I end up with an array at the bottom level when I do this
return [$item['key'] => $item['value']];
inside the mapToGroups, which is a function found here: https://laravel.com/docs/5.8/collections#method-maptogroups
Maybe I misunderstand how mapToGroups work. Anybody has an idea how to get key->key->value structure output?
Use mapWithKeys() instead of mapToGroups().
You're getting an array instead of the simple value you expect because the value is a group, albeit a group with only one member.
mapToGroups() groups all the values with the same key, but mapWithKeys() will assign a single value to each key.
You can see in the examples in the collection documentation, mapToGroups() produces a result like this:
[
'Sales' => ['John Doe', 'Jane Doe'],
'Marketing' => ['Johnny Doe'],
]
And mapWithKeys() result is like this:
[
'john#example.com' => 'John',
'jane#example.com' => 'Jane',
]
So I am using this code to push new user IDs into an array but I get
Warning: array_push() expects at least 2 parameters, one given
$post_likes = array(
"Some key" => array(
'date' => date("d/m/Y"),
'IP' => get_client_ip())
);
$new_likes = array(
'date' => date("d/m/Y"),
'IP' => get_client_ip());
array_push($post_likes[$current_user->ID] = $new_likes);
The code works. It pushes new key with array value into the previous array. But still I get that warning. What am I missing?
Instead of using array_push() you can directly do like this-
$post_likes[$current_user->ID] = $new_likes;
A sample hardcoded example:- https://eval.in/1000261
Set your new variable $new_likes as like this:
array_push($post_likes[$current_user->ID] = $new_likes,$new_likes); //expects at least 2 parameters
More info : http://php.net/manual/en/function.array-push.php
Updated:
I am proffering and suggesting to use #Magnus Eriksson's answer though you have accepted my answer.
So, just use as like below:
$post_likes[$current_user->ID] = $new_likes;
I wonder if I can select any key inside my array, and set it as the value of another key. in order to be more clear (because my question may not clear enough), I try to do something like this:
$variable = array(
'key' => 'value',
'key2' => $variable['key']
);
As you can see, it won't work (unless I do something like: $variable['key2'] = $variable['key'] out of the array, but this is not what i'm looking for and i'll use it only if I won't be able at all to do it inside the same array).
I searched for any solution but still haven't found one...
There is any way to do such thing inside the same array?
Thank you very much
This way you can`t do it, because this key does not exist yet.
Why to store two same variables in same array? Maybe show us what you are trying to do in bigger picture, so we can help you some way.
Think of it as the code doing what is in the parenthesis FIRST. Since $variable isn't set yet, you'll get an error on $variable['key'], since $variable isn't an array yet.
You must set $variable before
See
$variable = new array();
$variable['key'] = $variable->key2 = 'value';
Also
//create array
$variable = array(
'key' => 'value'
);
//then override
$variable = array(
'key2' => $variable['key'],
'key' => 'new_value'
);
My PHP call is storing a table from mySQL with the following code.
while (mysqli_stmt_fetch($stmtMyCountTotals)) {
$stmtMyCountTotalsrow = array(
'IndividualUnitsCounted' => $IndividualUnitsCounted,
'IndividualUnitsAdjusted' => $IndividualUnitsAdjusted
);
$stmtMyCountTotalsrows[] = $stmtMyCountTotalsrow;
}
$stmtMyCountTotals->close();
I can not seem to pull a individual value from it. For Example If I want to pull a number from column 1 row 2. I know this is a basic question but I can not seem to find an answer.
This is a multidimensional array, so you would access it like so:
$row1col2 = $stmtMyCountTotalsrows[1][2]
$row2col1 = $stmtMyCountTotalsrows[2][1]
But since it is associative you would want:
$var = $stmtMyCountTotalsrows[1]['IndividualUnitsCounted'];
If you want to access it by column, then you would need to retrieve the column names first into an array:
$cols = array_keys($stmtMyCountTotalsrows[0]);
$var = $stmtMyCountTotalsrows[1][$cols[1]]
// ^ where this is the column number
There are no column numbers in your array, the second dimension is an associative array. So it should be:
$stmtMyCountTotalsrows[$row_number]['IndividualUnitsCounted']
$stmtMyCountTotalsrows[$row_number]['IndividualUnitsAdjusted']
You have an array inside another array. Or a multidimensional array. It looks like this:
Array(
0 => Array(
'IndividualUnitsCounted' => 'Something',
'IndividualUnitsAdjusted' => 'Something Else'
),
1 => Array(
'IndividualUnitsCounted' => 'Yet another something',
'IndividualUnitsAdjusted' => 'Final something'
),
...
...
)
If this array is called $stmtMyCountTotalsrows:
$stmtMyCountTotalsrows[0]['IndividualUnitsCounted'] returns "Something"
$stmtMyCountTotalsrows[0]['IndividualUnitsCounted'] returns "Something Else"
$stmtMyCountTotalsrows[1]['IndividualUnitsAdjusted'] returns "Yet another something"
$stmtMyCountTotalsrows[1]['IndividualUnitsAdjusted'] returns "Final something"
If you don't know the name of the columns beforehand, you could use current() for the first:
echo current($stmtMyCountTotalsrows[1]);
Alternatively, use array_slice():
echo current(array_slice(stmtMyCountTotalsrows, 0, 1));
$list_box_contents = array();
$list_box_contents[0] = array('params' => 'class="productListing-odd"');
$list_box_contents[0][] = array('params' => 'class="productListing-data"',
'text' => TEXT_NO_PRODUCTS);
i want to make a condition whether there is a value in $list_box_contents[0][]["text"] .when i write the code if(!empty($list_box_contents[0][]["text"])). my IDE alet me there is an error. what's wrong with it?
[] it's not a position, index or something like this.
With
$list_box_contents[0][] = array('params' => 'class="productListing-data"',
'text' => TEXT_NO_PRODUCTS);
you are pushing the array with its two value (and keys) on the right of the = in the last position of the subarray which has index 0. That is the structure you'll have is:
$list_box_contents[0] => array(VALUES-BEFORE, [last-position-key] => array('params' => 'class="productListing-data"', 'text' => TEXT_NO_PRODUCTS))
Anyway to have an idea of what happens, you can use print_r($list_box_contents) or var_dump($list_box_contents)
after the lines you posted.
When you assign something as in $list_box_contents[0][], you are assigning the contents of whatever follows to the next element in the array $list_box_contents[0]. So in this case, you would be assigning
array('params' => 'class="productListing-data"',
'text' => TEXT_NO_PRODUCTS);
to index 0 of $list_box_contents[0].
You can't refer to it using the $array[] notation, because the PHP parser doesn't have any idea which element in $array that you want to refer to. You need to specify.