Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Setting an array property with a php variable?

  • 13-02-2013 01:58PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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