获取数据从多级阵列 [英] Getting Data From Multi-level Array
问题描述
下面的问题。
这是我想出了基于Pixeler的回答解决方案。
This is the solution I came up with based on Pixeler's answer.
<?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)
?>
编辑:的想法是,我可以返回第一级(苹果,橘子,葡萄),第二级(红,绿,肚脐,紫,绿)和第三级($ 2.95 $ 2.45 $ 4.95拨打一个号码$ 3.75 $ 3.25)。
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.
例如,我想要做这样的事情:
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
}
?>
编辑:重要的是要注意,我无法用言语来调用数据。我必须使用数字,即[0]调用数组中的第一个项目,因为苹果可以根据我从我的数据库加载哪些行数据的变化。换句话说......苹果实际上可以变成是书籍和红色可能会变成是这本书的名字=>价格
It is important to note that I cannot use words to call the data. I must use numbers, i.e. [0] to call the first item in the array, because Apples could change depending on what row of data I load from my database. 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.
我也看了json_de code和序列化/反序列化,我觉得我让他们从简单的介绍一下基本的想法...但我想我的主要问题是了解如何调用上述数据$ p $在我的例子psented。任何帮助将是真正伟大的。
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. Any help would be really great.
推荐答案
这件怎么样?
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?
第一种方式
$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>';
第二种方式
$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.
这篇关于获取数据从多级阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!