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"
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';
I am new to PHP and codeigniter I have recently written a query in the model which is return as expected. The var_dump of the data array from the controller looks like
array(1){
["industry"]=> array(1) {
[0]=> array(3) {
["id"]=> string(1) "1"
["title"]=> string(11) "Recruitment"
["active"]=> string(1) "1"
}
}
}
From the view I have tried using a foreach based on industries but am not getting anywhere when trying to access the arrays length and items. Your experience and guidance is much appreciated.
You are have 3 nested arrays, so you will need 3 nested foreach's. I tried to do a sample basing on your var_dump, see:
$array = [
"industry" => [
"id" => "1",
"title"=> "Recruitment",
"active"=> "1"
],
];
foreach ($array as $k) {
foreach ($k as $k2) {
foreach ($k2 as $k3=>$v) {
echo $k3." --> ".$v."<br/>";
}
}
}
Let me know if it makes sense.
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>";
}
I seem to be having mental issues when it comes to arrays in php. I am not sure why, How ever my array looks like this:
$elementOptions = array(
array(
'type' => 'Text',
'name' => 'test' ,
'isRequired' => true,
'attributes' => array(
'placeholder' => 'content'
),
'subFormName' => 'content'
);
I have a for each loop as such:
foreach ($options as $key => $value) {
if (is_array($value)) {
//do something else
} else {
//do something
}
}
The issue is, if I do a var dump inside the if(isarray()){} I get the following back:
array(1) {
["placeholder"]=>
string(7) "content"
}
array(4) {
["type"]=>
string(4) "Text"
["name"]=>
string(4) "test"
["isRequired"]=>
bool(true)
["attributes"]=>
array(1) {
["placeholder"]=>
string(7) "content"
}
}
now the issue is - I do not want the following in that var dump:
array(1) {
["placeholder"]=>
string(7) "content"
}
I am not sure, based on the "data structure" above, how this 'placeholder' => 'content' is considered an array.....in either case I do not want it as part of the arrays that are var dumped.... it should just be the second array in that var dump coming back.
And that's where you guys come in, why is the place holder coming back as an array when it shouldn't (TMK - to my knowledge).
"placeholder"=>"content" isn't considered an array or coming back as an array, it's a pair in an array that you've called "attributes"
array(4) {
["type"]=>
string(4) "Text"
["name"]=>
string(4) "test"
["isRequired"]=>
bool(true)
["attributes"]=> // this is your array
array(1) { // contents of this array are...
["placeholder"]=> // this is a key
string(7) "content" // this is a variable
} // array ends
}
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'];