Display data from multidimensional array [duplicate] - php

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

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

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 get keys from associative array [duplicate]

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

How to return an array in a foreach loop in PHP [duplicate]

This question already has answers here:
How can I echo or print an array in PHP?
(14 answers)
Closed 7 years ago.
I would like to do certain calculation in a function that takes a constant value against different values. To do that I created an array and a variable and also a loop that will take each member of the array and the constant value and pass them to the function that does the calculation. I would like to get an array and then I can do more calculations subsequently.
function subtract($a, $b){
$c=$b-$a;
return $c. ',';
}
$r=3;
$numbers = array(12, 11, 6, 9);
foreach ($numbers as $index=>$value) {
$deductions=array(subtract($r, $value));
//$minimum=min($deductions);
if (is_array($deductions)){
//echo $deductions;
}else{
//echo "not array";
}
}
//$minimum=min($deductions);
//echo $minimum;
echo $deductions;
I get "Array" and not 9,8,3,6
Why is this? Any help is greatly appreciated. echo was partial problem, I get
Array
(
[0] => 6,
)
not 9,8,3,6 as I expected?
You cannot echo an array. Try using print_r($deductions) or var_dump($deductions).
Also, the following line is likely incorrect:
$deductions=array(subtract($r, $value));
This line will keep replacing the previous $deductions variable so you only end up with one value in your array (6) because 9-3 is 6. If you are wanting to create an array of values you need to add the new value to the array as follows:
$deductions[] = subtract($r, $value);
You can't echo an array. Use var_dump or print_r instead.
You can also:
for($i=0;$i<count($deduction);$i++)
{
$return += $deduction[$i]." ";
}
echo $return;

how to calculate two values in array_map function [duplicate]

This question already has answers here:
Sum values of multidimensional array by key without loop
(5 answers)
Closed 10 months ago.
How can I calculate the value of one string containing few values?
For example,
$toPluck = 'price';
$arr = $gamesWorth;
$plucked = array_map(function($item) use($toPluck) {
echo $item[$toPluck];
}, $arr);
The webpage displays 2, 20, and 50.
I want to calculate them both and echo to the page 72.
I tried to find a solution on the web and on that website, but I can't find it..
Seems like a job for array_reduce
$toPluck = 'price';
$arr = array(
array('price' => 2),
array('price' => 20),
array('price' => 50),
);
echo array_reduce($arr, function($sum, $item) use($toPluck) {
return $sum + $item[$toPluck];
}, 0);
There are a few ways to handle it, but a simple modification to your existing code would be to return values from your array_map() and sum the resultant array with array_sum().
$toPluck = 'price';
$arr = $gamesWorth;
$plucked = array_map(function($item) use($toPluck) {
// Uncomment this if you want to print individual values
// Otherwise the array_map() produces no output to the screen
// echo $item[$toPluck];
// And return the value you need
return $item[$toPluck];
}, $arr);
// $plucked is now an array
echo array_sum($plucked);

Categories