1

I have been working on this a while. I see multi-dimensional arrays in php are not that easy. Here is my code:

      while (list($key,$value) = each ($x))
        {
        Print "$key  => $value\n<br>\n";
        }

This works well to display the keys of the main array. what I get is :

visitors => Array 
actions => Array 
actions-average => Array 
time-average => Array 
pages-entrance => Array

What I want is the visitors and the value (number of visitors), value of actions, etc. I want to then save the value in Mysql. Some I will have to convert from a string to and int or date.

I need to list one more level deep. But I cannot see how to do this. --------------Added ----------- So what I have is an array of arrays. I need to step through each array.

4 Answers 4

4

did you try print_r ?

if you need more control over formatting then embedded loops as suggested by @Nick is the best option. Although it would be more natural and safer to use foreach loops rather than while.

foreach($x as $key => $value){
  foreach( $value as $key2 => $value2){
    print "$key $key2 => $value2\n<br />\n";
  }
}

see PHP manual: each , there is a "caution" frame.

EDIT 1 I update sample code above for 2 day array.

It seems your array has more than 2 dimension. Then you should use recursion.

function my_print_r($x,$header="")
{
  foreach($x as $key => $value){
    if(is_array($value))
      my_print_r($value,$header . $key .  " " );
    else
      print "$header $key2 => $value2\n<br />\n";
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thats closer. What I get is '2011-06-07 => Array 2011-06-07 => Array 2011-06-07 => Array 2011-06-07 => Array 2011-06-07 => Array 2011-06-07 => Array'
Btw I dont see how to use Print_ with this function. If I do print_r on the array ($x) it dumps everything to the screen. This is helpful to see that array is working, but id does not solve my problem.
I was just pointing you to print_r in case you were not aware of it. As @Bob_Gneu suggested, var_dump() is also good to know
I used this method. What was very confusing is some of the arrays had only 1 element and some had many. So it looked like there was a loop problem, but really there was one one count in the loop. Thank You.
1

Try loops like this code:

$arrA=array("a", "b", "c");
$arrB=array("x", "y", "z");
$x=array("visitors" => $arrA, "actions" => $arrB);
foreach($x as $key => $value)
{
   foreach($value as $v)   
      echo "$key  => $v<br>\n";
}

OUTPUT

visitors  => a<br> 
visitors  => b<br> 
visitors  => c<br> 
actions  => x<br> 
actions  => y<br> 
actions  => z<br

2 Comments

@Joe: Please see above output it has generated. If it still doesn't work for you, please post your complete code here and I will be happy to fix that.
this is my complete code. I have an ajax call that loads everything into a multidimensional array.
1

the best way is var_dump($arr);

<?php

var_dump($_SERVER);

?>

with output that includes types, string length, and will iterate over objects as well.

Since you want to iterate over an array, give foreach a try:

foreach ($arr as $el)
{
    // ... Work with each element (most useful for non associative arrays, or linear arrays)
}

// or

foreach ($arr as $key => $value)
{
    // ... Work with $key and $value pairs (most useful for hashes/associative arrays)
} 

2 Comments

Var dump does show me the entire array. But I need to se it one at a time to do a conversion from string to int. or from string to date. Then I can insert it into the database.
very interesting. I think this is the track I will need.
0

/ / . . . here, we take variable $company for nested array and in this variable we put 2 employee id and its called multi-dimensional array and we take $company variable to assign the associative array with keys so we print the output with keys

$company=[

$emp=[1,"neha","employee",12000,30000],

$emp1=[2,"moni","manager",12000],

];

$company=["first" => $emp,"second" => $emp1];

foreach($company as $key => $value) {

echo "$key ";

foreach($value as $v1){
     
    echo "$v1";
}

}

output :-

employee ID name designation salary bonus

first 1 neha employee 12000 30000

second 2 moni manager 12000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.