echo如果它是一个不同的键

时间:2018-04-06 14:35:46

标签: php arrays

我有Array

$array = [
    0 => ['a', 'b', 'c'],
    1 => ['d', 'e', 'f'],
    2 => ['g', 'h', 'i']
];

我使用以下foreach loopecho他们

foreach ($array as $key => $arrs) {
    foreach ($arrs as $arr) {
        echo $arr;
        echo 'X';
        //result:aXbXcXdXeXfXgXhXiX 
    }
}

我想要做的只是echo 'X'; $key只有$array result: abcXdefXghi 才能获得结果

$new = false;
foreach ($array as $key => $arrs) {
    if ($new != $key) {
        $new = true;
    } else {
        $new = false;
    }
    foreach ($arrs as $arr) {
        echo $arr;
        if ($new) {
            echo 'X';
        }
    }
}

我尝试过的是

result: abcdXeXfXghi

但结果是

public class ShoppingActivity extends Activity {

    CheckBox checkBox1, checkBox2;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping);

        // The checkbox for Hamburger
        checkBox1 = findViewById(R.id.checkBox1); 
        // The checkbox for Cheese Burger
        checkBox2 = findViewById(R.id.checkBox2); 

        // The textView where you display the selected things
        textView = findViewById(R.id.textView);

        // Add listeners to your checkboxes to tell them to update the text view when they are clicked
        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateTextView();
            }
        });

        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateTextView();
            }
        });
    }


    // This updates the TextView depending on what is checked   
    private void updateTextView() {
        String text = "";
        if(checkBox1.isChecked()) {
            text += "Hamburger\n";
        }
        if(checkBox2.isChecked()) {
            text += "Cheese Burger\n";
        }
        textView.setText(text);
    }

}

3 个答案:

答案 0 :(得分:2)

添加一个标志并检查它:

$first = true;
foreach ($array as $key => $arrs) {
    // Do not echo X before first array.
    if ($first) {
         $first = false;
    } else {
         echo 'X';
    }

    foreach ($arrs as $arr) {
        echo $arr;
    }
}

或者如果您的密钥是数字和0索引 - 请检查$key值:

foreach ($array as $key => $arrs) {
    if ($key > 0) {
         echo 'X';
    }

    foreach ($arrs as $arr) {
        echo $arr;
    }
}

答案 1 :(得分:1)

我们的想法是在主数组的最后一个元素之前执行echo X

foreach ($array as $key => $arrs) {
    foreach ($arrs as $arr) {
        echo $arr;
    }
    if ($key != count($array) - 1) {
        echo "X";
    }
}
//result: abcXdefXghi

答案 2 :(得分:0)

如果我理解正确,我认为您正在尝试这样做:

foreach ($array as $key => $arrs) {
    echo 'X';
    foreach ($arrs as $arr) {
        echo $arr;
    }
}

如果您不想为第一个新密钥显示X,但只向其他新密钥显示:

    $first = true;
    foreach ($array as $key => $arrs) {
        if ($first) {
           $first = false;
        } else
        echo 'X';
        foreach ($arrs as $arr) {
            echo $arr;
        }
    }