1

i have a function which brings me some data from database and posts to my client. At the moment it sends data as a normal array(output is something like MyArray (a,b,c,d..)), but i want it to be MyArray (a(b,c,d)).. As like Castegory(Name, ID, Order..).. Can anyone please Help..Here is my code for already used version

public function get_button_template()
    {
        $this->q = "SELECT * FROM button_template ORDER BY order_number ASC";
        $this->r = mysql_query($this->q);
        if(mysql_num_rows($this->r) > 0)
        {        
            while($this->f = mysql_fetch_assoc($this->r))
            {
                $this->buttons[$this->i]["ID"] = $this->f["ID"];          
                $this->buttons[$this->i]["name"] = $this->f["button_name"];               
                $this->buttons[$this->i]["category"] = $this->f["button_category"];
                $this->buttons[$this->i]["order_number"] = $this->f["order_number"]; 
                $this->i++;
            }
        }
        return $this->buttons;
    }

EDIT A little öore detail please.. when i parsed this i get something like this:

"Vaule"( "Key1": "Value1" "Key2": "Value2" .

But what i want is omething like

 `"Category0":( "Key1": "Value1", "Key2": "Value2" . ) 

"Category1":( "Key1": "Value1", "Key2": "Value2" . )..`

How can i send a multidimentional array with key-value pairs?

5
  • 2
    just pass your array to json_encode. there's no practical limit to how deeply you nest your arrays. Commented May 14, 2012 at 9:04
  • @MarcB I think he wants to transform his array as well. Commented May 14, 2012 at 9:12
  • Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented May 15, 2012 at 13:05
  • @tereško I'm a beginner with PHP bro:) i will step as far as i can.. Now, can you please my question, from edit? Commented May 15, 2012 at 13:07
  • 1
    no , i will not please your question ... that sounds nasty Commented May 15, 2012 at 13:16

2 Answers 2

3

Just change how you build the array. If you want to group by category:

Edit

Code changed to create numbered categories using a name => key map.

$category_map = array(); $cat_nr = 0;
while ($this->f = mysql_fetch_assoc($this->r)) {
    if (!isset($category_map[$this->f["button_category"]])) {
        $category_key = "Category{$cat_nr}";
        $category_map[$this->f["button_category"]] = $category_key;
        ++$cat_nr;
    } else {
        $category_key = $category_map[$this->f["button_category"]];
    }
    $this->buttons[$category_key]][] = array(
        'category' => $this->f["button_category"],
        "ID" => $this->f["ID"],
        "name" => $this->f["button_name"],
        "order_number" => $this->f["order_number"],
    );
    $this->i++;
}

This produces an array like:

<category 1>: [
    (CatName1, Id1, name1, ordernr1)
    (CatName1, Id2, name2, ordernr2)
],
<category 2>: [
    (CatName2, Id3, name3, ordernr3)
    (CatName2, Id4, name4, ordernr4)
]

Then use json_encode on the end-result.

Btw, not sure why you store those buttons inside the object itself ;-)

Sign up to request clarification or add additional context in comments.

9 Comments

I'm making a Web Service application for iOS(ipad), i send buttons for each user from server, i will group the buttons on client side(ipad) with for categories. And i'm not so experienced with programming, i may have some problem with my design of objects, communication with server&polling data ect.. Thanks for your answer
when i parsed this i get something like this: "Vaule"( "Key1": "Value1" "Key2": "Value2" . But what i want is omething like "Category0":( "Key1": "Value1" "Key2": "Value2" . ) "Category1":( "Key1": "Value1" "Key2": "Value2" . ).. How can i send a multidimentional array with key-value pairs?
@ilis What's Vaule? Where does that come from? What's the input?
for example, i get the output of each inner loop(the elements of every category) as: 3 = ( { ID = 4; name = Yemekler; "order_number" = 12; } ); serf = ( { ID = 51; name = serf; "order_number" = 4; } ); Here "3" and "serf" are the names of categories.. i want to get them as: "Category0":"1"... "Category1":"serf"..
@ilis I don't understand what you want ... something like this? codepad.org/m16pqAS6
|
3

Use json_encode function. http://php.net/manual/en/function.json-encode.php

string json_encode ( mixed $value [, int $options = 0 ] )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.