Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:17:23+00:00 2026-06-18T08:17:23+00:00

I have a code like this: <?php $kode [J]= array (20, C, D, F);

  • 0

I have a code like this:

<?php

$kode ["J"]= array (20, C, D, F);
$kode ["K"]= array (50, B, G, U);
$kode ["T"]= array (70, V, W);

function kota ($start, $end){
    if (is_array($kode)) {
        foreach ($kode as $kota => $path){
            if ($kota=$end) {
                for ($i=1; $i < count ($kota); $i++){
                    $jalur=$start.$path[$i];
                }
            }
        }
        return $jalur;
    }
}
$start = "J";
$end = "T";
$hasil=kota ($start, $end);
echo "".$hasil;
?>

I want the output to be J-V-W
I don’t know what is wrong, can anyone help me? please…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:17:24+00:00Added an answer on June 18, 2026 at 8:17 am

    Your code has lots of issues

    1. As others pointed out, in if ($kota=$end) {, you are assigning $end to $kota and always return true i.e. the code in the IF clause always execute

    2. PHP has function scope. Simply put, variables declared inside a function cannot be used outside, and vice versa. Use parameters to pass your $kode into the function.

    3. Use of bare strings i.e. V and W in $kode ["T"]= array (70, V, W); and other places. This is highly recommended against, and PHP does warn you about this.

    4. As other pointed out, $jalur=$start.$path[$i]; would overwrite $jalur every time. The for-loop outside is meaningless. You would use .= the append-to operator. Note that you also need to initialize your variable before using this operator.

    5. $kota is always a string in your code, because in a foreach loop, the variable before => symbol means get the key of the array, and array keys can only be either String or integer. That said, for ($i=1; $i < count ($kota); $i++){ is meaningless because count($kota) cannot be greater than 1 – your for loop actually never runs.

    6. This is blatantly meaningless to append a variable with an empty string as in echo "".$hasil;

    I guess this is what you want.

    <?php
    
    $kode ["J"] = array (20, 'C', 'D', 'F');
    $kode ["K"] = array (50, 'B', 'G', 'U');
    $kode ["T"] = array (70, 'V', 'W');
    
    function kota ($kode, $start, $end){
        $jalur = $start;
        if (is_array($kode)) {
            foreach ($kode as $kota => $path){
                if ($kota == $end) {
                    for ($i = 1; $i < count($path); ++$i) {
                        $jalur .= '-' . $path[$i];
                    }
                }
            }
            return $jalur;
        }
    }
    $start = "J";
    $end = "T";
    $hasil = kota($kode, $start, $end);
    echo $hasil;
    ?>
    

    This code give you the string which starts with $start and all other elements except the first element in $kode[$end]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have some code like this function Chart(start, end, controller, method, chart) {
I have code like this <?php $array = array('http://www.google.com', 'http://www.facebook.com', 'http://www.twitter.com'); foreach ( $array
For Example if i have my code like this (php): <?php somefunc(1); function somefunc($a)
I have code like this: <?php if(isset($global_info_results)): ?> <?php echo count($global_info_results) ?> <span>Mali Oglasi:
I have code like this: <ul> <?php foreach($persons as $key) : ?> <?php if($this->session->userdata('id_user')
When I have a code like this: $('#intro-posts-container').load('/ajax/load.php', function() { bindVoting(); }); Does the
I have getJSON code like this: $('#selected_article').change(function(){ $.getJSON('<?php echo $_SERVER['SCRIPT_NAME']; ?>', { ajax: 'ajax_article_partlist'
have a php code like this,going to convert it in to C#. function isValid($n){
I have JavaScript code like this: var buffer=new Array(); function fetchData(min,max){ var ajaxReq =
I have seen code like this: function($cfg) use ($connections) {} but php.net doesn't seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.