Search

Reinhart Previano K.

Do you love to Ctrl-K, Ctrl-/, or / ? Now you can do three of them (>_ )!

No results so far...

20 October 2021

Menambahkan array baru pada object array

Reinhart Previano Koentjoro's profile picture

Reinhart Previano Koentjoro (@reinhart)

Published on Q&A #kotakode #laravel #php

Share Copy Link Print PDF Embed Share to Email Share to SMS Yahoo! Share to Yahoo! Mail Mastodon Share to Mastodon Share to KakaoStory Messenger Share to Messenger Pocket Share to Pocket Flipboard Share to Flipboard Pinterest Share to Pinterest Reddit Share to Reddit Y Combinator Share to Hacker News Odnoklassniki Share to Odnoklassniki Blogger Share to Blogger Pleroma Share to Pleroma Share to Friendica Share to KakaoTalk 1Artboard 1 copy 2 Share to Snapchat Xing Share to Xing Share to Misskey LINE Share to LINE Evernote Share to Evernote WhatsApp Share to WhatsApp LiveJournal Share to Livejournal Diaspora Share to Diaspora Share to Gmail Threads Share to Threads Threema Share to Threema Share to X Tumblr Share to Tumblr Buffer Share to Buffer LinkedIn Share to LinkedIn Mail.Ru Share to mail.ru VK Share to VKontakte Trello Share to Trello Facebook Share to Facebook Bluesky Share to Bluesky Skype Share to Skype Hatena Bookmark Share to Hatena Bookmark! Share via MastodonShare Telegram Share to Telegram WordPress Share to WordPress.com

Embed

This website supports oEmbed. To quickly use oEmbed, just copy this site's link to your oEmbed-supported apps and websites like WordPress.

Alternatively, copy and paste the HTML code below to embed this post in your website.

($_ )! We have made this thing responsive, but recommend at least 512x512 pixels for best results.
<iframe src="https://reinhart1010.id/blog/2021/10/20/menambahkan-array-baru-pada-object-array?embed" height="512" width="512" style="border:none;"><a href="{{ $canonical }}">https://reinhart1010.id/blog/2021/10/20/menambahkan-array-baru-pada-object-array</a></iframe>
Preview

Artikel ini merupakan jawaban saya terhadap salah satu pertanyaan pada situs Kotakode.com, sebuah forum dan komunitas para developer di Indonesia. Lihat pertanyaan asli

Pertanyaan

Saya punya list data berdasarkan tahun, dimana list data tersebut dicek apakah list data tersebut setiap tahun ada datanya atau tidak… jika list data tersebut tidak ada data.nya, maka tambahkan object baru… ini codingannya saya :

$data = [
    [
        'hak' => '12',
        'tambahan' => '6',
        'sisa' => '12',
        'tahun' => '2019'
    ],
    [
        'hak' => '12',
        'tambahan' => '6',
        'sisa' => '12',
        'tahun' => '2021'
    ]
];
$tahun = [
    '2019',
    '2020',
    '2021'
];
for($i=0;$i<count($tahun);$i++){
    $tahun == $tahun[$i];
    $filterData = array_filter($data, function($entry) {
        global $tahun;
        return $entry['tahun'] == $tahun;
    } );
    $newdata = [
        'hak' => '12',
        'tambahan' => '0',
        'sisa' => '0',
        'tahun' => $tahun
    ];
    if(count($filterData)==0){
        $data = array_push($newdata);
    }
}
return $data;

Hasil yang diinginkan seperti ini:

[ { hak: '12', tambahan: '6', sisa: '12', tahun: '2019' },
  { hak: '12', tambahan: '0', sisa: '0', tahun: '2020' },
  { hak: '12', tambahan: '6', sisa: '12', tahun: '2021' } ]

Tetapi setelah saya jalankan codinganya, muncul error seperti ini : array_filter() expects parameter 1 to be array, null given


Jawaban

Halo, kalau saya lihat kamu salah menggunakan array_push():

$data = array_push($newdata);

cara yang benar untuk memakainya adalah

array_push($data, $newdata);

karena fungsi array_push() akan mengembalikan return value berupa jumlah isi di dalam array yang sudah dimerge, bukan array yang sudah digabungkan tersebut. Makanya muncul error itu.

Share Copy Link Print PDF Embed Share to Email Share to SMS Yahoo! Share to Yahoo! Mail Mastodon Share to Mastodon Share to KakaoStory Messenger Share to Messenger Pocket Share to Pocket Flipboard Share to Flipboard Pinterest Share to Pinterest Reddit Share to Reddit Y Combinator Share to Hacker News Odnoklassniki Share to Odnoklassniki Blogger Share to Blogger Pleroma Share to Pleroma Share to Friendica Share to KakaoTalk 1Artboard 1 copy 2 Share to Snapchat Xing Share to Xing Share to Misskey LINE Share to LINE Evernote Share to Evernote WhatsApp Share to WhatsApp LiveJournal Share to Livejournal Diaspora Share to Diaspora Share to Gmail Threads Share to Threads Threema Share to Threema Share to X Tumblr Share to Tumblr Buffer Share to Buffer LinkedIn Share to LinkedIn Mail.Ru Share to mail.ru VK Share to VKontakte Trello Share to Trello Facebook Share to Facebook Bluesky Share to Bluesky Skype Share to Skype Hatena Bookmark Share to Hatena Bookmark! Share via MastodonShare Telegram Share to Telegram WordPress Share to WordPress.com

Embed

This website supports oEmbed. To quickly use oEmbed, just copy this site's link to your oEmbed-supported apps and websites like WordPress.

Alternatively, copy and paste the HTML code below to embed this post in your website.

($_ )! We have made this thing responsive, but recommend at least 512x512 pixels for best results.
<iframe src="https://reinhart1010.id/blog/2021/10/20/menambahkan-array-baru-pada-object-array?embed" height="512" width="512" style="border:none;"><a href="{{ $canonical }}">https://reinhart1010.id/blog/2021/10/20/menambahkan-array-baru-pada-object-array</a></iframe>
Preview

Reinhart Previano Koentjoro
Reinhart Previano Koentjoro
Citra Manggala Dirgantara
Citra Manggala Dirgantara

A Reinhart company

Products

Company