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'];
Related
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';
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);
I'm using a Gravity Forms to create a form but want to do something extra with the data after submission of the form.
I'm using var_dump($_POST); to see what the resulting information that is sent is and I get this:
array(12) { ["input_1"]=> string(8) "John Doe" ["input_2"]=> string(11) "Some School" ["input_3"]=> string(8) "Any City" ["input_4"]=> string(7) "Alabama" ["input_5"]=> string(3) "456" ["is_submit_18"]=> string(1) "1" ["gform_submit"]=> string(2) "18" ["gform_unique_id"]=> string(0) "" ["state_18"]=> string(60) "WyJhOjA6e30iLCI5ZTU3ZGE4Mjk1MjFkYjg3MzRlNGQ5MzZjN2E5OWU1MiJd" ["gform_target_page_number_18"]=> string(1) "0" ["gform_source_page_number_18"]=> string(1) "1" ["gform_field_values"]=> string(0) "" }
I'm not familiar with this, yet (I learn so much from all of your help) how would I use, for example, the result from ["input_5"]?
Thank you in advance.
Do you mean this? echo $_POST['input_5'];
$_POST is an associate array, meaning that it is a container for multiple values (array) with named keys (associative). A numeric array doesn't have key names, and looks up values by index instead.
Associate Array - use this to store data that intuitively should be named and order does not matter
$myArr = [
'Name' => 'John Doe',
'Address' => '000 Some St.',
'Phone' => '000-0000'
];
$myArr['Address'];
//or
$key = 'Address';
$myArr[$key]; //use a variable as a key
Numeric Array - use this to store the same type of data where order matters
$myArr = [a,b,c,d,e,f,g];
$toDo = [
'Go to the store',
'Buy Eggs',
'Make Breakfast',
'Be Happy'
];
$myArr[0] //"a"
$toDo[1] //"Buy Eggs"
I'm trying to parse an array that looks like this:
array(1) {
["StrategischeDoelstellingenPerDepartement"] => array(412) {
[0] => array(5) {
["CodeDepartement"] => string(8) "DEPBRAND"
["NummerHoofdstrategischeDoelstelling"] => string(1) "1"
["Nummer"] => string(2) "27"
["Titel"] => string(22) "DSD 01 - HULPVERLENING"
["IdBudgetronde"] => string(1) "2"
}
[1] => array(5) {
["CodeDepartement"] => string(8) "DEPBRAND"
["NummerHoofdstrategischeDoelstelling"] => string(1) "2"
["Nummer"] => string(2) "28"
["Titel"] => string(24) "DSD 02 - Dienstverlening"
["IdBudgetronde"] => string(1) "2"
}
[2] => array(5) {
["CodeDepartement"] => string(8) "DEPBRAND"
["NummerHoofdstrategischeDoelstelling"] => string(1) "2"
["Nummer"] => string(2) "29"
["Titel"] => string(16) "DSD 03 - KLANTEN"
["IdBudgetronde"] => string(1) "2"
}
...
(The array goes on but it's too big to post it here in its entirety)
I can do a foreach loop on the array like this:
foreach($my_arr->StrategischeDoelstellingenPerDepartement as $row){
echo "i found one <br>";
}
However, I want to do the same thing on other arrays and I want to make the function generic. The first key (StrategischeDoelstellingenPerDepartement in this case) can sometimes change, which is why I'd like to do it generically. I've already tried the following:
foreach($my_arr[0] as $row){
echo "i found one <br>";
}
But then I get the following notice, and no data:
Notice: Undefined offset: 0 in C:\Users\Thomas\Documents\GitHub\Backstage\application\controllers\AdminController.php on line 29
This is probably a silly question, but I'm new to PHP and this seemed like the right way to do it. Obviously, it isn't. Can anyone help me out, please?
Use reset to grab the first element of $my_arr without knowing the key name:
$a = reset($my_arr);
foreach($a as $row){
echo "i found one <br>";
}
Shift the sub-array off the main array and loop over it:
$sub = array_shift($my_arr);
foreach ($sub as $row) {
echo $row['Titel'], "<br>";
}
You are trying to do is object, not array $my_arr->StrategischeDoelstellingenPerDepartement.
You could use isset() to check the index existence:
if(isset($my_arr['StrategischeDoelstellingenPerDepartement'])){
foreach($my_arr['StrategischeDoelstellingenPerDepartement'] as $row){
echo "i found one <br>";
}
}
Or, you could use array_values() to ignore the array keys and to make it an index array:
$my_new_arr = array_values($my_arr);
foreach($my_new_arr as $row){
echo "i found one <br>";
}
Use current ref : http://in3.php.net/manual/en/function.current.php
$a = current($my_arr);
foreach($a as $row){
echo "i found one <br>";
}
How can I get the follow thing done.
The follow array I have:
array(2) { [0] => array(3) {
["id"] => string(1) "5"
["avatar"] => string(15) "4e0d886ee9ed3_n"
["username"] => string(5) "testuser1"}
[1] => array(3) {
["id"] => string(1) "1"
["avatar"] => string(15) "4e25bc58b6789_w"
["username"] => string(6) "testuser2"
}
}
I want to create a array with just one user in it, but it has to be random. It can be like
user with id=5, id=1 or a hole other user (when there are more users).
Did you try, something like:
$rand = array_rand($your_array);
http://fr2.php.net/manual/en/function.array-rand.php
Try using array_rand().
$randomKey = array_rand($yourArray);
$randomUserId = $yourArray[$randomKey]['id'];
Simple.
$rand_user = array_rand($your_array);
PHP Manual: array_rand