get array key name passed into the function like $array['key'] - php

Is it possible to get the array key name this way?
$array = ("first" => 1);
function f($object)
{
echo(???); //should give first
}
f($array['first']);

It's not possible if you pass only the value into the function like f($object['first']), the key name has no relation to the passed value in that case.
You need to pass the entire array (f($array)) and use:
echo key($object);

I don't really know what you want to get, but I guess this will help you:
$array = array('foo' => 'bar', 'baz' => 'foobar');
foreach ($array as $key => $value) {
echo $key . ' = ' . $value . '<br />';
}
This will return
foo = bar
baz = foobar

I would use
$array = ("first" => 1);
function f($object, $key) {
echo($key); // will give first
echo($object[$key]); // will give 1
}
f($array, 'first');

Related

iterate using foreach loop with key=>value but assign different valirable to each lines output

Is it possible to iterate using foreach loop with key=>value but assign different variable to each lines output. I am looking for something like this:
foreach($counts as $key => $value){
$c = $key . ' => ' . $value;
$d = $key . ' => ' . $value;
};
when the sample output from vardump($counts) looks like:
array(3) { ["Type_1"]=> int(1) ["Type_2"]=> int(3) ["Type_3"]=> int(1) }
Each
int() is a quantity so ideally I would like the output to look like
$c = 1
$d = 3
$e = 1
also ok with
$c = Type_1 1
$d = Type_2 3
$e = Type_3 1
Either way, I want to access the quantities by reference the variable directly.
Your best bet is to use an array, that's what they are for:
foreach($counts as $key => $value){
$output[] = $key . ' => ' . $value;
};
It depends on why you are doing this, how you will use it and what you know, but why not use the key:
foreach($counts as $key => $value){
$output[$key] = $key . ' => ' . $value;
};
You could create an array of variable names and shift one off each time and use that, however you will never know how many there are and this is very bad practice:
$vars = range('a', 'z');
foreach($counts as $key => $value){
${array_shift($vars)} = $key . ' => ' . $value;
};
In your example you would have $a, $b and $c.
Not sure what your purpose here but you can assign different variable to each lines output by using reference variable that stores the value of the $variable inside it, that is $key in your code like below. It will help in case you know what keys will be in counts array all the time.
$counts = [ "Type_1" => 1,"Type_2"=>3, "Type_3"=> 1 ];
foreach($counts as $key => $value){
$$key = $key . ' => ' . $value;
};
echo $Type_1;
echo $Type_2;
echo $Type_3;
Output will be
Type_1 => 1
Type_2 => 3
Type_3 => 1

How can I get keys of a name attribute

