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.
Related
Hello I've multidimensional array that looks like that:
array(13890) {
[0]=>
array(2) {
["Icd"]=>
array(2) {
["id"]=>
int(111)
["nazwa"]=>
string(6) "DŻUMA"
}
["ProjectIcd"]=>
array(0) {
}
}
[1]=>
array(2) {
["Icd"]=>
array(2) {
["id"]=>
int(566)
["nazwa"]=>
string(7) "ŚWINKA"
}
["ProjectIcd"]=>
array(0) {
}
}
An so on.
I want to change it so it looks something like that:
array(13890) {
[0]=> array(2) {
["id"]=>
int(111)
["text"]=>
string(6) "DŻUMA"
}
How is this possible to do?
I want to add, I want to convert the array to json and feed it to select2 js in ajax.
Will that be a problem or not?
Short solution using array_map function:
// $arr is your initial array
$new_arr = array_map(function($a){
return ['id' => $a['Icd']['id'], 'text' => $a['Icd']['nazwa']];
}, $arr);
So you can simple create a new array and add there the values, which you want based on the old array. Then you convert the array to a json string with the php function json_encode():
$array = array("text"=>$old_array[0]["Icd"]["nazwa"]);
echo json_encode($array);
I hope this is something that you want.
$res = [];
$i = 0;
foreach($array as $arr) {
//get keys
if (count($arr) > 0) {
$keys = array_keys($arr);
// loop through the keys and fetch data of those keys
// put in array
foreach($keys as $key) {
if ($arr[$key]) {
$res[$i]['id'] = $arr[$key]['id'];
$res[$i]['text'] = $arr[$key]['nazwa'];
}
$i++;
}
}
}
print_r($res);
// To change array to json
echo json_encode($res);
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
}
}
I have an array of JSON objects that look like this:
{"id":1,"place":2,"pic_name":"aaa.jpg"}
My PHP file receives a simple array like:
["4","3","1","2","5"]
These numbers correspond to the place value in my JSON object. Now I need to compare the place of each element in the secont array with the value of ID in the JSON object and if the match I have to replace the value of PLACE with the element from the array.
I am having problems doing the comparison. Any guidelines? What I come up with:
foreach($ids as $index=>$id) { //the array
$id = (int) $id;
foreach ($obj as $key=>$value) { //the JSON objects
foreach ($value as $k=>$v){
if ($id != '' && array_search($index, array_keys($ids)) == $value[$k]['id']) {
$value[$k]['place'] = $id;
file_put_contents($file, json_encode($ids));
} else { print "nope";}
} } }
That will be:
$array = [
'{"id":1,"place":2,"pic_name":"aaa.jpg"}',
'{"id":2,"place":5,"pic_name":"aab.jpg"}',
'{"id":3,"place":1,"pic_name":"aba.jpg"}',
'{"id":4,"place":3,"pic_name":"baa.jpg"}',
'{"id":5,"place":4,"pic_name":"abb.jpg"}'
];
$places = ["4","3","1","2","5"];
$array = array_map(function($item)
{
return json_decode($item, 1);
}, $array);
foreach($array as $i=>$jsonData)
{
if(false!==($place=array_search($jsonData['id'], $places)))
{
$array[$i]['place'] = $place+1;
}
}
$array = array_map('json_encode', $array);
//var_dump($array);
-with output like:
array(5) {
[0]=>
string(39) "{"id":1,"place":3,"pic_name":"aaa.jpg"}"
[1]=>
string(39) "{"id":2,"place":4,"pic_name":"aab.jpg"}"
[2]=>
string(39) "{"id":3,"place":2,"pic_name":"aba.jpg"}"
[3]=>
string(39) "{"id":4,"place":1,"pic_name":"baa.jpg"}"
[4]=>
string(39) "{"id":5,"place":5,"pic_name":"abb.jpg"}"
}
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));
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'];
}