Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

learner difficulties with php arrays

Can anyone tell me what I am doing wrong with the array definition below?
When I use the "print_r" at the end, it only gives the correct result for the first level.
<?
$layout=array("Copyright","About_the_Author","Credits","Preface",
"Acknowledgments","Contents","Appendices","Index");

$layout["Copyright"]=array("Liability","Warranty","Disclaimer","Trademarks");
$layout["About_the_Author"]=array("Identity","Address","Qualifications");
$layout["Credits"]=array();
$layout["Preface"]=array();
$layout["Acknowledgments"]=array();
$layout["Contents"]=array("Tome 1");
$layout["Appendices"]=array("Appendix A","Appendix B","Appendix C");
$layout["Index"]=array();

$layout["Copyright"]["Liability"]=array();
$layout["Copyright"]["Warranty"]=array();
$layout["Copyright"]["Disclaimer"]=array();
$layout["Copyright"]["Trademarks"]=array();

$layout["About_the_Author"]["Identity"]=array();
$layout["About_the_Author"]["Address"]=array();
$layout["About_the_Author"]["Qualifications"]=array();

$layout["Contents"]["Tome 1"]=array("Part I","Part II","Part III","Part IV","Part V");

$layout["Appendices"]["Appendix A"]=array();
$layout["Appendices"]["Appendix B"]=array();
$layout["Appendices"]["Appendix C"]=array();

$layout["Contents"]["Tome 1"]["Part I"]=array("Chapter 1","Chapter 2","Chapter 3","Chapter 4");
$layout["Contents"]["Tome 1"]["Part II"]=array("Chapter 5","Chapter 6","Chapter 7","Chapter 8","Chapter 9");
$layout["Contents"]["Tome 1"]["Part III"]=array("Chapter 10","Chapter 11","Chapter 12","Chapter 13","Chapter 14");
$layout["Contents"]["Tome 1"]["Part IV"]=array("Chapter 15","Chapter 16","Chapter 17","Chapter 18","Chapter 19","Chapter 20","Chapter 21","Chapter 22","Chapter 23","Chapter 24","Chapter 25","Chapter 26");
$layout["Contents"]["Tome 1"]["Part V"]=array("Chapter 27","Chapter 28");
print_r ($layout[5][0]);
?>
0
rblampain
Asked:
rblampain
  • 2
1 Solution
 
Diablo84Commented:
It looks like you are confusing values with indexes, i presume you are trying to do something like this:

<?
$layout["Copyright"]=array("Liability" => array(),"Warranty" => array(),"Disclaimer" => array(),"Trademarks" => array());
$layout["About_the_Author"]=array("Identity" => array(),"Address" => array(),"Qualifications" => array());
$layout["Credits"]=array();
$layout["Preface"]=array();
$layout["Acknowledgments"]=array();
$layout["Contents"]=array("Tome 1" => array("Part I" => array(),"Part II" => array(),"Part III" => array(),"Part IV" => array(),"Part V" => array()));
$layout["Appendices"]=array("Appendix A" => array(),"Appendix B" => array(),"Appendix C" => array());
$layout["Index"]=array();

$layout["Contents"]["Tome 1"]["Part I"]=array("Chapter 1","Chapter 2","Chapter 3","Chapter 4");
$layout["Contents"]["Tome 1"]["Part II"]=array("Chapter 5","Chapter 6","Chapter 7","Chapter 8","Chapter 9");
$layout["Contents"]["Tome 1"]["Part III"]=array("Chapter 10","Chapter 11","Chapter 12","Chapter 13","Chapter 14");
$layout["Contents"]["Tome 1"]["Part IV"]=array("Chapter 15","Chapter 16","Chapter 17","Chapter 18","Chapter 19","Chapter 20","Chapter 21","Chapter 22","Chapter 23","Chapter 24","Chapter 25","Chapter 26");
$layout["Contents"]["Tome 1"]["Part V"]=array("Chapter 27","Chapter 28");

echo '<pre>';
print_r ($layout);
echo '</pre>';
?>

Perhaps not the most elegant use of a multi-dimensional array.

If you wanted to maintain the initial numeric index that you added in your code you could add [] befoe the primary index in each case, ie: replace $layout["Copyright"] with $layout[]["Copyright"] although there should not be a need for it as you can just reference the associative index, eg: print_r($layout["Contents"]);

Diablo84
0
 
Diablo84Commented:
To clear up the confusion between values and indexes, taking a small piece of your original code...

$layout["Copyright"]=array("Liability","Warranty","Disclaimer","Trademarks");

in the above you have assigned 4 values to your array, as they do not have keys assigned manually they will automatically have numeric indexes starting at 0 and incrementing by 1 for each item.

Consider the following:

$layout["Copyright"]=array("Liability","Warranty","Disclaimer","Trademarks");

echo $layout["Copyright"][0].'<br>'; //would output Liability
$layout["Copyright"][0]=array(); //assigns a new array to the index where the value Liability is currently stored
echo $layout["Copyright"][0].'<br>'; //outputs Array as we have an array here now

$layout["Copyright"]["Liability"] = array(); //this would in fact add a new index, 'Liability' which is assigned a new array

To use an associative index you need to use this syntax in your array "key" => "value" for example:

$layout["Copyright"]=array("Liability" => array());

$layout["Copyright"]["Liability"][] = "a value"; //assigned a value to the array assigned to the Liability array
$layout["Copyright"]["Liability"][3] = "another value"; //assigned another value to a specific numeric index of the array
$layout["Copyright"]["Liability"]["indexname"] = "a third value"; //assigned a value to the array with an associative index

print_r($layout);

The output of the above will be:

Array
(
    [Copyright] => Array
        (
            [Liability] => Array
                (
                    [0] => a value
                    [3] => another value
                    [indexname] => a third value
                )

        )

)

Multi dimensional arrays can be confusing if you do not manage the structure well, make good use of print_r($yourarray); to monitor it with the human friendly output.

Diablo84


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now