I have this name="opt['.$id.']" value="'.$points.'" inside a checkbox input.Does anybody knows how I can get the $id?
UPDATED:
foreach($_POST['opt'] as $id => $value) {
$gift_ids = $value;
$gift_ids2 = implode(", ", $gift_ids);
}
echo $gift_ids2;
}
But I don't get any value on echo..
You need to iterate over the HTML array. Something like this should do it for you:
foreach($_POST['opt'] as $id => $value) {
Demo: https://eval.in/585379
your used array so you need to Iterate the $_POST['opt'] value
$_POST = array('opt' => array('1'=>100 ), 'eksasrgirwsh' => 'other');
foreach($_POST['opt'] as $id => $value)
{
echo $id //key example 1
echo $value //value example 100
}
If your $_POST is like this
$_POST = array('opt' => array('1'=>100 ), 'eksasrgirwsh' => 'other');
and you are giving $_POST['opt'] to foreach then your array for foreach is
$_POST['opt'] = array('1'=>100);
then you can't use implode in foreach because it give you an error. Do it without implode.
foreach($_POST['opt'] as $id => $value) {
$gift_ids = $value;
echo $gift_ids;
}

How to store each array value in a variable with PHP

I have an array that looks something like this:
Array
(
[2] => http://www.marleenvanlook.be/admin.php
[4] => http://www.marleenvanlook.be/checklogin.php
[5] => http://www.marleenvanlook.be/checkupload.php
[6] => http://www.marleenvanlook.be/contact.php
)
What I want to do is store each value from this array to a variable (using PHP). So for example:
$something1 = "http://www.marleenvanlook.be/admin.php";
$something2 = "http://www.marleenvanlook.be/checklogin.php";
...
You can use extract():
$data = array(
'something1',
'something2',
'something3',
);
extract($data, EXTR_PREFIX_ALL, 'var');
echo $var0; //Output something1
More info on http://br2.php.net/manual/en/function.extract.php
Well.. you could do something like this?
$myArray = array("http://www.marleenvanlook.be/admin.php","http://www.marleenvanlook.be/checklogin.php","etc");
$i = 0;
foreach($myArray as $value){
${'something'.$i} = $value;
$i++;
}
echo $something0; //http://www.marleenvanlook.be/admin.php
This would dynamically create variables with names like $something0, $something1, etc holding a value of the array assigned in the foreach.
If you want the keys to be involved you can also do this:
$myArray = array(1 => "http://www.marleenvanlook.be/admin.php","http://www.marleenvanlook.be/checklogin.php","etc");
foreach($myArray as $key => $value){
${'something'.$key} = $value;
}
echo $something1; //http://www.marleenvanlook.be/admin.php
PHP has something called variable variables which lets you name a variable with the value of another variable.
$something = array(
'http://www.marleenvanlook.be/admin.php',
'http://www.marleenvanlook.be/checklogin.php',
'http://www.marleenvanlook.be/checkupload.php',
'http://www.marleenvanlook.be/contact.php',
);
foreach($something as $key => $value) {
$key = 'something' . $key;
$$key = $value;
// OR (condensed version)
// ${"something{$key}"} = $value;
}
echo $something2;
// http://www.marleenvanlook.be/checkupload.php
But the question is why would you want to do this? Arrays are meant to be accessed by keys, so you can just do:
echo $something[2];
// http://www.marleenvanlook.be/checkupload.php
What I would do is:
$something1 = $the_array[2];
$something2 = $the_array[4];

converting array containing keys and values into string

I have an array it contains key and value. i would like to converting into a string.
array(
[business_type]=>'cafe'
[business_type_plural] => 'cafes'
[sample_tag]=>'couch'
[business_name]=>'couch cafe'
)
Expected Output:
business_type,cafe|business_type_plural,cafes|sample_tag,couch|business_name,couch cafe
NOTE:
I was searching in StackOverflow and found the below question and it has answer. I want exactly reverse one.
converting string containing keys and values into array
Try
$data = array();
foreach($arr as $key=>$value) {
$data[] = $key.','.$value;
}
echo implode('|',$data);
Another Solution:
function test_alter(&$item1, $key, $delimiter)
{
$item1 = "$key".$delimiter."$item1";
}
array_walk($arr, 'test_alter',',');
echo implode('|',$arr);
Use the foreach() function to go through the array and string the keys/values together...
Assuming your array is called $array
$result = "";
foreach($array as $key => $value){
$result .= $key . "," . $value . "|";
}
It's as simple as that.
EDIT - Thanks Nelson
After that, lost the last |
$result = rtrim($result, "|");
try this
$pieces=array();
foreach(array('key1'=>'val1', 'key2'=>'val2', 'key3'=>'val3') as $k=>$v)
{
$pieces[]=$k.','.$v;
}
echo implode('|', $pieces);

Does it exist any function to dynamically create variables from the key value pair of an array?

Let's say I have this array,
$array = array(
'name' => 'hermet',
'emails' => array ('hermet#example.com',
'hermet#example.net');
);
So this way echo $array ['name'] == 'hermet' prints true. I would like to know if there is a function already embedded in PHP that let me do this:
echo $name == 'hermet'; // obviously 'false'
foreach ($array as $key => $value) {
$aux = $key;
$$aux = $value;
}
echo $name == 'hermet'; // now prints 'true'
It seems to work even with a multidimensional array but I don't know if PHP has already any function to do that.
Thank you in advance.
You might be looking for extract
$array = array(
'name' => 'hermet',
'emails' => array ('hermet#example.com',
'hermet#example.net')
);
extract($array);
var_dump($emails);
echo $name;
-- EDIT: If you are concerned about Paul's remark, supply EXTR_SKIP to the second argument of extract, that way it won't overwrite variable in case you've already defined it prior to calling extract.
$name = 'jason';
extract($array, EXTR_SKIP);
echo $name; // still 'jason'

Categories