1

I have an array like this:

Array
(
    [47] => Array
        (
            [name1] => 
        )

    [43] => Array
        (
            [name2] => 
        )

    [53] => Array
        (
            [name3] => selected
        )

    [50] => Array
        (
            [name4] => 
        )

    [51] => Array
        (
            [name5] => 
        )

    [37] => Array
        (
            [name6] => 
        )

)

and I want to show the value of name1, name2, name3, name4, name5, name6. I tried with:

for($i = 0; $i < 6; $i++){
    echo $array_object[$i] . "<br/>";
}

but it doesn't work. How can I fix it? Thanks!

2
  • Did you try foreach? Commented Jun 20, 2014 at 8:44
  • 1
    1). You have an array in an array. 2). Your indices are not 0-6. Commented Jun 20, 2014 at 8:45

5 Answers 5

2

You can use the array_keys function to get the indexes and then use them numerically:

$keys=array_keys($array_object);
for($i = 0; $i < 6; $i++){
    echo $array_object[$keys[$i]][{'name'.($i+1)}]."<br/>";
}

This will allow you to use index [0] even though it refers to index [47] based on your example data in the question.

While I haven't bothered to check if your data will always contain at least 6 entries (again based on the example code you posted) but if your loop exceeds the number of entries in your array you will get an undefined index error unless you check it first.

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

Comments

0

If your array is not sorted you need to create a (really long and bad) loop.

foreach($array_object as $subArray){
   foreach ($subArray as $elem){
      echo ($elem);
   }
}

Please consider to simplify your array because with huge amount of data, it's going to be so long.

Comments

0
foreach ($array_object AS $entry) {
    foreach($entry AS $key=>$val) {
        echo $val.'<br />';
    }
}

Comments

0

Try with array_values is simple:

$array_object= array_values($array_object);

for($i = 0; $i < count($array_object); $i++)
{
    echo $array_object[$i]['name']."<br/>";
}

Comments

0

use one of this examples:

foreach($elements AS $element) {
    foreach($element AS $key=>$value) {
        if(!preg_match('/^name[0-9]+/s', $key)) // match "nameNUM" style text
            continue;
        echo $value.'<br/>';
    }
}

or

foreach($elements AS $element) {
    for($i=1; $i<=6; $i++) {
        if(!isset($element['name'.$i])) 
            continue;
        echo $value.'<br/>';
    }
}

or

$accepted_values = array('name1','name2','name3','name4','name5','name6');
foreach($elements AS $element) {
    foreach($element AS $key=>$value) {
        if(!in_array($key, $accepted_values)) 
            continue;
        echo $value.'<br/>';
    }
}

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.