I have the following array. how can I get the value of 'installed' key i.e 1. which value I have to check in my application.
Array
(
[0] => Array
(
[id] => 53686899
)
[1] => Array
(
[installed] => 1
[id] => 542813519
)
[2] => Array
(
[installed] => 1
[id] => 567790764
)
[3] => Array
(
[id] => 567570764
)
)
using foreach loop how can i do this job? anybody can plz help me?
foreach ($array as $value)
{
echo $value['installed']. "<br />";
}
will output
1
1
Try this :
foreach ($array as $value){
if(array_key_exists('installed',$value)){
echo $value['installed']. "<br />";
}
}
If you are not checking for array_key_exists it will show error in first loop.
Absolutely the same way like when you iterate 1 dimensional array:
foreach ($array as $value) {
var_dump($value);
var_dump($value['installed'];
}
Loop through the array and get the 'installed' key's value:
foreach ($array as $innerArray) {
echo $innerArray['installed'];
}
Related
Hey guys i am new of the laravel, how can i parse this output array? This is my array is coming from repeater using jquery.
Array
(
[tour_baslik] => 1. Day
[tour_icerik] => content here....
[lunch] => Array
(
[0] => 2
)
[dinner] => Array
(
[0] => 1
[1] => 2
)
)
Array
(
[tour_baslik] => 2.Day
[tour_icerik] => content 2 here...
[lunch] => Array
(
[0] => 1
[1] => 2
)
[dinner] => Array
(
[0] => 2
)
)
I need parse like that but i'm stuck:
foreach($myarray as $key => $data){
echo $key . '-' . $data; }
Output must be:
tour_baslik - 1.day
tour_icerik - content here..
lunch - 2
dinner - 1,2
If you need to iterate through all items of your input, you could use a recursive function like the following:
function iterator($key, $value) {
if (is_array($value)) {
foreach ($value as $key => $value) {
iterator($key, $value);
}
} else {
echo !empty($key) ? "$key - " : "";
echo $value."\n";
}
}
iterator(null, $input);
I have data from my shop in three arrays. The output:
Array ( [0] => Array ( [1] => Array ( [infos] => Array ( [id_category] => 1...
I want to get id_category. Should I use two foreach or something else?
Here an example for using a foreach:
foreach ($array as $key1 => $level1){
foreach ($level1 as $key2 => $level2){
echo $level2['infos']['id_category'];
}
}
I have an array called $test_data, and I want to update a key ['test_duration']. However, I am unable to do this update. Consider the following array:
Array
(
[0] => Array
(
[test_id] => 1116
[test_name] => ques stats
[test_no_questions] => 50
[test_duration] => 28800
)
[1] => Array
(
[test_id] => 1112
[test_name] => Own Test 1
[test_no_questions] => 2
[test_duration] => 7200
)
)
I tried the following, but it didn't work out:
foreach ($test_data as $key => $value) {
$value[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
If I print the array after this manipulation, it's printing the same array as before. What is the problem here?
update $test_data instead of $value
foreach ($test_data as $key => $value) {
$test_data[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
You need to nest a furthermore.
foreach ($test_data as $arr)
{
foreach($arr as $k=>$v)
{
$value[$k]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
}
Use like this,
foreach ($test_data as $key => $value) {
$test_data[$key]['test_duration'] = ConvertTimeStampToTimeFormate($value['test_duration']);
}
This is my first question here so i dont exactly know the normal style.
I have a problem with multiple arrays. My arrays are sorted this way:
Array
(
[count] => 2
[gebruikerData] => Array
(
[gebruiker1] => Array
(
[merken] => Array
(
[0] => merk1
[1] => merk10
[2] => merk19
)
[loginnaam] => testfasdfasd
[geslacht] => Man
[persoonlijkheidsType] => TEST
[beschrijving] => fasdfasdfasd
[gebruikerID] => 19
[leeftijd] => 21
)
[gebruiker2] => Array
(
[merken] => Array
(
[0] => merk1
[1] => merk9
[2] => merk36
)
[loginnaam] => test1233
[geslacht] => Man
[persoonlijkheidsType] => TEST
[beschrijving] => safasfd
[gebruikerID] => 20
[leeftijd] => 21
)
)
)
I need to retrieve all the information in this array. There can be as many fields gebruiker(number) as the database output, so i tried to use multiple foreach loops in eachother. My problem is that it is not possible to use the key from one foreach loop as index in another foreach loop like this:
foreach ($gebruikerData as $key => $value)
{
foreach ($key as $key2 => $value2)
{
echo $key2;
}
}
Does anyone have another idea how i could retrieve the information from the array? Or is if could use my own way with a slight change?
Try like this
foreach ($gebruikerData as $key => $value)
{
if(is_array($key))
{
foreach ($key as $key2 => $value2)
{
if(is_array($key2))
{
foreach($key2 as $key3=>$value3)
echo $key3.'-'.$value3;
}
else
echo $key2.'-'.$value2;
}
}
else
echo $key.'-'.$value;
}
Check for the $key is "array or not" each time,if it is array then it will fo to for loop orelse it will echo it directly
Ok, I'm still struggling with my arrays... I have created a two-dimensional array and saved into a session to get results on another page: $_SESSION['myARRAY']
print_r($_SESSION['myARRAY']);
// will output:
Array ( [Car] => Array ( [0] => 1 [1] => 9 [2] => 0 )
[Truck] => Array ( [0] => 2 [1] => 10 [2] => 0 )
[Bus] => Array ( [0] => 1 [1] => 8 [2] => 2 ))
Now I need to output the data into the following format:
$xls->addRow(Array("Car",1,9,0));
$xls->addRow(Array("Truck",2,10,0));
$xls->addRow(Array("Bus",1,8,2));
I tried to do something like this:
foreach($_SESSION['myARRAY'] AS $key => $value) {
$arr[$key] = $key;
foreach($value AS $k => $v) {
$arr[$key] = $v;
}
$xls->addRow($arr[$key]);
}
but it did not really work. I think I'm close but not quite there...
$value is already an array. Now you only need to prepend the $key to it. You could use array_unshift:
foreach($_SESSION['myARRAY'] AS $key => $value) {
array_unshift($value, $key);
$xls->addRow($value);
}
Of course if you do this more than once, you should consider storing the consolidated array.
I'd probably use array_unshift as it seems like a more appropriate way to solve this problem, but you could also do it like:
foreach($_SESSION['myARRAY'] AS $key => $value) {
$xls->addRow(array_merge(array($key), $value));
}