-3

i got question How to insert value to a existing array, and my expetation of result is like :

Array
(
    [id] => 11
    [title] => Lorem ipsum
    [view] => 3445
)

But what i have did is :

<?php

$newVideo = [
    'id'=>'11',
    'title'=>'Lorem ipsum'            
];

$view = '3445';

$newVideo[] = ['view'=>$view];

print_r($newVideo);

and my result is :

Array
(
    [id] => 11
    [title] => Lorem ipsum
    [0] => Array
        (
            [view] => 3445
        )

)

UPDATE QUESTION: in my case i have complex foreach in each foreach i want to store the value to variable array like this :

$newVideo = array();
foreach ($videos['items'] as $v){
    $id = $v['snippet']['resourceId']['videoId'];
    $title = $v['snippet']['title'];

    $newVideo[] = [ "id" => $id ,"title" => $title];

the $newVideo is have ['id'] and ['title'] and i want to store ['view'] too, but i have to foreach function first to get the view data from ['id'] so i my code to foreach function to get a view is like :

$funcvid = array();
foreach($newVideo as $id){

    $ids = $id['id'];

    $vidview = YtJson('apilink', $ids, '');
    array_push($funcvid, $vidview);
}

and i got the video statistic so i can grab the view data from that function, so i foreach again to store view data value with this code :

foreach($funcvid as $f){
    foreach ($f['items'] as $h){
        $view = $h['statistics']['viewCount'];
}
    $newVideo['view'] = $view;
}

but for last foreach i can't store the value to $newVideo['view'], it won't lopping, and i got success if array is 2 dimensional, but my expetation is array is 1 dimensional

ryanpandu
  • 3
  • 2
  • `newVideo['view'] = $view;` – Barmar Jan 03 '20 at 02:27
  • This is basic syntax that you should find in any textbook or tutorial. – Barmar Jan 03 '20 at 02:28
  • okay, sorry for newbie question, but in my case i have complex foreach, that won't insert the value as i want, so i make sure to find the answer, FYI in my code is : foreach($funcvid as $f){ foreach ($f['items'] as $h){ $view = $h['statistics']['viewCount']; } $newVideo['view'] = $view; } and many foreach above the code so i want to store every foreach to new array associative – ryanpandu Jan 03 '20 at 03:24
  • Post the code in the question, not a comment. – Barmar Jan 03 '20 at 03:26
  • Looks like `$newVideo` should be a 2-dimensional array, since you need a different element corresponding to each item. – Barmar Jan 03 '20 at 03:27
  • ya, for 2 dimensional i have success to do it, but i want to store the value in 1 dimensional, so i can foreach the array variable again to get the final result – ryanpandu Jan 03 '20 at 03:31
  • I don't understand. Can you show a sample `$funcvid` and the desired `$newVideo`? – Barmar Jan 03 '20 at 03:32
  • i have edit my question, so my code is like in my question, please check it again – ryanpandu Jan 03 '20 at 03:40
  • You're overwritng `$view` each time through the inner loop. So you're just getting the `viewCount` from the last item. Is that really what you want? Maybe you want to sum them all? – Barmar Jan 03 '20 at 03:46
  • thankyou @Barmar you help me a lot – ryanpandu Jan 03 '20 at 03:54

1 Answers1

0

$newVideo is a 2-dimensional array. You need to index the first dimension to get to the associative inner array.

foreach($funcvid as $i => $f){
    foreach ($f['items'] as $h){
        $view = $h['statistics']['viewCount'];
    }
    $newVideo[$i]['view'] = $view;
}

But you don't need 3 different loops, you can do this all in one loop.

<?php
$newVideo = array();
foreach ($videos['items'] as $v){
    $id = $v['snippet']['resourceId']['videoId'];
    $title = $v['snippet']['title'];
    $vidview = YtJson('apilink', $ids, '');
    foreach ($vidview['items'] as $h) {
        $view = $h['statistics']['viewCount'];
    }
    $newVideo[] = [ "id" => $id ,"title" => $title, 'view' => $view];
}
Barmar
  • 741,623
  • 53
  • 500
  • 612