Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Setting an array property with a php variable?

Options
  • 13-02-2013 2:58pm
    #1
    Registered Users Posts: 4,946 ✭✭✭


    hey

    I've shortened this down to the minimum. When ever I set a variable and I try to send it into an array for a search the search comes up with nothing, but if i replace $var1 inside the array with '1' I get a result.

    Any Ideas? I'm under pressure timewise and would really appreciate any feedback!
    $var1 = 1;
    $theArray= array('valueToBeSet'   =>  array('whatever' => $var1));
    

    Thanks!


Comments

  • Registered Users Posts: 2,793 ✭✭✭oeb


    red_ice wrote: »
    hey

    I've shortened this down to the minimum. When ever I set a variable and I try to send it into an array for a search the search comes up with nothing, but if i replace $var1 inside the array with '1' I get a result.

    Any Ideas? I'm under pressure timewise and would really appreciate any feedback!
    $var1 = 1;
    $theArray= array('valueToBeSet'   =>  array('whatever' => $var1));
    
    Thanks!


    The question is a little vague. That returns exactly as expected

    [PHP]<?php
    $var1 = 1;
    $theArray= array('valueToBeSet' => array('whatever' => $var1));

    var_dump($theArray);
    ?>[/PHP]
    returns
    [SIZE=2]array   
    [SIZE=2]  [/SIZE]'valueToBeSet' =>      array 
    [SIZE=2]      [/SIZE]'whatever' => int 1[/SIZE]
    
    as expected.

    Can you post an example of exactly what's failing for you?


  • Registered Users Posts: 4,946 ✭✭✭red_ice


    oeb wrote: »
    Can you post an example of exactly what's failing for you?

    I was trying to write the whole array string into a single variable. It was echoing out fine, but just not searching properly.

    So
    $var1 = 'value1' => $var1;
    $var2 = 'value2' => $var2;
    $var3 = 'value3' => $var3;
    
    $search = $var1 . $var2 . var3;
    
    array ($search)
    

    echo $search would output
    'value1' => '1', 'value2' => '2', 'value3' => '3'
    

    but it wouldnt search when I ran the code.

    Thats it in a nutshell. I've simplified it, and have it working with no problems, I just wanted to do it that way so I could refine searches etc.


  • Subscribers Posts: 1,911 ✭✭✭Draco


    You can't do that. You need to do the following:
    $search = array( 'value1' => $var1, 'value2' => $var2, 'value3' => $var3);
    

    In your code you're just creating a string rather an array.


Advertisement