1

I have try for days, to make my query row as array but haven't success yet. I have 'cat.1','cat.2','cat.3','cat.4','cat.5','cat.1','cat.2','cat.3','cat.4','cat.5' in mysql row and want to make this as array so i will get 10 array rather than 1. Next i will use it to combine with another row that has same pattern. Look like this 0,0,1,1,0,0,1,1,1,0

while($row = $result->fetch_array(MYSQLI_ASSOC))  
  {  
    $keys = Array($row['listcat']);
    $values = Array($row['rightval']);
    $final_array [$row['testid']]= array_combine_($keys, $values);
  }

my function array_combine_ i get from here

function array_combine_($keys, $values){
    $result = array();

    foreach ($keys as $i => $k) {
     $result[$k][] = $values[$i];
     }

    array_walk($result, function(&$v){
     $v = (count($v) == 1) ? array_pop($v): $v;
     });

    return $result;
}

I always get one array rather than ten, so its can't be combine two arrays. What i want the result is like this

[8131] => Array ( 
  [cat.1] => Array ( [0] => 0 [1] => 0 ) 
  [cat.2] => Array ( [0] => 0 [1] => 1 ) 
  [cat.3] => Array ( [0] => 1 [1] => 1 ) 
  [cat.4] => Array ( [0] => 1 [1] => 1 ) 
  [cat.5] => Array ( [0] => 0 [1] => 0 ) 
)
[8173] => Array ( 
  [cat.1] => Array ( [0] => 0 [1] => 0 ) 
  [cat.2] => Array ( [0] => 0 [1] => 1 ) 
  [cat.3] => Array ( [0] => 1 [1] => 1 )
  [cat.4] => Array ( [0] => 1 [1] => 1 ) 
  [cat.5] => Array ( [0] => 0 [1] => 0 ) 
)
9
  • Where you take Array ( [0] => 0 [1] => 0 ), etc.? What I see is VERY bad SQL table schema Commented Jun 10, 2019 at 22:27
  • Can you var_dump a row and show the output of that for an example so we can tell for sure what's in the table? And could you show an example of the output you're currently getting instead of what you're trying to get? (Sorry, got that backward at first) Commented Jun 10, 2019 at 22:28
  • thanks for your comments, i use this for my a simply way.. Commented Jun 10, 2019 at 22:31
  • var_dump($keys) = "'KD.1','KD.2','KD.3','KD.4','KD.5','KD.1','KD.2','KD.3','KD.4','KD.5'" Commented Jun 10, 2019 at 22:36
  • 1
    I hope you meant 'column', not 'row'! :-( Commented Jun 10, 2019 at 22:39

1 Answer 1

1

Array($row['listkd']) will not parse the string as separate array elements. It just creates an array with a single element, which is the entire contents of the listkd column.

You can use str_getcsv() to parse the string. It will split it on the commas, and parse the quotes as delimiters around the values.

while($row = $result->fetch_array(MYSQLI_ASSOC))  
  {  
    $keys = str_getcsv($row['listkd'], ",", "'");
    $values = str_getcsv($row['rightval'], ",", "'");

    $final_array [$row['testnis']]= array_combine_($keys, $values);
  }
Sign up to request clarification or add additional context in comments.

2 Comments

He could use str_getcsv to parse the CSV strings - it'll strip all the quotes, separators and create an array all at once.
@ReddHerring Good idea, didn't think of that shortcut.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.