Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:34:59+00:00 2026-05-26T13:34:59+00:00

Question below. This is the solution I came up with based on Pixeler’s answer.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:35:00+00:00Added an answer on May 26, 2026 at 1:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question and my answer below are mainly in response to an area of
Author post-edit: Chosen solution (Original question remains below this box) SUMMARY: You SHOULD NOT
Hello dearest community, EDITED This is my solution, based on Devart answer. I slightly
This is with reference to the below question: Execute program from within a C
Background: This question relates to versions of Delphi below 2009 (ie without Unicode support
My question is sort of a follow on from this question below but I
Edit: I'm looking for solution for this question now also with other programming languages.
I have the below question as well as this time i have done some
The question is in the title I guess. This is the temporary solution I
I've created a question about this a few days . My solution is something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.