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);
Related
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']
];
echo $a['b']['b2'];
What does the value in the brackets refer to? Thanks.
This is an array.
what you are seeing is
<?php
$a = array(
'b' => array(
'b2' => 'x'
)
);
So in this case, $a['b']['b2'] will have a value of 'x'.
This is just my example though, there could be more arrays in the tree. Refer to the PHP Manual
Those are keys of a multidimensional array.
It may refer to this array:
$a = array(
"a" => array(
"a1" => "foo",
"a2" => "bar"
),
"b" => array(
"b1" => "baz",
"b2" => "bin"
)
)
In this case, $a['b']['b2'] would refer to 'bin'
This refers to a two dimensional array, and the value inside the bracket shows the key of the array
That means the variable $a holds an array. The values inside of the brackets refer the array keys.
$a = array('b' => 'somevalue', 'b2' => 'somevalue2');
In this case echo'ing $a['b'] would output it's value of 'somevalue' and $a['b2'] would output it's value of 'somevalue2'.
In your example, it's refering to a multi-dimensional array (an array inside of an array)
$a = array('b' => array('b2' => 'b2 value'));
where calling b2 would output 'b2 value'
Sorry if my answer is too simplistic, not sure your level of knowledge :)
$a is an array, a list of items. Most programming languages allow you to access items in the array using a number, but PHP also allows you to access them by a string, like 'b' or 'b2'.
Additionally, you have a two-dimensional array there - an array of arrays. So in that example, you are printing out the 'b2' element of the 'b' element in the $a array.
I have json_encode($array) it gives me a list in a different order on google chrome
array_reverse: used to Return an array with elements in reverse order, e.g:
$array = array_reverse($array);
echo json_encode($array,JSON_UNESCAPED_UNICODE);
The encoding is done on the backend by the PHP so Google Chrome has nothing to do with this issue.
Check your array order before you encode it.
check the value in $array using var_dump to make sure, thats the order you want.
Edit: look at the 3rd example in the manual
What does your JSON data look like? If:
{1,2,3}
Your browser will not necessarily preserve the order. But if formatted as a JSON array:
[1,2,3]
Then the order will be preserved.
If your array is associative, but indexed by integers (like $arr1 = ['3' => 'x', '2' => 'a', '1' => 'b'];), the browser (when parsing JSON) assumes that the keys of the array indicate the order in which the values are stored (as in a classical, non-associative array - like $arr2 = ['x','a','b'];).
See for yourself - compare the results of parsing the JSON generated from those two associative arrays - one is indexed by non-integer strings (A), the other one is indexed by integers (even if they are string-typed in PHP) (B).
For both examples the values in PHP are stored in order: 'x', 'a', 'b', and only the keys are different.
A) associative array, indexed by strings
<?php
$arr1 = [
'foo' => 'x',
'bar' => 'a',
'baz' => 'b'
];
$json = json_encode($arr1); // $json is now a string: '{"foo":"x","bar":"a","baz":"b"}'
And then, in the browser:
var jsonData = JSON.parse('{"foo":"x","bar":"a","baz":"b"}');
console.log(jsonData);
// prints {foo: "x", bar: "a", baz: "b"} - the keys are in different order then expected!
B) associative array, indexed by integers (even if they are strings in PHP!)
<?php
$arr1 = [
'3' => 'x',
'2' => 'a',
'1' => 'b'
];
$json = json_encode($arr1); // $json is now a string: '"{"3":"x","2":"a","1":"b"}"'
And then, in the browser:
var jsonData = JSON.parse('"{"3":"x","2":"a","1":"b"}"');
console.log(jsonData);
// prints {1: "b", 2: "a", 3: "x"} - the keys are in different order then expected!
As you see - the JSON parser assumes a classical array when the
indices are integers.
So if you need to maintain the order, I'd suggest switching to a classical array and perhaps adding an order / key field to the JSON (for sorting purposes - if you need it):
$arr1 = [
'3' => 'x',
'2' => 'a',
'1' => 'b'
];
$json = json_encode(array_values($arr1)); // note the array_values() here - but this way you loose the index keys
Another way:
$arr1 = [
'3' => [ 'key' => '3', 'value' => 'x'],
'2' => [ 'key' => '2', 'value' => 'a'],
'1' => [ 'key' => '1', 'value' => 'b'],
];
$json = json_encode(array_values($arr1));
// this way you have both values and keys,
// and the parsed JSON will be in the exact order you want it to be
$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.
It is possible to put an array into a multi dim array? I have a list of user settings that I want to return in a JSON array and also have another array stored in that JSON array...what is the best way to do that if it isn't possible?
A multi dimension is already an array inside an array. So there's nothing stopping you from putting another array in there. Sort of like dreams within dreams :P
Just use associative arrays if you want to give your array meaning
array(
'SETTINGS' => array(
'arr1' => array( 0, 1),
'arr2' => array( 0, 1)
),
'DATA' => array(
'arr1' => array( 0, 1),
'arr2' => array( 0, 1)
)
)
EDIT
To answer your comment, $output_files[$file_id]['shared_with'] = $shared_info; translates to (your comment had an extra ] which I removed)
$shared_info = array(1, 2, 3);
$file_id = 3;
$output_files = array(
'3' => array(
'shared_with' => array() //this is where $shared_info will get assigned
)
);
//you don't actually have to declare it an empty array. I just did it to demonstrate.
$output_files[$file_id]['shared_with'] = $shared_info; // now that empty array is replaced.
any array key can have an array value in php, as well as in json.
php:
'key' => array(...)
json:
"key" : [...]
note: php doesn't support multidimensional arrays as in C or C++. it's just an array element containing another array.