0

I am fetching data from my DB and they look like this:

[{"0":"1","key-1":"1","1":"1","key-2":"1","2":"1","key-3":"1","3":"1","key-4":"1"}] 

where the key-1 are the name of the column. (I only have one entry so).

I want to extract only the column values and save them into a new array that will output like this:

{"key-1":"1","key-2":"1","key-3":"1","key-4":"1"} 

I want it to look exactly like this and not : [{"key-1":"1","key-2":"1","key-3":"1","key-4":"1"}]

I tried this:

$cart["key-1"]=$output["key-1"];

where $output is the outcome of the DB that shown first (the one with []).

and the $cart is the new array I want.

Both are declared as:

$cart=array();
$output=array();

and $output[]=$row where row is the result of the DB fetch. How to do it?

3 Answers 3

1

Here's one way to do it, I've substituted the database row for a string here, and made use of json_decode() and json_encode()

$data = '[{"0":"1","key-1":"1","1":"1","key-2":"1","2":"1","key-3":"1","3":"1","key-4":"1"}]';

// convert to an array
$data = json_decode($data, true);

// create new array here
$cart = array();

for ($i = 0; $i < count($data); $i++)
{
    foreach ($data[$i] as $k => $v)
    {
        if (strpos($k, 'key') !== FALSE)
        {
            $cart[$k] = $v;
        }
    }
}

echo $cart['key-1'] . '<br/>';

echo json_encode($cart);

Output:

1
{"key-1":"1","key-2":"1","key-3":"1","key-4":"1"}
Sign up to request clarification or add additional context in comments.

Comments

0

This is a very chaotically asked question. From what I gathered you want maybe this..?

$cart=array();
foreach ($output as $index=>$value){
  if stripos($index,"key-"){
    cart[$index]=$value;
  }
}

2 Comments

It is not chaotically at all. I want to get only the values from the column and store them in a new array. What is complicated in this?? I will try your answer.
Ok, my bad. I didn't realize it was JSON and assumed it was just an array. Dale's answer seems to be correct.
0

Use mysql_fetch_assoc() to get only column names ;)

while ($row = mysql_fetch_assoc($result_of_query))
{
    echo $row['key-1'];
    echo $row['key-2'];
}

2 Comments

Ok thanks with that. I also want my ooutput not to have this [] in front of it. because i need that output to be in that format. ( start with {}). I know almost nothing about php so I am unaware of what is the difference between [] and {} but I need it in that format({}).
You return a json from db? I mean, I understood your question like this: You tried to fetch some rows from DB, and they came in an array with both numbered(indexed) arrays and column-named key-value paired arrays. So my code is a solution to the fetching "problem" like this... If you have a json, then you just have to call json_encode() on $output_array[0] and then loop through its returned value, and get only the column names.

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.