0

This is my array :

array (size=12)
  0 => 
    array (size=2)
      'mon' => string '2018-01-01 00:00:00' (length=19)
      'nb_argus' => string '29' (length=2)
  1 => 
    array (size=2)
      'mon' => string '2018-02-01 00:00:00' (length=19)
      'nb_argus' => string '21' (length=2)
  2 => 
    array (size=2)
      'mon' => string '2018-03-01 00:00:00' (length=19)
      'nb_argus' => string '34' (length=2)
  3 => 
    array (size=2)
      'mon' => string '2018-04-01 00:00:00' (length=19)
      'nb_argus' => string '18' (length=2)
  4 => 
    array (size=2)
      'mon' => string '2018-05-01 00:00:00' (length=19)
      'nb_argus' => string '25' (length=2)
  5 => 
    array (size=2)
      'mon' => string '2018-06-01 00:00:00' (length=19)
      'nb_argus' => string '17' (length=2)
  6 => 
    array (size=2)
      'mon' => string '2018-07-01 00:00:00' (length=19)
      'nb_argus' => string '23' (length=2)
  7 => 
    array (size=2)
      'mon' => string '2018-08-01 00:00:00' (length=19)
      'nb_argus' => string '8' (length=1)
  8 => 
    array (size=2)
      'mon' => string '2018-09-01 00:00:00' (length=19)
      'nb_argus' => string '14' (length=2)
  9 => 
    array (size=2)
      'mon' => string '2018-10-01 00:00:00' (length=19)
      'nb_argus' => string '0' (length=1)
  10 => 
    array (size=2)
      'mon' => string '2018-11-01 00:00:00' (length=19)
      'nb_argus' => string '0' (length=1)
  11 => 
    array (size=2)
      'mon' => string '2018-12-01 00:00:00' (length=19)
      'nb_argus' => string '0' (length=1)

I'm simply trying to display each of the 'nb_argus' values inside a foreach loop, but can't use the STRING to point at, i need to use an index, this is my code :

foreach ($array_12_months  as $key => $tab) {

        foreach($tab as $row2 => $tab2){

            $tab2 = array_values($tab2);
            echo $tab2[1] . " my complete nb_argus value <br/>";
        }
    }

It doesnt work, and instead of displaying 29 for example, it displays 2 !

instead of displaying 21 , it displays only one character : 2

I really can't find any solution, have tried plenty of things, i can't access to my nb_argus values, while i'm using array_values(), it doesnt work, please help

Is it because the nb_argus is a string that it is not working ? I can't find any solution.

1
  • Looks like you are using too many loops. Commented May 19, 2022 at 6:59

2 Answers 2

2

Use array_column and extract only the 'nb_argus' to a flat array and echo them.

$nb_argus = array_column($arr, 'nb_argus');
foreach($nb_argus as $nb){
    echo $nb . " ";
}

Or you can implode the array and echo them like:

$nb_argus = array_column($arr, 'nb_argus');
echo implode(" ", $nb_argus);
Sign up to request clarification or add additional context in comments.

2 Comments

No i cant use nb_argus i need to use an index instead, because nb_argus is not always the same
How does it change? Can you give a sample what it looks like if it changes?
-1

If you really need to access the index by number, instead of using the nb_argus key, then you can use:

foreach($array as $item) {
    $values = array_values($item);
    echo $values[1] . " my complete nb_argus value <br/>";
}

3 Comments

Yes thank you, there are 12 values per month, and i need to do a math operation on each of them, by 'gluing' another 12 months array ... ... I'm trying this now
'gluing' is not a math term I have heard of before. What are you actually trying to do? Also your data shows 12 values per year, not per month.
Instead of using echo, add all data to an array, then use some function like array_map or array_reduce

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.