1

I have this code, but how can i add an extra item in the array? In this example i want to add an array with a 'slug'-title. This doesn't work.

$interviewresult = $db->query($interviewsql);
$interviewdata = $interviewresult->fetch_assoc();
$interviewdata['slug'] = $slug->create($interviewdata['title']);

while($interviewtrackerlist = $interviewresult->fetch_assoc()) {
    $interviewtracker_list[] = $interviewdata;
}

echo "<pre>".print_r($interviewtracker_list,1)."</pre>";

This gives a repeat of an item:

Array
(
    [0] => Array
        (
            [id] => 4041
            [title] => Interview: Blaat.nl
            [date_posted] => 2014-05-01 10:13:09
            [wanneer] => 01-05
            [slug] => interview-blaat-nl
        )

    [1] => Array
        (
            [id] => 4041
            [title] => Interview: Blaat.nl
            [date_posted] => 2014-05-01 10:13:09
            [wanneer] => 01-05
            [slug] => interview-blaat-nl
        )

    [2] => Array
        (
            [id] => 4041
            [title] => Interview: Blaat.nl
            [date_posted] => 2014-05-01 10:13:09
            [wanneer] => 01-05
            [slug] => interview-blaat-nl
        )

)

But i want to have a array like this:

Array
(
    [0] => Array
        (
            [id] => 4041
            [title] => Interview: Blaat.nl
            [date_posted] => 2016-05-01 10:13:09
            [wanneer] => 01-05
            [slug] => interview-blaat-nl
        )

    [1] => Array
        (
            [id] => 4042
            [title] => Interview: Yaddah Yaddah
            [date_posted] => 2016-05-10 22:53:49
            [wanneer] => 01-05
            [slug] => interview-yaddah-yaddah
        )

    [2] => Array
        (
            [id] => 4043
            [title] => Interview: Pete Puck
            [date_posted] => 2016-10-07 21:13:08
            [wanneer] => 01-05
            [slug] => interview-pete-puck
        )

)

How can i do this?

  • Dont use old mysqli! https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php Use pdo instead! – cb0 Apr 26 '16 at 14:38
  • @cb0 I don't understand, what's wrong with everybody? mysqlnd, mysqli and other connectors have right to live and be used. What changes if I use PDO instead of mysqlnd driver? is it about security for You to use pdo? - if developer writes ugly code, he will also write this ugly code with PDO (: p.s. I'm using frameworks so, I'm away of directly using mysql*, pdo drivers. but if I'll use, I'll use mysqli, not pdo, cuz pdo allocates much memory. – num8er Apr 26 '16 at 14:58
  • I the future i will use a framework. For now i'll use mysqli-OO, works perfect. – Jack Vonderberg Apr 26 '16 at 15:19
  • I posted a link explaining that. Just to cite the answer, those are most important to me: 1. Is officially deprecated as of PHP 5.5. 2. Has been removed entirely as of PHP 7.0 If you just develop under >5.5 it will be ok. If you don't write ugly code you could also replace "prepared statements" with native php function. If you dont need OO use it. If you don't want transactions or SP then you can of course use mysqli. I however would recommend pdo. It's more "future proof" in my eyes. – cb0 Apr 26 '16 at 15:25
  • @cb01 MySQLi is not deprecated, but the old MySQLnd driver! And yes, i'm using MySQLi! – Jack Vonderberg Apr 27 '16 at 08:10

2 Answers2

0

Look at this line inside your while loop,

$interviewtracker_list[] = $interviewdata;
                                  ^ look here

In each iteration of the loop you're pushing the same data to the array. Your code should be like this:

$interviewresult = $db->query($interviewsql);
$interviewtracker_list = array();
while($interviewdata = $interviewresult->fetch_assoc()){
    $interviewdata['slug'] = $slug->create($interviewdata['title']);
    $interviewtracker_list[] = $interviewdata;
}

// display $interviewtracker_list array
var_dump($interviewtracker_list);
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
0

Be simple.

Get result, iterate it, and change or add field inside of loop:

$interviewresult = $db->query($interviewsql);
while($interviewdata = $interviewresult->fetch_assoc()) {
    $interviewdata['slug'] = $slug->create($interviewdata['title']);
    $interviewtracker_list[] = $interviewdata;
}
num8er
  • 18,604
  • 3
  • 43
  • 57