How find the Name in multidimensional arrays in php [duplicate] - php

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

Related

How to properly convert string form post variables to objects or arrays [duplicate]

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 )

Search multi-dimensional array for specific key/value [duplicate]

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

How to push key value of element to array_push using PHP [duplicate]

This question already has answers here:
array_push() with key value pair
(8 answers)
Closed 4 years ago.
I have created empty array and want to push all values and its key to the new created array but I am getting error array_push() expects at least 2 parameters I know array_push needs two parameter and here I am passing only one but what I ant is all keys and values to be pushed directly to array
// Here 'userid' is just text or can say sample key and
// Here $userid is getting from table so expected output
// 'userid'=>$userid
$temp = array();
array_push($temp['userid'] = $userid);
To push to an array using a key - value pair, you do not need to use array_push.
array_push expects the array, and the value (no key) that you're pushing.
To push to an array using a key - value pair simply do :
$temp['user_id'] = $userid;
To use array_push you must give it the original array (modified by reference) and the new entry (value) for it. In this case since you have a key as well, you'd need to merge your arrays:
$temp = array_merge($temp, ['userid' => $userid]);
However, you can use simple array syntax instead to achieve the same thing:
$temp = array();
$temp['userid'] = $userid;

Access to a array wont work [duplicate]

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>";
}

PHP array of arrays: get a list of values for a given attribute [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 years ago.
suppose i have an array of arrays in PHP:
$array = [
['value'=>1, 'other_attribute'=>'whatever'],
['value'=>13, 'other_attribute'=>'whatever'],
['value'=>45, 'other_attribute'=>'whatever'],
['value'=>64, 'other_attribute'=>'whatever'],
];
how can i get a list containing just a specific attribute of each of the array's elements? in my case, if i wanted to get the list of 'value', the output should look like this:
[1, 13, 45, 64]
using the Laravel framework, it's easy to do this with query builder objects, simply like this: $array->lists('value');. is there a way to do this in plain PHP?
Sure, just build your own loop to do it:
$values = []; // the new array where we'll store the 'value' attributes
foreach ($array as $a) { // let's loop through the array you posted in your question
$values[] = $a['value']; // push the value onto the end of the array
}

Categories