This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
actually i dont understand why i get no access to the values of a array.
if i print a array i get this result
print_r($e)
Array
(
[FIELDNAME1] => MYTEXT
[FIELDNAME2] => MYTEXT2
)
now i want to access the field directly with
echo"Element 0".$e[0]."<br>";
echo"Element 1".$e[1]."<br>";
Under $e[0] and $e[1] I get no response (empty/nothing).
Why I can't get access to $e[0] etc.?
Is there any way to get access with 0/1/2... for this array, background is that i dont know the names of the elements, so i have to access with 1 and 2.
Because your array is associative. You'd access values by their associated key:
echo"Element 0".$e['FIELDNAME1']."<br>";
echo"Element 1".$e['FIELDNAME2']."<br>";
That's because you have an associative array here, where the array keys are FIELDNAME1 and FIELDNAME2 and not 0, 1 like you stated.
This will work:
echo"Element 0".$e['FIELDNAME1']."<br>";
echo"Element 1".$e['FIELDNAME2']."<br>";
Or if you want to loop through your array, try this:
foreach ($e as $k => $v) {
echo "Element $k : ".$v."<br>";
}
Related
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 3 years ago.
I have tried most of the solutions I found, but cannot get it to work.
I have a form parameter that post list of ids separated with commas in a string as per
$list_of_ids = "261,420,788";
Now I need to convert the list of ids into object or arrays so that I can loop through it and insert into database.
My problem is how I can make it look like this:
["261","420","788"]
Here is how I will like to loop it and update database
foreach($list as $id){
echo $id;
// loop and update database
}
$list = explode(",", $list_of_ids);
Should give you an array of the numbers. Take precaution against sql-injections though, if you want to put this into a database!
Just use explode :
$string= "123,234,345";
$array = explode(",",$string);
Output :
Array ( [0] => 123 [1] => 234 [2] => 345 )
This question already has answers here:
Using array_search for multi value search
(3 answers)
Closed 4 years ago.
I have a multi-dimensional array similar to this:
$arr1 = array(
0 => array("departmentID"=>1,"userID"=>"3000001"),
1 => array("departmentID"=>2,"userID"=>"3000002"),
2 => array("departmentID"=>3,"userID"=>"3000003")
);
I basically need to search the array to see if a specific key/value pair exists. For example, I need to know if department ID 2 with userID 3000002 is in the array.
I've tried this code:
$key = array_search('2', array_column($arr1, 'departmentID'));
echo ("The key is: ".$key);
This works fine but it's only a search on the department ID. I need to know if the departmentID value of 2 exists with the userID value of 3000002 and I can't quite figure it out.
Would be grateful for any help!
$key = array_search(array("departmentID"=>2,"userID"=>"3000002"), $arr1);
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
How can I iterate to get id value? This is my array:
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
tried
<?php
foreach($faculty as $value)
{
echo $value['Id'];
}
?>
Gives an error
Use of undefined constant Id - assumed Id
This is a json which is basically a string, to be more precise the given json contains a list (currently 1 element):
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
You need to convert it to an array first:
$jsonValues = json_decode($json, true); //here you will have an array of users (1 now)
foreach($jsonValues as $faculty) //for each user do something
{
echo $faculty['Id'];
}
This is JSON format. First you have to decode it. Example:
$a = '[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]';
$dec = json_decode($a);
echo $dec[0]->Id;
Result: 216
Decoded you have an array, containing exactly one object. You have to access the object properties with -> then.
With JSON [] brackets means an array, while {} brackets mean objects. Learn more: https://en.wikipedia.org/wiki/JSON
This question already has answers here:
get name of a post variable [duplicate]
(3 answers)
Closed 7 years ago.
Is there a way to get the names of the form fields from $_POST ?
I know I can get the values with $_POST['foo'], but how do I get the list of names (not sure what they are called).
Any help is great.
$_POST is an associative array. Use array_keys to get the keys of the array.
$names = array_keys($_POST);
As simple as that
print_r($_POST);
print_r — Prints human-readable information about a variable
http://php.net/manual/en/function.print-r.php
An array consists of array key and value.
$array = array('color1' => 'red','color2' => 'blue', 'color3' => 'green');
to get the key values use
$keys = array_keys($array);
print_r($keys);
This question already has answers here:
PHP json_decode notation issue
(2 answers)
Closed 8 years ago.
I m having json file as
{"103.186.190.216":1398640126}
i read the file and json decode it returs as
stdClass Object ( [203.196.190.226] => 1398640126 )
How can i print simple as
echo $json_arr->203.196.190.226;
Can have ip as key ?
i know can retrieve it by
foreach ($json_arr as $key => $val) {
echo $val;
}
Or how can print it in for loop rather than foreach
And also how can push values inside these type of object array
Coz normal php array_push(); Didn't work.
Or on the lighter note what is best way to create these type of array which we want give the key And value dynamically ?
Use this syntax:
echo $json_arr->{'203.196.190.226'};