0

I have a array thats returned from a php query. result is like this.

okay, here is the for loop just after the select was done

  $row = $stmt->fetchAll();
  $arraySize = sizeof($row);

  for($x = 0; $x < $arraySize;$x++)
    {

        // Gets rid of all the numeric KEY values
        $max = sizeof($row[$x])/2;
        for($i = 0; $i < $max;$i++)
        {
            unset($row[$x][$i]);
        }

        $row[$x][] = false; // appends a boolean value for the tick box in datatables
        $row['select'] = $row['9'];
        unset($row['9']);

        $json[] = $row[$x];
    }
echo json_encode($json);
Array
    (
        [0] => Array
            (
                [uuid] => 365
                [name] => August_Kidsmeals.mp4
                [title] => 
                [path] => file://C:/wamp/www/chopperv2/video/library/
                [duration] => 00:30:00
                [uploaded_date] => 2014-07-22
                [uploaded_by] => admin
                [keyword] => 
                [comment] => 
            )

        [1] => Array
            (
                [uuid] => 368
                [name] => August_breakfast.mp4
                [title] => 
                [path] => file://C:/wamp/www/chopperv2/video/library/
                [duration] => 00:30:00
                [uploaded_date] => 2014-07-22
                [uploaded_by] => admin
                [keyword] => 
                [comment] => 
            )
    )

Now I want to push a extra element onto the array ( 'selected' ) before sending it via AJAX to a datatable.

I would like to custom name the KEY of the pushed element, but Im having difficulties doing this.

I have tried this. which works but its numeric.

$row[$x][] = true;

Its part of a For loop. This results in a 'appendation' of [9] => 1

I tried to rename the [9] with something like this

$row['select'] = $row['9'];
unset($row['9']);

but to no avail. Please can someone slap me in the right direction.

This should be the result

 [0] => Array
        (
            [uuid] => 365
            [name] => August_Kidsmeals.mp4
            [title] => 
            [path] => file://C:/wamp/www/chopperv2/video/library/
            [duration] => 00:30:00
            [uploaded_date] => 2014-07-22
            [uploaded_by] => admin
            [keyword] => 
            [comment] => 
            [select] => 1      //  <<------    I WANT THIS
        )
Kevin
  • 41,694
  • 12
  • 53
  • 70
morne
  • 4,035
  • 9
  • 50
  • 96
  • Show the desired result array – hindmost Jul 23 '14 at 08:41
  • i think you have to provide more code, it's hard to guess from here how you're doing it – Kevin Jul 23 '14 at 08:41
  • What is `$x`? Where are you getting this `selected` from? You need to narrow down your question more. – Darren Jul 23 '14 at 08:41
  • Where has the "rename key"-part gone though? – fast Jul 23 '14 at 08:43
  • So you just want to add an item to your current array? – Naruto Jul 23 '14 at 08:43
  • Yes, append a element with a custom KEY name. – morne Jul 23 '14 at 08:44
  • 2 ways to do it.. At the moment you get this array, I guess it comes from the DB.. Or when you retrieve the original array, just loop it and add it: http://stackoverflow.com/questions/2121548/how-to-push-both-value-and-key-into-array-php – Naruto Jul 23 '14 at 08:47
  • does this came from `PDO` select? use `PDO::FETCH_ASSOC` so that you dont need to unset numeric key values – Kevin Jul 23 '14 at 08:47
  • Btw: Have a look at PDO::FETCH_ASSOC, e.g. at: http://stackoverflow.com/questions/16846531/how-to-read-fetchpdofetch-assoc – fast Jul 23 '14 at 08:47

2 Answers2

1

A simple foreach should suffice and use & reference on each copy:

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // no need to unsetting numeric indexed values, all are in associative now
foreach($rows as &$row) {
    $row['selected'] = true;
}
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Okay, thanks but thats not the point. Yes is is a `PDO::FETCH_ASSOC`, but I want to append a custom `KEY => VALUE` at the end of each array. The `'SELECT'` key that I want to append is NOT returned with the query – morne Jul 23 '14 at 08:52
  • @mornenel wait did you even tried this? this appends it on the end of each array – Kevin Jul 23 '14 at 08:55
  • @mornenel sure no prob – Kevin Jul 23 '14 at 08:58
0

Just add the element to the desired row in your loop: (And maybe, if you olny want that selected row to the whole result)

if(9 == $x) {
 $row[$x]['select'] = 1;
 $result = $row[$x];
 break;
}
fast
  • 885
  • 7
  • 15