Error when post data with ajax? - php

When I using ajax post data with struct
And I get data from code here:
$_POST["post"]; => result is 979, that's OK
$_POST["href[href]"]; => result is 0, How to fix it?

Bracket notation is used to create an array entry. Use this instead:
$_POST["href"]["href"];
Calling $_POST["href"] will return an associative array:
array(
'commentID' => 297980913637729,
'href' => 'http://dongcam.vn/t3927'
);

It's a multidimensional array, so:
$_POST["href"]["href"];

Related

Print result is different in the array function

I have a problem with the array PHP function, below is my first sample code:
$country = array(
"Holland" => "David",
"England" => "Holly"
);
print_r ($country);
This is the result Array ( [Holland] => David [England] => Holly )
I want to ask, is possible to make the array data become variables? For second example like below the sample code, I want to store the data in the variable $data the put inside the array.:
$data = '"Holland" => "David","England" => "Holly"';
$country = array($data);
print_r ($country);
But this result is shown me like this: Array ( [0] => "Holland" => "David","England" => "Holly" )
May I know these two conditions why the results are not the same? Actually, I want the two conditions can get the same results, which is Array ( [Holland] => David [England] => Holly ).
Hope someone can guide me on how to solve this problem. Thanks.
You can use the following Code.
<?php
$country = array(
"Holland" => "David",
"England" => "Holly"
);
foreach ($country as $index => $value)
{
$$index = $value;
}
?>
Now, Holland & England are two variables. You can use them using $Holland etc.
A syntax such as $$variable is called Variable Variable. Actually The inner $ resolves the a variable to a string, and the outer one resolves a variable by that string.
So there is this thing called
Destructuring
You can do it something like ["Holland" => $eolland, "England" => $england] = $country;
And now you have your array elements inside the variables.
Go read the article above if you want more information about this because it gets really useful (especially in unit tests usind data provders from my experience).
If you want to extract elements from an associative array such that the keys become variable names and values become the value of that variable you can use extract
extract($country);
To check what changed you can do
print_r(get_defined_vars());
Explanation on why the below does not work
$data = '"Holland" => "David","England" => "Holly"';
When you enclose the above in single quotes ' php would recognise it as a string. And php will parse a string as a string and not as an array.
Do note it is not enough to create a string with the same syntax as the code and expect php to parse it as code. The codes will work if you do this
$data = ["Holland" => "David","England" => "Holly"];
However, now $data itself is an array.
A simple copy of an array can be made by using
$data = $country;

Getting value from a PHP array converted from json

Here is the var_export of $myArray:
Array:array ( '#attributes' =>
array ( 'success' => 'true', ), 'Client' => '1218421234', )
What is the code to get the value of Client into a string?? I tried several constructs but must be having a 'senior' day...
Obviously,
$client = $myArray->Client;
...did not work, neither did...
$client = $myArray->attributes()->Client;
... any help?
It's an array, so you should access to it as:
$myArray['Client']
As $myArray->Client would be if $myArray was an object and Client a property of that object and the same goes for #attributes which is just another array with the key 'success'.
The result you have shown indicates that 'Client' is a String inside an array, so it's $myArray['Client'].
Both your first and second try assumes that $myArray is not an array.

PhP returning Json in []

Hey I am trying to pass data back in json format using json_encode.
However it seems to return like this:
[{‘county’:’us’,’sector’:’retail’}]
However the end user has said that they expect the response without the square brackets.
How do I do this?
Thanks.
Depends on how you are generating the value you encode... json_encode has an option JSON_FORCE_OBJECT you could use but from what I can see that's probably not what you want, as it seems to me this would only turn the "square brackets" into a wrapper object. Nevertheless, if you'd like to try:
json_encode($value, JSON_FORCE_OBJECT);
Edit: as others have said, it seems likely json_encode($value[0]) is what you want.
Assuming your array is:
$data = [
'country' => 'us',
'sector' => 'retail',
];
Then just return a json_encoded array like so:
json_encode($data);
// {"country":"us","sector":"retail"}
However, if your array with data is "nested" like so:
$data = [[
'country' => 'us',
'sector' => 'retail',
]];
then return the encoded first element of it:
json_encode($data[0]);
// {"country":"us","sector":"retail"}
My experience of this happening is that you're accidentally storing your data as the first item of an array.
For example you may have done:
$json_store = Array();
$json_store[] = Array('country' => 'us', 'sector' => 'retail');
$json = json_encode($json_store);
A way of testing for this is to see if doing the following removes the brackets:
$json_encode($json_store[0])
If it does, you've a unnecessarily nested array that you should fix. Using the above line probably should be avoided as it's better to fix how the data is stored in the first place.

How to use json and php

I'm new to JSON, but I have experience in PHP. Can someone explain to me how JSON works, especially with PHP, and EASY way would be nice.
EX: I have a php array like:
array(
array('id' => 1, 'img' => "http.img1.png", 'title' => 'ice cream'),
array('id' => 2, 'img' => "http.img2.png", 'title' => 'silly snail'),
array('id' => 3, 'img' => "http.img3.png", 'title' => 'big bear'),
array('id' => 4, 'img' => "http.img4.png", 'title' => 'Funny cat'),
);
is this fine, or should I alter this array? I want to convert this to a JSON Object. In the php array should there be a parent, and do I have to assign array elements as children, or can each php obj be it's own JSON obj? Thank you!
Just run json_encode on the variable that you want to turn into a json string.
$something = array("test" => array("value", "another value", 4));
echo json_encode($something)
This will produce
{"test":["value","another value",4]}
Also, putting that string into $something = json_decode("{"test":["value","another value",4]}"); will produce back the same array that was passed into json_encode.
Note that JSON is not a programming language; it is a way to represent objects. http://json.org has a complete visual representation of how to use it. JSON's main components are Arrays (surrounded by []) and Objects (surrounded by {}). Arrays are lists of comma separated values (see json.org for how to tell it the types...its pretty simple) while objects are key:value pairs separated by commas between each pair where they key is a string surrounded by quotation marks. Above I created an Object with a key called "test" whose value was an Array with two strings and a number in it.
Use json_encode() for encoding the array, get the array back by using json_decode().

Problem inserting string into array

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).

Categories