I have a weird problem here. I'm using an associative array in php (using cakePHP) which has the following form:
$my_array = array(
'data['a']['b'] => 'value1',
'data['b']['c'] => 'value2',
'data['b']['d'] => 'value3',
'data['e'] => array(
'data['e1']['e2']' => 'value3',
'data['e1']['e3']' => 'value4'));
The problem I'm having is that
'data['e1']['e2']' => 'value3' and 'data['e1']['e3']' => 'value4'
are taken like an array like this:
'data['e1']' => array(
['e2'] => 'value3',
['e3'] => 'value4');
I don't want these to be taken as arrays, I want them to be taken as key and value of the array 'data['e']'. As a matter of fact, I want all the elements of the arrays $my_array and 'data['e']' to be taken as keys and values of the corresponding array (not as arrays).
Any help please?
P.S This seems to happen only when I do a debug on cakePHP, if I don't use cakePHP everything seems to be fine and "data" comes from a cURL posted data to cakePHP
Your code is invalid PHP. My best guess is that it should look like this:
$my_array = array(
$data['a']['b'] => 'value1',
$data['b']['c'] => 'value2',
$data['b']['d'] => 'value3',
$data['e'] => array(
$data['e1']['e2'] => 'value3',
$data['e1']['e3'] => 'value4'));
Please show us the contents (for instance, using print_r) of $data.
POSTed data in a certain syntax is automatically parsed into $_POST as array. If you want to get the raw input, use file_get_contents('php://input'). See http://php.net/manual/en/wrappers.php.php.
Related
Hope everything is good.
I use php file() and it works very well. I only need to separate my "values" in the txt-file with a new row, and then 'file()' will give me the contents of the txt-file as an array with all the values separately.
I do not know if I can take the same function one step further to achieve 'key/values' and 'multidimensional arrays'. If not, what do I have for other options to be able to save 'text data' in a txt file and then get it back in a multidimensional array?
At the moment, I only get the following:
[0] => 'value1',
[1] => 'value2',
[2] => 'value3',
If you know any solutions that are very straightforward and can put me on the right track here, I am very grateful.
A simple primer for doing this with JSON:
// example array of data
$myarray = array();
$myarray[] = ['name' => 'value 1','age' => '31','city' => 'nowhere'];
$myarray[] = ['name' => 'value 2','age' => '12','city' => 'somewhere'];
$myarray[] = ['name' => 'value 3','age' => '67','city' => 'anywhere'];
print_r($myarray);
// to save your array to a file
file_put_contents('/path/to/file.json',json_encode($myarray));
// now to retrieve:
$myarray = json_decode(file_get_contents('/path/to/file.json'),true);
print_r($myarray);
By doing it this way, you retain those keys you want, and the values. The JSON encoding/decoding handles all the painful bits of storing it as text in a flatfile.
How to configure PhpStorm's PHP coding style settings so this code:
<?php
$array = [
'element1' => 'value1',
'element2' => 'value2',
'element3' => 'value3',
];
Is reformatted like this:
<?php
$array = [
'element1' => 'value1',
'element2' => 'value2',
'element3' => 'value3',
];
Problem is that even if I format arrays like I want, any call to reformat option will "destroy" that formatting and bring me back to unindented elements.
I am interested in short array declaration only.
Based on your current code style: please change Continuation indent from 0 to the desired value (I guess it will be 4, based on your code sample).
This can be done for all languages at once (General node) or just PHP.
I have an array like this:
$a = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => array(
'key4' => 'value4',
'key5' => array(
'key6' => 'value6'
)
)
);
as you can see there are inner arrays inside $a
Now, I have a list of keys, example:
key1
key4
key6
I need a script that search if those key exists, and if exists change their values.
I Need to change their values with base64_encode($value_of_the_key)
so Maybe a callback that get the current value and convert it using base64_encode() function.
COuld someone help me?
I'm tring to see the current php functions but it seems there is not ones that do this thing.
THanks
EDIT:
Using the follow code i can get the keys in the callback....but the problem is:
How can i modify the values directly in the array? I Mean.... ok ... i get key and value, but how to change the value in the original array? ($a)
$a = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => array(
'key4' => 'value4',
'key5' => array(
'key6' => 'value6'
)
)
);
function test($item, $key)
{
echo "$key. $item<br />\n";
}
array_walk_recursive($a, 'test');
array_walk_recursive() with callback supplied should help. More info here.
I have an array:
array(
'myVar1' => 'value1',
'myVar2' => 'value1',
'myVar3' => 'value3',
);
Is there a built in function in PHP that will make 3 variables e.g. $myVar1, $myVar2, $myVar3 do that when i echo $myVar1; it retuens 'value1'
Obviously I can loop the array and set them accordingly (so please no answers with this), but if there is a internal PHP function that would be great!
extract() is the function:
Import variables into the current symbol table from an array...
Checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table...
$array = array(
'myVar1' => 'value1',
'myVar2' => 'value1',
'myVar3' => 'value3',
);
echo $array['myVar1'];
echo $array['myVar2'];
Is that what you mean?
i'm trying to insert an implode generated string to an array that then later be used for json implementation
the implode generated string is look like this
'id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]
i would like to used it in this code
$this->_JsonArr[]=array($Generated string);
to achieve something like this
$this->_JsonArr[]=array('id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]);
instead i got something like this
$this->_JsonArr[]=array(" 'id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]");
seem like generated string is treated as one element as key and value pair.
obviously i can get expected output from mysql because of this, can anybody help me with this
Why do you need to implode anything? Just pass the array:
$this->_JsonArr[] = your-non-imploded-array-here;
I think a full solution to what you want to do is something like this (i.e., the third code box in your question):
$row = array(
'id' => $this->_SqlResult[0],
'UserId' => $this->_SqlResult[1],
'Msg' => $this->_SqlResult[2],
'MsgStamp' => $this->_SqlResult[3]
);
$this->_JsonArr[] = $row;
$this->_JsonArr[]=array($Generated
string);
Looks like you want use arrays keys and values, but as I see you put into array plain string with expectation that array parse your plain string in format: keys => values.
You can try create array like below:
$this->_JsonArr[ $Generated_key ] = array( $Generated_value );
(Please correct me if I wrong understand your question).