How to print multidimensional array in php - php

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

Related

Problems with mutiple array in foreach loops

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

foreach loop for multidimensional array

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

php multidimensional array output blank

i'm trying to output data table with PHP multidimensional array, but i have problem with foreach loop it loops for only one index, it doesn't output next indexes, in my print_r output for $docs
Array
(
[docs] => Array
(
[0] => Array
(
[title] => Rew
[imgurl] => http://localhost/site/uploads/2012/07/print.jpg
[level_id] => Array
(
[0] => 2
[1] => 3
)
)
[1] => Array
(
[title] => Second
[imgurl] => http://localhost/site/uploads/2012/07/type.jpg
[level_id] => Array
(
[0] => 1
[1] => 3
)
)
)
)
in my php
$i =0;
foreach ($docs as $doc){
foreach($doc as $a_doc){
foreach($doc as $a_doc){
echo $doc[$i]['title'];
}
}
$i++;
}
but it doesn't give any output,i would REALLY appreciate if someone could give me some advice.
If the array containing the docs key only has a single element, you can do this:
foreach( $docs['docs'] as $doc ) {
echo $doc['title'];
}
If it should have more than one entry (besides docs), go for:
foreach( $docs as $entry ) {
foreach( $entry as $doc) {
echo $doc['title'];
}
}
Try this:
foreach($docs as $key => $doc){
echo $doc['title'];
}
Each $doc that is iterated is an array of doc attributes. The $key in the foreach() loop is the currently indexed key for the currently iterated element in the $docs array.

PHP: Adding a key-value pair to an array

I have an array that looks like this:
$array = Array ( [0] => Array ( [site_id] => 89193)
[1] => Array ( [site_id] => 89093)
[2] => Array ( [site_id] => 3059 )
)
What I want to do is to add a new pair to the array, how can I do so?
For example, next to site_id, I want to add the key [site_name] and its value.
foreach ($array as &$child) {
$child['site_name'] = $value;
}
or, without references:
foreach (array_keys($array) as $key) {
$array[$key]['site_name'] = $value;
}

Output two-dimensional array

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

Categories