Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    Why won't include() work inside arrays?

    Trying to use an include() inside an array but I'm getting errors. Why is this?

    Thanks guys!

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it's because there is something wrong.

    Maybe post the code and the error you get because my psychic powers are letting me down at the moment.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Quote Originally Posted by raf
    it's because there is something wrong.

    Maybe post the code and the error you get because my psychic powers are letting me down at the moment.

    Swami says: <calling powers="mystic">Parse Error</calling> You are ending before beginning an ending...

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bcarl314
    Swami says: <calling powers="mystic">Parse Error</calling> You are ending before beginning an ending...
    Noone can proove anyhting ever truely ends ... so i fear there will be no fix possible (until the next Windows release) --> but can we trust on that

    So please give us the error you're getting and maybe we can recreat some kind of hope or temporarely equilibrium
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    ok..

    Here is my code..

    PHP Code:
    <?php

    $people 
    = array(

    include(
    "people.inc");

    );


    ?>
    Here is the error:

    Parse error: parse error, expecting `')'' in xxxxxxxxxxxxxxxxxxxxxxxx on line 5

    Cheers!

  • #6
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    wohoo, I was (er I mean Swami) was right! Parse Error!

    Anyway, if your trying to create an array of include files, I'd try something like this...

    PHP Code:
    $people = array("people.inc");
    include(
    $people[0]); 

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The parser expects variables or values to be put into an array. include is neither of both, so it throws an error. include is a language feature, and you just can't use it at that point in your script. The PHP language is defined this way and you can't get around that.

    If you tell us what you're actually trying to achieve, we can (hopefully) give you more detailed help.

    EDIT: Posts crossed. And that even when I'm a couple of time zones ahead of bcarl. Bah.
    De gustibus non est disputandum.

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks bcarl but that idea isn't going to work in my situation.

    Ok, what I'm trying to achieve here is to pull the array information from another file (several other arrays as this is a multi-dimensional array).

    Now you're probably thinking why don't I just put it in the code?

    Because I want to use a file to append extra arrays to the file & I need to break up the main array to add sub-arrays inside the main array otherwise they will get added after the main array would close.

    Now, I know this could all be handled with a database better but I'm just doing this as a learning experience.

  • #9
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    do you mean you want to have array $x , then make that array bigger by including array $y within it ?

    not that you can as Mordred explained but trying to work out exactly what you mean.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ok.. here is the code that I have inside "people.inc", this may help you better understand what I am trying to do..

    PHP Code:
            array("name" => "Bill",
                            
    "age" => 15,
                            
    "country" => "USA",
                            
    "email" => "rat@test.com"),

            array(
    "name" => "Harry",
                            
    "age" => 24,
                            
    "country" => "USA",
                            
    "email" => "rat2@test.com"),

            array(
    "name" => "Devon",
                            
    "age" => 32,
                            
    "country" => "Italy",
                            
    "email" => "devon@test.it"),

            array(
    "name" => "Brett",
                            
    "age" => 23,
                            
    "country" => "USA",
                            
    "email" => "ratrat@test.com"),

            array(
    "name" => "George",
                            
    "age" => 33,
                            
    "country" => "Germany",
                            
    "email" => "goo@test.de"),

            array(
    "name" => "Mary",
                            
    "age" => 35,
                            
    "country" => "USA",
                            
    "email" => "moo@test.com"
    As you can see there is several arrays inside there which I am trying to pull into people.php inside the main array.

    Doing it this way I can eaisly append a new array to the "people.inc" file & it will also be updated inside the main array inside people.php.

  • #11
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    In that case, why don't you just name the array in your include file???

    PHP Code:
    //include.inc
    $tmpArrayName //All the array stuff 
    Then just include it...

    PHP Code:
    include(include.inc);
    $people $tmpArrayName

  • #12
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    I can't put all the array code in the include file because then when I append to it the new sub-array will be outside the main array.

  • #13
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    err... so you wan't to merge the tw arrays? or append the array to another one?
    It doesn't matter if it's a uni-dimensional or multidimensiona array.
    Say your array inside the file you include looks just liek you posted. But inside a real array instead of a list of elements.So
    $includearray=(your elements come here);
    Then you can use
    PHP Code:
    $bla= array ('animalsounds'=> array('dog'=>'woefwoef''cat'=>'niauw'mordred=>'grrrr'));
    require(
    'include.php');
    $total array_merge($bla$includearray); 
    Or if the $includearray needs to be an element of the $bla:

    $bla[] = $includearray;


    But including the elements isn't gonnan work, because they are just include like that. As seperate arrays. Just like you would copy paste them insie your code. So you need to group them inside an array that you can then refer to.

    Maybe gve us an example of the endresult you have in mind + the relevant parts of the script you are using it in
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #14
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Yes... I'm trying to pull all the code in post #10 to where the include reference is in the below code:

    PHP Code:
    <?php 

    $people 
    = array( 

    include(
    "people.inc"); 

    ); 


    ?>
    You then can see the only way to reference this array is by using..

    $people[0]["name"]

    ..variables.

  • #15
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK. The elaborate approach then.

    If you want the result (your script (--> script.php) when it's getting parsed to look like
    PHP Code:
    <?php  
    echo 'I am using arrays here !';
    $people = array(array("name" => "Bill"
                            
    "age" => 15
                            
    "country" => "USA"
                            
    "email" => "rat@test.com"), 

            array(
    "name" => "Harry"
                            
    "age" => 24
                            
    "country" => "USA"
                            
    "email" => "rat2@test.com"), 

            array(
    "name" => "Devon"
                            
    "age" => 32
                            
    "country" => "Italy"
                            
    "email" => "devon@test.it"), 

            array(
    "name" => "Brett"
                            
    "age" => 23
                            
    "country" => "USA"
                            
    "email" => "ratrat@test.com"), 

            array(
    "name" => "George"
                            
    "age" => 33
                            
    "country" => "Germany"
                            
    "email" => "goo@test.de"), 

            array(
    "name" => "Mary"
                            
    "age" => 35
                            
    "country" => "USA"
                            
    "email" => "moo@test.com")  );  
    echo 
    '<br /> Very simple';
    ?>
    then you need to have this inside the included file (lets call it included.php)
    PHP Code:
    <?php  
    $people 
    = array(array("name" => "Bill"
                            
    "age" => 15
                            
    "country" => "USA"
                            
    "email" => "rat@test.com"), 

            array(
    "name" => "Harry"
                            
    "age" => 24
                            
    "country" => "USA"
                            
    "email" => "rat2@test.com"), 

            array(
    "name" => "Devon"
                            
    "age" => 32
                            
    "country" => "Italy"
                            
    "email" => "devon@test.it"), 

            array(
    "name" => "Brett"
                            
    "age" => 23
                            
    "country" => "USA"
                            
    "email" => "ratrat@test.com"), 

            array(
    "name" => "George"
                            
    "age" => 33
                            
    "country" => "Germany"
                            
    "email" => "goo@test.de"), 

            array(
    "name" => "Mary"
                            
    "age" => 35
                            
    "country" => "USA"
                            
    "email" => "moo@test.com")  );  
    ?>
    and inside the actual script, where you wan't to use this array, you'd have

    PHP Code:
    <?php
    echo 'I am using arrays here !';  
    require(
    'include.php');
    echo 
    '<br /> Very simple';
    ?>
    You see?
    you could then use it like if you copy-paste it, because that is all that include() and require() do. They simply paste the content inside the script and you can then refere to variables as if they were part of yout script.php
    Like
    PHP Code:
    <?php  
    require('include.php');
    echo 
    'The arrray i include looks like :<br />';
    print_r $people
    echo '<br /> and it\'s second element is:' $people[1];
    echo 
    '<br /><br />And here i includes this $people array inside another array:';
    $total = array ($people'yet another array'=> array('one'=>'test1''second'=>'lala'));
    print_r $total;
    ?>
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •