Here is my array
$datas = array(array('studentid' => '9','toschool' => '4','tohome'=>'4'),array('studentid' => '10','toschool' => '4','tohome'=>'4'));
When i return this i am getting as
return $data;
Output :
[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]
I want to get this same array from the postman
So, i pasted the ouput for the name as data
And i receive it as
$gotdata = Input::get('data');
and when i print i got the same output
[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]
When i tried to save the record, the $data works
MYModel::insert($data);
But the
MTIServiceAttendance::insert($gotdata);
And it throws the error as
Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, string given
How can i fix this so that the $gotdata should be saved.
Note : for the Model the input should be
MTIServiceAttendance::insert(array(array('studentid' => '9','toschool' => '4','tohome'=>'4'),array('studentid' => '10','toschool' => '4','tohome'=>'4')));
What should i do to make the input form like this array ?
Update : Here is the var_dump of the arrays
Return :
return var_dump($data);
Output :
array(2) { [0]=> array(3) { ["studentid"]=> string(1) "9" ["toschool"]=> string(1) "4" ["tohome"]=> string(1) "4" } [1]=> array(3) { ["studentid"]=> string(2) "10" ["toschool"]=> string(1) "4" ["tohome"]=> string(1) "4" } }
Return :
return var_dump($gotdata);
Output :
string(94) "[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]"
$gotdata is a JSON string representation of your array. So while it looks the same in your first output you can clearly see the difference when using var_dump. Simply use json_decode to convert it in an array:
$gotdata = Input::get('data');
$gotdata = json_decode($gotdata, true);
MTIServiceAttendance::insert($gotdata);
Related
So this below is the structure of JSON i have when I decode it in PHP, but for some reason I am having hard time to loop through this JSON object. I don't know how can I get each values of "incident","description","technique" from those array to save them In my DB.
array(1) {
["Access"]=>
array(2) {
[0]=>
array(3) {
["incident"]=>
string(19) "sssssssssssssssssss"
["description"]=>
string(10) "ssssssssss"
["technique"]=>
string(19) "Link "
}
[1]=>
array(3) {
["incident"]=>
string(18) "ssssssssssssssssss"
["description"]=>
string(0) ""
["technique"]=>
string(19) "Link "
}
}
}
So far I have this PHP code but it's returning me an error saying invalid argument in first foreach loop.
$objectFirst =($_POST['Access1']);
$data = json_decode($objectFirst,true);
foreach ($data->Access as $tech){
foreach($tech as $incident){
foreach($incident as $ss){
var_dump($ss->incident);
}
}
}
When you access the element with this notation, $data->Access, it means you try to access a property of the $data object. But in your case, $data is an array, therefore you have to use the array notation.
So it should be corrected as $data['Access']. One other issue in your code is the level of loops.
foreach ($data->Access as $tech){
foreach($tech as $incident){
foreach($incident as $ss){
var_dump($ss->incident);
}
}
}
The inner most loop is incorrect because $incident will contain a string, not an array. When you try to access $ss['incident'], it will fail. So just change it to:
foreach ($data['Access'] as $tech){
foreach($tech as $incident){
var_dump($incident);
}
}
Hope it helps!
<?php
$data = [
'access' =>
[
[
'foo' => 'I',
'bar' => 'got'
],
[
'foo' => 'a',
'bar' => 'big'
]
]
];
foreach($data['access'] as $array)
var_dump($array['foo'], $array['bar']);
Output:
string(1) "I"
string(3) "got"
string(1) "a"
string(3) "big"
var_dump($response);
outputs:
array(1) { ["metafields"]=> array(1) { [13]=> array(10) { ["id"]=> int(32616923206) ["namespace"]=> string(7) "ly26638" } } }
array(1) { ["metafields"]=> array(1) { [13]=> array(10) { ["id"]=> int(32641864774) ["namespace"]=> string(7) "ly26638" } } }
how can I convert $response to an object to operate it like in the following code:
echo $response->metafields[0]->id;
I've tried the code below, but without any result :/
$object = json_decode(json_encode($response));
If you want to access your "metafields's id" like $response->metafields[13][id] then you need to cast your response array to object.
Ex.
$response = (object) array(
'metafields' => array(
'13' => (object) array(
'id' => 32616923206,
'namespace' => "ly26638"
)
)
);
Then you can use syntax like $response->metafields[13]->id to access the "id" value.
If you make an object with (object) $response, the child elements will still be arrays. Then you could do
$response->metafields[13][id]
If you want the object notation through the whole chain you need something like the function sandip has provided.
But why not just make this call:
$response[metafields][13][id]
You can't find the member of an object by '[]'.
The '[]' is for arrays, and the '->' is for objects.
So the reason for the blank output is: metafields[0].
The code below is wrong:
echo $response->metafields[0]->id;
So you should change the key of the array, the key of an array should not be a pure number.
I tried to change the key to 'a13', and with the code of:
echo $response->metafields->a13->id;
I got "32616923206".
That's all. O(∩_∩)O~
array(24) { ["user_id"]=> string(1) "9" ["facebook_id"]=> string(15) "381305418721463" ["first_name"]=> string(4) "John" ["last_name"]=> string(4) "Does" ["current_latitude"]=> string(10) "-37.825697" ["current_longitude"]=> string(10) "144.999965" ["current_address"]=> string(45) "229 Swan Street, Richmond VIC 3121, Australia" ["date_of_birth"]=> string(10) "01/01/1990" ["city"]=> string(30) "Melbourne, Victoria, Australia" ["country"]=> string(0) "" ["email_address"]=> string(22) "bzingatester#gmail.com" ["profile_pic"]=> string(0) "" ["first_login"]=> string(2) "no" ["blocked_users_id"]=> string(0) "" ["my_friend"]=> string(52) "10152805813948795,10155307822515151,1389504958030240" ["search_radius"]=> string(2) "50" ["device_type"]=> string(3) "ios" ["device_id"]=> string(1) "1" ["device_token"]=> string(64) "6ddaf9d59418e99b1c9cb28c21d94647bfed9f78a80b410164c1f2798beee84a" ["hideFromActivityFeed"]=> string(2) "no" ["hideFromFriendsofFriends"]=> string(2) "no" ["hideNotification"]=> string(2) "no" ["created_date"]=> string(19) "2015-01-07 01:00:11" ["modified_date"]=> string(19) "2015-02-27 05:36:12" }
In PHP I have an array called $userData as per above, I want to be able to echo values such as first name 'first_name', using code like this
echo $userInfo->first_name;
(this doesnt seem to work)
I DO NOT want to loop and fetch all keys in array using foreach, just get values 'first_name', 'last_name' ect. from the array
Please try like this,
$first_name = $userInfo['first_name'] // $userInfo is array name
You can change the array to object and then use -> like below
$userData= (object) $userData;
And then
$userData->firstName ..etc
Please use type casting.
$userinfo = (array)$userInfo;
Then you access your key first_name.
echo $userinfo['first_name'];
First of use a different method to dump data ..
echo "<pre>";
print_r($your_array);
exit;
It just simply bodes for a better presentation to see what is an object and what is an array.
You cannot access array members with the object notation. You can however cast your array as an object.
(object)$yourarray;
If you want to access array members, use :
$yourarray['first_name'];
If you cast as an object:
$obj = (object)$yourarray;
Now access:
$obj->firstname;
Hope that helps.
Actually we use arrays in programming to prevent loop or something like this to find a value, so instead of this structure,
$a = 'text1';
$b = 0;
$c = true;
$d = 'text2';
we use arrays:
$array = array('a' = > 'text1', 'b' => 0, 'c' => true, 'd' => 'text2' );
and to access the value of some key, we call the key name, inside brackets after array's name:
echo $array['a'];
//will echo text1
//or
echo $array['b'];
//will echo true
This is associative array...
if you just pass values to an array without keys, php will use numeric keys instead...
$array = array( 'text1', '0', true, 'text2' );
$array[0] = 'text1';
$array[1] = 0;
$array[2] = true;
$array[3] = 'text2';
I'm trying to print an ID from MySQL, the field loads into an array and is visible via print_r but I can't echo it or transfer it to another variable ... what am I missing?
if ( $_POST['section'] == "freelance" ) {
$field_name = "promoter";
} else {
$field_name = "connector";
}
echo $row[$field_name.'_login_ID']
As requested the results of var_dump($row)
array(13) {
["connector_login_id"] => string(2) "14"
["connector_type"] => string(10) "non-profit"
["unique_code"] => string(9) "test-t001"
["update_code"] => string(1) "N"
["md5ID"] => string(0) ""
["username"] => string(6) "bugger"
["connectorEmail"] => string(17) "gzigner#gmail.com"
["password"] => string(32) "098f6bcd4621d373cade4e832627b4f6"
["connectorPass"] => string(4) "test"
["active"] => string(1) "Y"
["modified"] => string(19) "2009-08-21 15:37:22"
["lastlogin"] => string(19) "0000-00-00 00:00:00"
["md5email" ]=> string(32) "051cba58da33fac6b2d18af5182079f4"
}
$row[$field_name.'_login_ID'] <-- "ID"
array(13) {
["connector_login_id"] <-- "id"
Seems like a simple typo to me.
Alternatively, are you sure $field_name gets set to 'connector', since 'promoter_login_id' doesn't exist in this array.
This is purely speculation without your code, but it's probable that the field you are trying to echo contains a hyphen, e.g. "mytable-id", considering that it does indeed show when you use print_r() to print out the entire array. If this is the case you would need to use {'mytable-id'} to get/echo it's value:
echo($dataArray->MyTable->{'mytable-id'});
*Edit: I don't know if your code is copy and pasted, but the value you are trying to print is:
echo $row[$field_name.'_login_ID'];
instead of:
echo $row[$field_name.'_login_id'];
PHP is case-sensitive. You could also try this:
$field_name = $field_name.'_login_id';
echo $row[$field_name];
or
$field_name = $field_name.'_login_id';
echo $row['$field_name'];
I am trying to json_encode an array which is returned from a Zend_DB query.
var_dump gives: (Manually adding 0 member does not change the picture.)
array(3) {
[1]=>
array(3) {
["comment_id"]=>
string(1) "1"
["erasable"]=>
string(1) "1"
["comment"]=>
string(6) "test 1"
}
[2]=>
array(3) {
["comment_id"]=>
string(1) "2"
["erasable"]=>
string(1) "1"
["comment"]=>
string(6) "test 1"
}
[3]=>
array(3) {
["comment_id"]=>
string(1) "3"
["erasable"]=>
string(1) "1"
["comment"]=>
string(6) "jhghjg"
}
}
The encoded string looks like:
{"1":{"comment_id":"1","erasable":"1","comment":"test 1"},
"2":{"comment_id":"2","erasable":"1","comment":"test 1"},
"3":{"comment_id":"3","erasable":"1","comment":"jhghjg"}}
What I need is:
[{"comment_id":"1","erasable":"1","comment":"test 1"},
{"comment_id":"2","erasable":"1","comment":"test 1"},
{"comment_id":"3","erasable":"1","comment":"jhghjg"}]
Which is what the php.ini/json_encode documentation says it should look like.
How are you setting up your initial array?
If you set it up like:
array(
"1" => array(...),
"2" => array(...),
);
then you don't have an array with numeric indexes but strings, and that's converted to an object in JS world. This can happen also if you don't set a strict order (i.e. starting at 0 instead of 1).
This is a shot in the dark, however, because I can't see your original code: try setting your array without using keys at all in the first place:
array(
array(...),
array(...),
);
Added information that expands on Seb's answer.
php > print json_encode( array( 'a', 'b', 'c' ) ) ;
["a","b","c"]
php > print json_encode( array( 0 => 'a', 1 => 'b', 2 => 'c' ) ) ;
["a","b","c"]
php > print json_encode( array( 1 => 'a', 2 => 'b', 3 => 'c' ) ) ;
{"1":"a","2":"b","3":"c"}
php >
Note: its formatting it this way with good cause:
If you were to send
{"1":"a","2":"b","3":"c"}
as
["a","b","c"]
When you did $data[1] in Php you would get back "a", but on the JavaScript side, you would get back "b" .
A common way to test for a traditional, continuous array in php is to check for an index '0'. Try adding that to your array, it'll probably considering it an array instead of hashmap.
i had a similar problem, got it to work after adding '' (single quotes) around the json_encode string.
Following from my js file:
var myJsVar = <?php echo json_encode($var); ?> ; -------> NOT WORKING
var myJsVar = '<?php echo json_encode($var); ?>' ; -------> WORKING