I have to pass a query parameters with different values but absolutely equal keys. I found this \GuzzleHttp\Psr7\build_query(); But it returns only last value. For instance:
$array = [
'test' => '1',
'test' => '2'
]
\GuzzleHttp\Psr7\build_query($array);
//OR
http_query_builder($array);
It returns every time string with the last item.
Does it possible to do that with PHP? Because the side which will take these parameters just can not do anything in their side so I have to pass parameters with the equal keys.
The problem was not with the specific method used, but with how you filled your array to begin with:
$array = [
'test' => '1',
'test' => '2'
]
You can not use the same array key twice; your array only contains one element now, because the second one has overwritten the existing first one under that same key.
Make the test element itself an array, that contains your two values:
$array = [
'test' => ['1', '2']
];
Related
I want to check if all the values of my input, an array, are in another array. For example:
$my_arr = ['item1', 'item2', 'item3', 'item4'];
Validator::make($request, [
'item' => [ /* what do i put here??? */ ],
]);
I don't think the rule in works, because that expects a single value as input, not an array. in_array doesn't work either. I've tried creating a custom rule or closure, but neither of those allow me to pass in a parameter (the array I want to check against). Especially since I want this same functionality for multiple arrays, for different inputs, it would be nice to have a generic rule that works for any array I give it.
If that's not possible, I suppose I need to create a rule for each specific array and use !array_diff($search_this, $all) as this answer says. Is there an alternative to this?
True that in doesn't accept an array but a string. So you can just convert the array into a string.
$my_arr = ['item1', 'item2', 'item3', 'item4'];
Validator::make($request, [
'item' => [ 'in:' . implode(',', $my_arr) ],
]);
implode
Another better solution might be to use Illuminate\Validation\Rule's in method that accepts an array:
'item' => [ Rule::in($my_arr) ],
Laravel Validation - Rule::in
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',
]
Is it possible to use php variable as a part of an array name. like this
my arrays are Fday1, Fday2....but i cant go with the foreach keys beacause there are different counts of values for each array
$FdayArray = "Fday".$FdayKey;
array_push($FdayArray, $forecast);
the FdayKey would be a number between 1-9
How do I do this correctly?
You are looking for the variable variables feature of php.
Sometimes it is convenient to be able to have variable variable names.
That is, a variable name which can be set and used dynamically. It
takes the value of a variable and treats that as the name of a
variable
You would use
array_push($$FdayArray, $forecast);
Instead of maintaining different array for each $FdayKey, you can have an associative array, which has internal arrays corresponds to each $FdayKey
The array can look like:
$FdayArray = [
'1' => [],
'2' => [],
'3' => [],
'4' => [],
'5' => [],
'6' => [],
'7' => [],
'8' => [],
'9' => []
];
When you need to push to the array, just use the $FdayKey as a index to get the relevant array.
So you can push as:
array_push($FdayArray[$FdayKey], $forecast);
I have a routine in my code that computes the differences between two arrays in order create an SQL UPDATE statement.
As soon the routine starts I create a copy of the input array in order to manipulate it while the input one is kept untouched. When I'm ready to create the UPDATE statement I compute the difference between them, using the modified array as leading one.
In every test I ran both arrays were filled with key=value pairs and all values were strings, even when they're integers in the database (PDO behavior) and until now everything worked flawlessly.
But right now I've found something strange. Two different actions, in two different Controllers, but with the same logic are producing different differences:
This one works as expected:
$input = array(
'tid' => '3',
'enabled' => '1'
);
$modified = array(
'tid' => '3',
'enabled' => 0,
'modified' => '2014-11-26 15:17:55'
};
$diff = array(
'enabled' => 0,
'modified' => '2014-11-26 15:17:55'
);
$input is the result of a query. $modified is a copy of the first array manipulated through class methods. When I'm ready to create the statement, $diff is computed in order to send to PDO (or other driver) the correct statement.
Here, array_diff() works. the tid index is present in both array and it's ignored. The enabled, a simple on/off flag, is different and it's included. The datetime representation too.
But look the variables of this other case:
$input2 = array(
'sid' => '1',
'finished' => '0'
);
$modified2 = array(
'sid' => '1',
'finished' => 1,
'modified' => '2014-11-26 15:21:58'
);
$diff2 = array(
'modified' => '2014-11-26 15:21:58'
);
The same as before but with different field names. The sid is ignored but the finished is ignored too while it shouldn't because it is not present in the $input.
By replacing array_diff() with array_diff_assoc(), as expected, everything works, but why?
From the docs:
array array_diff ( array $array1 , array $array2 [, array $... ] )
Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.
In your example, $modified2 has an entry 'finished' which has value 1. But $input2 also has value 1 for key 'sid'. Thus, using array_diff($modified2, $input2) will result in the removal of every entry with value 1, no matter what the key is.
Using array_diff_assoc, it will only check to see if $input2 has the entry 'finished' => 1, which it does not, so the entry will not be removed.
$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.