PHP multidimensional array - foreach loop - php

I have a problem getting array values "lat" and "long" without going deeper by foreach function. Array:
array(1) {
["Berlin, Germany(All airports)"]=>
array(1) {
["Berlin Brandenburg Willy Brandt(BER)"]=>
array(2) {
["lat"]=>
string(9) "52.366667"
["lon"]=>
string(9) "13.503333"
}
}
}
Function:
foreach($results as $key => $val){
//here i want to reach lat and long without additional foreach loop
}
Thank you all for answers.

foreach($results as $key => $val){
$temp = current($val); # fetch first value, which is array in your example
echo $temp['lat'];
}

Related

Re-arranging and combining an array

I'm having an issue when I dump the array below. This dumps several arrays. Some are 2 some are 3, which complicates it even more. Basically what I want I put below. I have tried array_push, array_combine, array_merge, several different ways including $array[$param] = $insertValue and I'm stuck. I am open to creating a brand new array too.
Please note not all arrays are counts of 3 but always return at least 1.
Original array:
array(3) {
[0]=>
array(2) {
["contact_id"]=>
string(9) "CONTACTID"
["contact_id_content"]=>
string(19) "123456789123456"
}
[1]=>
array(2) {
["sm_owner"]=>
string(9) "SMOWNERID"
["sm_owner_content"]=>
string(19) "123456798452"
}
[2]=>
array(2) {
["contact_owner"]=>
string(13) "Contact Owner"
["contact_owner_content"]=>
string(16) "Jane Doe"
}
Array desired:
array(3) {
[0]=>
array(6) {
["contact_id"]=>
string(9) "CONTACTID"
["contact_id_content"]=>
string(19) "123456789123456"
["sm_owner"]=>
string(9) "SMOWNERID"
["sm_owner_content"]=>
string(19) "123456798452"
["contact_owner"]=>
string(13) "Contact Owner"
["contact_owner_content"]=>
string(16) "Jane Doe"
}
try this code:
$NewArray = array();
foreach($OriginalArray as $value) {
$NewArray[] = array_merge($value,$NewArray);
}
or you can use array_merge_recursive
let $result = [];
foreach ($yourarray as $key => $value) {
$result = $value;
}
var_dump($result);
Here you go: How to Flatten a Multidimensional Array? – Barmar 23 mins ago
function flatten($array)
{
return array_reduce($array, function($acc, $item){
return array_merge($acc, is_array($item) ? flatten($item) : [$item]);
}, []);
}
// loop the individual fields
for ($i=0; $i<count($row_data); $i++) {
$newArray = flatten([$i => $response_row]);
}
Try something like this:
function flatten(array $array) : array
{
$newArray = [];
foreach ($array as $subArray) {
foreach ($subArray as $key => $value) {
$newArray[$key] = $value;
}
}
return $newArray;
}

Create a n.2 PHP array from JSON output

I'm using
$data=json_decode($response,true)
the output is
array(3)
{
["instrument"]=> string(7) "EUR_USD" ["granularity"]=> string(1) "D" ["candles"]=> array(10)
{
[0]=> array(7)
{
["time"]=> string(27) "2016-09-26T21:00:00.000000Z" ["openMid"]=> float(1.125495) ["highMid"]=> float(1.1259) ["lowMid"]=> float(1.119125) ["closeMid"]=> float(1.121605) ["volume"]=> int(17059) ["complete"]=> bool(true)
}
[1]=> array(7)
{
["time"]=> string(27) "2016-09-27T21:00:00.000000Z" ["openMid"]=> float(1.1218) ["highMid"]=> float(1.12369) ["lowMid"]=> float(1.118215) ["closeMid"]=> float(1.12171) ["volume"]=> int(17915) ["complete"]=> bool(true)
}
}
}
I want to create two arrays with the values openMid and closeMid for example:
$open=array(1.125495,1.1218)
$close=array(1.121655,1.12171)
I have to develop the foreach code in order to achieve that.
Anyone can help me? Thanks
Check out the solution below:
$open= []; //Declare empty array to hold openMid values
$close= []; //Declare empty array to hold closeMid values
#Loop through $array
foreach ($array as $key => $value) {
#If any of the value is an array
if (is_array($value)) {
$arr= $value; //Store the array
}
}
//Loop through the second array
foreach ($arr as $val) {
//If the value is of index openMid
if ($val->openMid) {
$open[]= $val->openMid; //Push into the $open array holder
}
//If the value is of index closeMid
if ($val->closeMid) {
$close[]= $val->closeMid; //Push into the $close array holder
}
}

Assign Number to Foreach Variables

I want to create a list of variables from a foreach loop. I have an array like so
array(5) {
[0]=> string(105) "http://a4.mzstatic.com/us/r30/Purple5/v4/06/f2/02/06f20208-1739-4fda-c87f-171d73b912a7/screen480x480.jpeg"
[1]=> string(105) "http://a1.mzstatic.com/us/r30/Purple1/v4/53/89/0b/53890b90-c6a5-4db3-cfb9-d6314bf9cfd1/screen480x480.jpeg"
[2]=> string(105) "http://a3.mzstatic.com/us/r30/Purple1/v4/8f/31/b3/8f31b351-c9d7-e545-0ace-e09fcb390264/screen480x480.jpeg"
[3]=> string(105) "http://a5.mzstatic.com/us/r30/Purple3/v4/e7/5f/de/e75fde7b-dc5f-5b26-e531-abf07c409317/screen480x480.jpeg"
[4]=> string(105) "http://a1.mzstatic.com/us/r30/Purple3/v4/27/ce/c6/27cec64e-7e6d-7135-cc76-d0132151dadb/screen480x480.jpeg"
}
and I'd like to assign each to it's own variable. I want the final output to be something like
$var0 = "first array url";
$var1 = "second array url";
etc...
I think I want something like this but it's not quite the right syntax.
foreach ($array as $key => $value) {
$var[$key] = $value;
}
Any ideas?
foreach($array as $key => $value) {
${"var{$key}"} = $value;
}
The function you are searching for is extract.
Just use it after your foreach loop given in your example.
$vars = [];
foreach ($array as $i => $value) {
$vars['var'.$i] = $value;
}
extract($vars);

Unable to iterate on the content of an array

I have an array which contents data that I can see on doing a var_dump() but I am unable to iterate through its content using foreach()
The var_dump() generates the following output
array(4) { [0]=> array(1) { [0]=> string(5) "Admin" } [1]=> array(1) { [0]=> string(4) "rick" } [2]=> array(1) { [0]=> string(6) "techbr" } [3]=> array(1) { [0]=> string(7) "testdom" } }
I want to be able to get the content of this array and store it in another.
Currently I am using the following code
$empList = array();
$empList = emp_list($mysqli);
var_dump($empList);//This generated the above output
foreach ($empList as $value)
{
echo $value."<br>";
}
Output of the echo is this
Array
Array
Array
Array
How do I sort this out?
Thank you for your suggestions I have modified the code this way
$i=0;
$empList = array();
$tempList = array();
$tempList = emp_list($mysqli);
foreach ($tempList as $value)
{
$empList[$i] = $value[0];
$i++;
}
Now the $empList array stores stuff in the correct format
It has an array inside another array so, use two foreach loops
$empList = array();
$empList = emp_list($mysqli);
foreach ($empList as $value)
{
foreach ($value as $temp)
{
echo $temp."<br>";
}
}
As u_mulder says in the comments on your question, your array isn't an array of strings - it's an array of more arrays. var_dump() is designed to deal with complicated nested contents, but echo can't print arrays - that's why it's just telling you that each item in $empList is an array, and not what its contents are.
If you wanted to get the content out of a specific array in $empList you'd need to access it by its index key, with something like:
$first = $empList[0];
foreach ($first as $value) {
echo $value."<br>";
}
Or if you wanted to iterate through them all you could just put two foreach loops one inside the other.

Read key of array two dimension

I try to read some keys of an array like
array(3) {
["poste5:"]=> array(3) { [0]=> string(7) "APPLE:" [1]=> string(5)
"demo1" [2]=> string(5) "demo2" }
["services:"]=> array(4) { [0]=> string(9) "orange" [1]=>
string(5) "demo3" [2]=> string(5) "demo4" [3]=> string(5) "demo1" }
["essai:"]=> array(2) { [0]=> string(6) "sd" } }
I try to read this name : poste5 , services , essai
If i use :
foreach ($this->aliasRead as $key => $value){
echo array_keys($this->aliasRead[$key]);
}
I have : Array()
But if i use :
foreach (array_keys($this->aliasRead) as $key => $value2) {
echo $value2;
}
I have poste5 , services , essai
I try to use with this loop foreach ($this->aliasRead as $key => $value){
because i have another traitment after.
How to collect this key of my first loop in this loop foreach ($this->aliasRead as $key => $value){ ?
You already have what you want here:
foreach ($this->aliasRead as $key => $value){
echo $key; // key of the value in the array
print_r($value); // value of $this->aliasRead[$key] which in turn is another array
}
Edit: The reason your second loop works is because of this: array_keys($this->aliasRead[$key]) returns a new array containing the keys of the old array as its values. So $myNewArray = array_keys($this->aliasRead[$key]) is the same as $myNewArray = array('poste5','services','essai'). So, when you loop over this new array like this:
foreach ($myNewArray as $key => $value2) {
echo $value2;
}
$value2 contains your values, which are the keys of your first array, and $key will be 0, 1 and 2 after each step through the loop.
Try this,
$keys = array_keys($this->aliasRead);
print_r($keys);
Or
$keys = array();
foreach ($this->aliasRead as $key => $value){
$keys[] = $key;
}
It's because you're trying to echo an array. This will always give you the string "Array". If you want to see the array's contents, try
var_dump(array_keys($this->aliasRead[$key]));
By the way, in the foreach statement you posted, $this->aliasRead[$key] will be the equal to $value. So this will work as well:
var_dump(array_keys($value));

Categories