开发者

Accessing Data From an Array With a Counter

开发者 https://www.devze.com 2023-04-05 23:33 出处:网络
I have an array structure like so: Array ( [0] => Array ( [term_id] => 22 [name] => Aaa Aa [slug] => aaa-aa

I have an array structure like so:

Array
(
    [0] => Array
        (
            [term_id] => 22
            [name] => Aaa Aa
            [slug] => aaa-aa
            [term_group] => 0
        开发者_Go百科    [term_taxonomy_id] => 22
            [taxonomy] => category
            [description] => 
            [parent] => 13
            [count] => 0
            [cat_ID] => 22
            [category_count] => 0
            [category_description] => 
            [cat_name] => Aaa Aa
            [category_nicename] => aaa-aa
            [category_parent] => 13
        )

    [1] => Array
        (
            [term_id] => 11
            [name] => adasdasda
            [slug] => adasdasda
            [term_group] => 0
            [term_taxonomy_id] => 11
            [taxonomy] => category
            [description] => asdsdad
            [parent] => 1
            [count] => 0
            [cat_ID] => 11
            [category_count] => 0
            [category_description] => asdsdad
            [cat_name] => adasdasda
            [category_nicename] => adasdasda
            [category_parent] => 1
        )
)

I was wondering how to go about easily looping through the data for output...

For example, I want to display all the names, descriptions and slugs.

I can't figure out how to display them all...

This is what I have, and it kind of works, however it's throwing and invalid argument.

    for ($i = 0; $i <= count($data); $i++) {
    foreach($data[$i] as $key => $value){
            echo "KEY: ".$key."<br />\n VALUE: ".$value."<br />\n"; 
        }
    }


No need to use the for loop, foreach will suffice. This way you won't have to deal with the out of boundary (index not set) warning.

$array = array(); // <- your data here
foreach ($array as $arr) {
    echo 'Name: ', $arr['name'], '<br />',
         'Description: ', $arr['description'], '<br />',
         'Slug: ', $arr['slug'], '<br />';
}


You get the warning because you are accessing an element of $data that does not exists. The boundary check in the for condition is one too high, count will return 2, but $data[2] is not set and can not be used within foreach then.

Instead do (Demo):

for ($i = 0; $i < count($data); $i++) { # $i < count, not $i <= count
    foreach($data[$i] as $key => $value){
        echo "KEY: ".$key."<br />\n VALUE: ".$value."<br />\n"; 
    }
}

Naturally it's easier to use an outer foreach as well:

foreach($data as $oneData)
{
    foreach($oneData as $key => $value)
    {
        echo "KEY: $key<br />\n VALUE: $value<br />\n"; 
    }
}

This basically does the same and you don't need to care about the counter variable.

The next thing is that you need to decide which of the data in question you would like to display. Instead of displaying all items, you can pick them selectively:

$displayKeys = array('name', 'description', 'slug');

foreach($data as $oneData)
{
    foreach($displayKeys as $key)
    {
        $value = $oneData[$key];
        echo "KEY: $key<br />\n VALUE: $value<br />\n"; 
    }
}

Then you might want to label the output:

$displayKeys = array('name' => 'Name', 'description' => 'Description', 'slug' => 'Slug');

foreach($data as $oneData)
{
    foreach($displayKeys as $key => $label)
    {
        $value = $oneData[$key];
        echo "$label: $value<br />\n"; 
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消