2

I am receiving this when i use print_r:

Array ( 
    [0] => 15 
    [1] => 15 
    [2] => 15 
    [3] => 15 
    [4] => 15 
    [5] => 16 
    [6] => 15 
    [7] => 15 
    [8] => 15 
    [9] => 14 
    ... and so on ... 
)

and I was wondering how I can get an array of these second values (i.e.):

$newArray = array(15,15,15,15,15,16,15,15,15,14, ... );

I have tried using array_values but to no avail!

As a background, I got these results from a single column in my database and am now trying to plot them using HighRoller/HighCharts.

Thanks for the help!

3 Answers 3

3

You don't have an array of arrays. You have an array of values with numeric indexes. Unless I am totally misunderstanding your question ....

Array ( 
    [0] => 15 
    [1] => 15 
    [2] => 15 
    [3] => 15 
    [4] => 15 
    [5] => 16 
    [6] => 15 
    [7] => 15 
    [8] => 15 
    [9] => 14 
    ... and so on ... 
)

This means your array at index 0 has value 15 and so on.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much for the help - silly misunderstanding on my behalf and was confused by the print_r output
Personally I like to use var_export over print_r since var_export will always print whatever you give it exactly. It's great for dealing with weak typed objects like bools/ints/strings as well as arrays and objects.
0

the "second values" are the values of the array... the square bracket numbers are the indexes. you simply access them with $array_name[0] or $array_name[5] or foreach($array_name as $idx => $val) echo($val);

Comments

0

As others have said, what you are showing is not a multi-dimensional array.

A multi-dimensional array would look something like this when var_dumped

array(3) {
  [0] =>
  array(1) {
    [0] =>
    int(15)
  }
  [1] =>
  array(1) {
    [0] =>
    int(15)
  }
  [2] =>
  array(1) {
    [0] =>
    int(15)
  }
}

To answer the question even though it doesn't look like what you have, for each level of a multi-dimensional array you can use an embedded foreach() loop.

<?php

$myArray = [
    [15],
    [15],
    [15],

];

var_dump($myArray);//outputs above example

foreach($myArray as $arr) {
    foreach($arr as $val) {
        echo $val;//outputs the value of each array inside the outer array
    }
}

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.