简体   繁体   English

从多级数组中获取数据

[英]Getting Data From Multi-level Array

Question below. 问题如下。

This is the solution I came up with based on Pixeler's answer. 这是我根据Pixeler的答案提出的解决方案。

<?php
$i = 0;
foreach ($array as $k1 => $v1) {
        if (is_array($v1)) {
            echo $k1."<br />";
                foreach ($v1 as $k2 => $v2) {
                    echo "--".$k2."<br />----".$v2."<br />";
                }
            $i++;
        }
        echo "<br /<br />";
}
?>

<?php
$array = array( "Apples" => array("Red" => "$2.95", "Green" => "$2.45"),
                "Oranges" => array("Navel" => "$4.95"),
                "Grapes" => array("Purple" => "$3.75", "Green" => "$3.25")
              );
?>

Is it possible to take what's above and get the text for each value by calling a number? 是否可以通过调用数字来获取上述内容并获取每个值的文本? How could I do something like this?... 我怎么能这样做?...

<?php
echo $array[0];
//Outputs "Apples"

echo $array[0][0];
//Outputs "Red"

echo $array[0][0][0];
//Outputs "$2.95"

echo $array[3][2][1];
//Outputs "$3.25" (Grapes > Green > Price)
?>

EDIT: The idea is that I can return the first level (Apples, Oranges, Grapes), second level (Red, Green, Navel, Purple, Green), and third level ($2.95, $2.45, $4.95, $3.75, $3.25) by calling a number. 编辑:我的想法是,我可以通过调用返回第一级(苹果,橘子,葡萄),第二级(红色,绿色,肚脐,紫色,绿色)和第三级($ 2.95,$ 2.45,$ 4.95,$ 3.75,$ 3.25)一个号码。

For example, I want to do something like this: 例如,我想做这样的事情:

<?php

count($array); //returns 3 (Apples, Oranges, Grapes)

//Do some for/foreach function that will produce the following:

//Apples
//->Red: $2.95
//->Green: $2.45
//Oranges
//->Navel: $4.95
//Grapes
//Purple: $3.75
//Green: $3.25

//I'm hoping to structure the code like this:

foreach($i = 0; $i =< count($array); $i++){
    //echo title of array (Apples)
    //echo each child key and it's value (Red: $2.95; Green: $2.45)

    foreach($secondcounter = 0; $secondcounter =< count($array[$i]); $secondcounter++){
        echo array_key($array[$i][$secondcounter]) . ": " .$array[$i][$secondcounter];
        //Note: I don't actually know if array_key does the trick or not, it's meant to return "Red" in this case, while the non array_key()'d is meant to return the price of red apples, $2.95
}

?>

EDIT: It is important to note that I cannot use words to call the data. 编辑:重要的是要注意我不能使用单词来调用数据。 I must use numbers, ie [0] to call the first item in the array, because Apples could change depending on what row of data I load from my database. 我必须使用数字,即[0]来调用数组中的第一项,因为苹果可能会根据我从数据库加载的数据行而改变。 In other words... Apples could actually turn out to be Books, and Red could turn out to be the name of the book => price. 换句话说......苹果实际上可以变成书籍,而红色可能会成为书籍的价格=>价格。

I'm intending on using serialize/unserialize to store and retrieve the data from the database, although I'm not overly familiar with these functions, I had a brief look at them and they seem reasonably easy to use. 我打算使用序列化/反序列化来存储和检索数据库中的数据,虽然我对这些函数并不太熟悉,但我简单地看了一下它们,它们似乎相当容易使用。


I've been researching for hours but I cant find anything. 我已经研究了几个小时,但我找不到任何东西。 It is vital that I am able to call the data by numbers, not text. 至关重要的是,我能够通过数字而不是文本来调用数据。 So $array["Apples"] is unacceptable. 所以$ array [“Apples”]是不可接受的。

I have also looked at json_decode and serialize/unserialize, and I think I get the basic idea of them from a brief look... but I think my main issue is understanding how to call the above data as presented in my example. 我也看了json_decode和序列化/反序列化,我想我从简短的看看得到了它们的基本概念......但我认为我的主要问题是理解如何调用我的例子中提到的上述数据。 Any help would be really great. 任何帮助都会非常棒。

This is the solution I came up with based on @Pixeler's answer. 这是我根据@ Pixeler的答案提出的解决方案。

<?php
$i = 0;
foreach ($array as $k1 => $v1) {
        if (is_array($v1)) {
            echo $k1."<br />";
                foreach ($v1 as $k2 => $v2) {
                    echo "--".$k2."<br />----".$v2."<br />";
                }
            $i++;
        }
        echo "<br /<br />";
}
?>

What about this one? 这个如何?

array_search("Apples",array_keys($array));

If you want to loop your array one way or another you might have to create new array, loop through your old array and create new numeric array array? 如果你想以这种或那种方式循环你的数组,你可能需要创建新数组,循环遍历旧数组并创建新的数值数组数组?

FIRST WAY 第一种方式


$array = array( "Apples" => array("Red" => "$2.95", "Green" => "$2.45"),
                "Oranges" => array("Navel" => "$4.95"),
                "Grapes" => array("Purple" => "$3.75", "Green" => "$3.25")
              );

$newArray = array();
foreach ($array as $value) {
   $newArray[] = (is_array($value)) ?  array_values($value) : $value;
}

echo '<pre>';
var_dump($newArray);
echo '</pre>';

SECOND WAY 第二种方式


$array = array( "Apples" => array("Red" => "$2.95", "Green" => "$2.45"),
                "Oranges" => array("Navel" => "$4.95"),
                "Grapes" => array("Purple" => "$3.75", "Green" => "$3.25")
              );

$newArray = array();
$i = 0;
foreach ($array as $key => $value) {
 if (is_array($value)) {
   $newArray[$i] = array();
  foreach ($value as $k => $v) {
    $newArray[$i][] = $v;
  }
  $i++;
 }
}

echo '<pre>';
var_dump($newArray);
echo '</pre>';

echo $newArray[0][0];

Obviously I will recommend first way. 显然我会推荐第一种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM