3

Question below.

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)
?>

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.

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, 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.

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Jack Smith
  • 55
  • 1
  • 8
  • So you want to use the array just like numeric array but you also want to know what is number of example : `Grapes -> Purle` ? I would also recommend using `serialize` and `unserialize` if you want to keep such array in database. – Revenant Oct 29 '11 at 20:16
  • Yeah, I think so. I want to be able to return every word, "Apples", "Red" and "$2.95", if that makes sense. But I want to be able to return all those values only using numbers. – Jack Smith Oct 29 '11 at 20:21
  • I guess you will have to re-think what you want to do. Wouldn't be easier to just fetch the data from mysql with associated array and also with a numeric array. You would have both of them and you can do whatever you want. Other way around, you will have to create multiple arrays, compare them, try to find index numbers etc.. etc.. You will just end up writing coding a lot of lines. – Revenant Oct 29 '11 at 21:06
  • I actually just figured out how to do it, based off your code :D. I will share in a moment. – Jack Smith Oct 29 '11 at 21:13
  • I walked my dog and came back. Page didn't refresh somehow so I didn't see last changes in your question. Now it is more clear what you want to do and how you want to do it. I'm glad you solved your problem (: – Revenant Oct 29 '11 at 21:22
  • Thanks :). Wouldn't have been able to do it without you though! – Jack Smith Oct 29 '11 at 21:24
  • 1
    @Jack Smith: Please add your solution as an answer (not on top of your question). Then accept it (or the one that brought you to it to honor that users support). – hakre Oct 30 '11 at 12:42
  • In one hand u want string `echo $array[0]; //Outputs "Apples"`, in other hand you want array `echo $array[0][0];`. This can't be done in this way – Peter Oct 31 '11 at 07:24

2 Answers2

1

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 />";
}
?>
Jack Smith
  • 55
  • 1
  • 8
0

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.

Revenant
  • 2,942
  • 7
  • 30
  • 52
  • That would be perfect, but Apples could change to a number of different things. This is why I need it to be called by number. – Jack Smith Oct 29 '11 at 19:52
  • I'm updating my question to clarify. Sorry. – Jack Smith Oct 29 '11 at 19:55
  • That works very well, but the only issue I have left is how to get "Apples" as well, I mean.. I can call "Red" and "$2.95", but I can't call the name that I've given above that level, if you understand what I'm saying. – Jack Smith Oct 29 '11 at 20:05
  • I added shorter version of it. Sorry, didn't remember about `array_values()` at first. I couldn't understand exactly what you mean :( – Revenant Oct 29 '11 at 20:09
  • Sorry, it was my fault - I wasn't clear enough. I mean, the code you've got there is perfect for returning the values of the prices, but I also need to return the values of the labels for the prices and the name given to the groups of the arrays. – Jack Smith Oct 29 '11 at 20:14
  • I wrote a comment under your question. I'm still not sure if I understood you correctly. – Revenant Oct 29 '11 at 20:18