Php get keys from associative array [duplicate] - php

This question already has answers here:
PHP get key value from array
(3 answers)
Closed 1 year ago.
How could I get only the entire first row from an array. (I use Laravel)
For example when I say:
$request-all();
I receive:
array:2 [
"email" => "james#hotmail.com"
"password" => "adms|Wh"
]
I want to receive:
email
password

If I understood your question correctly, you want something like this:
$keys = array_keys($array);
foreach ($keys as $key){
echo $key;
}

You can use the PHP array_keys() function to get all the keys out of an associative array.
$array = array("email"=>"james#hotmail.com", "password"=>"adms|Wh");
Get keys from $array array
print_r(array_keys($array));
You can also use the PHP foreach loop to find or display all the keys, like this:
Loop through $array array
foreach($array as $key => $value){
echo $key . " : " . $value . "<br>";
}

Related

How to allow same keys in one array? [duplicate]

This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed 4 years ago.
I need to add the same keys to the array, but with different values,
foreach ($selections as $selection) {
$array += [$selection['option_id']=>$selection['product_id']];
}
// example output
$array = [30=>12,14=>10],
but really it should be
[30=>7,30=>12,14=>10];
When the key repeats, it merges.
You just can't.
But you can make the value of this key an array.
So you'll have
$array = [30=>[7,12],14=>10];
You can use any array functions on $array[30]
What you should do is to return the products ids as an array:
$array = array_reduce($selections, function ($carry, $selection) {
if (!isset($carry[$selection['option_id']])) {
$carry[$selection['option_id']] = [];
}
$carry[$selection['option_id']][] = $selection['product_id'];
return $carry;
}, []);
Now the result would be:
[30 => [7, 12], 14 => [10]];
Keys in array are, as the word itself says, keys to access the value they contain and each key must be unique, else you won't have a way to . If you could have two time or more the same value, how could you tell which will access one value and which one will access the other one? To solve your problem you have a way: generate a multidimensional array such that you can have multiple value stored "behind" a single key. E.g. [30 => [7,12], 14 => 10]
Based on your code you can just create a double loop with a nested foreach to navigate through all the value, something like:
foreach ($selections as $selection) {
if(!is_array($selection['product_id']) $array += [$selection['option_id']=>$selection['product_id']];
else {
foreach ($selection['product_id'] as $product) {
$array += [$selection['option_id']=> product];
}
}
}

Display data from multidimensional array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
How do I display the individual values from this array?
for instance: X = 8.6; Y = 43; F = more stuff?
$MEGA['Stuff'] = [
8.6,
43,
'more stuff'
];
You could make it an associative array with key-value pair.
$MEGA['Stuff'] = [
'X' => 8.6,
'Y' => 43,
'F' => 'more stuff'
];
foreach ($MEGA['Stuff'] as $k => $v) {
echo $k . ' : '. $v;
echo '<br/>';
}
Adding to Object Manipulator answer, above, in case you cannot manipulate the original array, you could use the array_combine function to set the keys to the array, thus reducing the need of iterating over it twice.
$keys = ["X", "Y", "F"];
$MEGA["Stuff"] = array_combine($keys, $MEGA["Stuff"]);
Now the $MEGA["Stuff"] array is in the form, Object Manipulator has it, and you can manipulate it at your liking
suppose k,y and f is fix therefor use can use following code.
you have 3 characters k,y,f right
arrays count is 3
now you can make code like this
$char=array('k','y','f');
get count of $MEGA['Stuff'];
$count=count($MEGA['Stuff']);
Now we are using for loop.
for($i=0;$i<$count;$i++)
{
echo $char[$i].' = '.$MEGA['Stuff'][$i];echo '<br/>';
}
You can use this code for display value from array.
You can just use echo to display the data:
echo $MEGA['stuff'][0])
echo $MEGA['stuff'][1])
echo $MEGA['stuff'][2])

Grab value from complex array and output as simple associative array [duplicate]

This question already has answers here:
How to use array values as keys without loops? [duplicate]
(6 answers)
Closed 6 years ago.
Please check attached images. These image contains $complex_arr print and $simple_arr print results. I want to convert $complex_arr to $simple_arr output.
How is that possible? What is actually doing each of $complex_arr inside value will be converted as associative simple array like $simple_arr
$arr1 = array("asso1"=>"a", "asso2"=>"1");
$arr2 = array("asso1"=>"b", "asso2"=>"2");
$arr3 = array("asso1"=>"c", "asso2"=>"3");
$complex_arr = array($arr1,$arr2,$arr3);
$simple_arr = array("a"=>"1", "b"=>"2", "c"=>"3");
// print_r($complex_arr);
print_r($simple_arr);
Input:
Output:
You have to write it on our own... here is my idea:
public function makeItSimpler($arr){
$newarr = array();
foreach($arr as $complex){
$newarr[$complex['asso1']]=$complex['asso2'];
}
return $newarr;
}
Perhaps you can do it better... take look at "array-map" http://php.net/manual/de/function.array-map.php
Good Luck
foreach ($complex_arr as $key => $value) {
$simple_arr[$value['asso1']]=$simple_arr[$value['asso2']];
}
With php5.5 (where array_column becomes available) it is:
$simple_arr = array_combine(
array_column($complex_arr, 'asso1'),
array_column($complex_arr, 'asso2')
);
And even simplier (after I reread function manual):
$simple_arr = array_column($complex_arr, 'asso2', 'asso1');

PHP foreach change array value [duplicate]

This question already has answers here:
How to modify an array's values by a foreach loop?
(2 answers)
Closed 4 months ago.
When you have a foreach loop like below, I know that you can change the current element of the array through $array[$key], but is there also a way to just change it through $value?
foreach($array as $key => $value){
}
It's probably really simple, but I'm quite new to PHP so please don't be annoyed by my question :)
To be able to directly assign values to $value, you want to reference $value by preceding it with & like this:
foreach($array as $key => &$value){
$value = 12321; //the same as $array[$key] = 12321;
}
unset($value);
After the foreach loop, you should do unset($value) because you're still able to access it after the loop.
Note: You can only pass $value by reference when the array is a variable. The following example won't work:
foreach(array(1, 2, 3) as $key => &$value){
$value = 12321; //the same as $array[$key] = 12321
}
unset($value);
The php manual on foreach loops
there's a function for that, and is builtin since early version of PHP, is called array_map

PHP - multidimensional array from arrays [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
I've got 6 arrays - 1 with name and 5 with some properties - which should be assigned to that name. All values are of course in order. I'd like to make a 2-dimensional array with will be later put into CSV and the result should be as on the table here:
I guess that i have to do 2 loops here, but I can't make them work. How to construct such array?
Solution found
I've connected all arrays:
$final_array = array($nazwa_array,$new_ilosc_array,$new_koszt_array,$new_cena_lifo_array,$new_cena_fifo_array,$new_rodzaj_array);
I've found a matrix transposition function, which returns array in correct order:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$a = array();
foreach ( $names AS $key => $value ) {
$a[$key]['name'] = $value;
$a[$key]['property1'] = $value.'->'.$property1_array[$key];
$a[$key]['property2'] = $value.'->'.$property2_array[$key];
$a[$key]['property3'] = $value.'->'.$property3_array[$key];
$a[$key]['property4'] = $value.'->'.$property4_array[$key];
$a[$key]['property5'] = $value.'->'.$property5_array[$key];
}

Categories