Jump to content

Insert Array Within Array Problem


RedBoffin

Recommended Posts

Hello  :)

 

I am trying this...

 

$this->package = array( 'title'=>'','location'=>'','brand'=>'','itinery'=>array(),'options'=>array() );

 

...but when I try to do this later...

 

array_push( $this->packages['itinery'], $this->itinery );

 

...I get this error message: Warning: array_push() [function.array-push]: First argument should be an array in /home/c20esca/public_html/packagesParser.php on line 172

 

I thought that 'itinery'=>array() is creating a key:value pair 'itinery':empty array, that I can access by using $this->packages['itinery'] but clearly this is not the case.

 

Q1. How do I create an empty array called 'itinery' in the $this->packages array?

Q2. How do I then access this $this->packages array as an array within the array_push method?

 

Thanks for reading this - hope you can help?

Link to comment
https://forums.phpfreaks.com/topic/48459-insert-array-within-array-problem/
Share on other sites

it looks like you have a class with a variable called 'package' which is an array. unless you are creating a method within this class to nest another array inside it, you will not be able to  use $this->package to call upon the array. so i recommend you create a method inside the class to push a new array inside $this->package. something like this:

 

<?php
        class my_class(){
                var $package = array();
                var $itinery = array();

                function setPackate(){
                        $this->package = array(
                                                        'title' => '',
                                                        'location' => '',
                                                        'brand' => '',
                                                        'itinery' => array(),
                                                        'options' => array()
                                                );
                }

                function setItinery(){
                        $this->itinery = array(
                                                        'something' => '',
                                                        'somethingelse' => ''
                                                );
                }

                function push(){
                                array_push($this->package, $this-itinery);
                }
        }

        $obj = new myclass();

        $obj->setPackage();
        $obj->setItinery();
        $obj->push();
?>

Thanks for the reply but I don't understand how it solves my problem.

 

Unless I am misunderstanding something, array_push($this->package,$this->itinery) will simply put the $this->itinery array into the $this->package array.

 

I need to be able to put arrays into the $this->package['itinery'] array specifically.

 

Does this clarify?

yes that clarifies. so you'd just add another method to add an array into $this->package['itinery']. like this:

<?php
        class my_class()
        {
                var $package = array();
                var $itinery = array();

                function setPackage($package)
                {
                        $this->package = $package;
                }

                function setItinery($itinery)
                {
                        $this->itinery = $itinery;
                }

                function push()
                {
                        array_push($this->package['itinery'], $this-itinery);
                }
        }

        $packageArray = array(
                                        'title' => '',
                                        'location' => '',
                                        'brand' => '',
                                        'itinery' => array(),
                                        'options' => array()
                               );

        $itineryArray = array(
                                        'something' => '',
                                        'whatever' => '',
                                        'somethingelse' => '',
                               );

        $obj = new my_class();

        $obj->setPackage($packageArray)->setItinery($itineryArray)->push();
?>

 

but the important thing to take note of is that you're working with a class. there are many rules that do not apply to procedural programming when using object oriented programming. like the one i mentioned in my previous post. you must do it within the class if you are going to call it $this->anything.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.