Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在foreach php上创建具有特定顺序条件的新密钥数组_Php_Arrays - Fatal编程技术网

在foreach php上创建具有特定顺序条件的新密钥数组

在foreach php上创建具有特定顺序条件的新密钥数组,php,arrays,Php,Arrays,我有多维数组,我想通过检查[delivery\u indicator]==0在我使用的foreach中创建新行 我的阵列 Array ( [0] => Array ( [item_line_no] => 0001 [delivery_indicator] => 0 ) [1] => Array ( [item_line_no] =>

我有多维数组,我想通过检查
[delivery\u indicator]==0在我使用的foreach中创建新行

我的阵列

Array
(
    [0] => Array
        (
            [item_line_no] => 0001
            [delivery_indicator] => 0
        )
    [1] => Array
        (
            [item_line_no] => 0002
            [delivery_indicator] => 0
        )
    [2] => Array
        (
            [item_line_no] => 0003
            [delivery_indicator] => 1
        )
)
这是我用来创建新数组的代码

 foreach($po as $key => $i){
    if($i['delivery_indicator'] === '0'){
       $po[] = array(); /* create new array */
    }                 
 }
将创建一个新数组,但位于数组末尾。 如何在
[delivery indicator]=0的数组下方创建新数组?


我期望的结果

Array
(
    [0] => Array
        (
            [emp_order_no] => xxx-002
            [item_line_no] => 0001
            [delivery_indicator] => 0
        )
    [1] => Array
        (
         /* new array */
        )
    [2] => Array
        (
            [emp_order_no] => xx-002
            [item_line_no] => 0002
            [delivery_indicator] => 0
        )
    [3] => Array
        (
         /* new array */
        )
    [4] => Array
        (
            [emp_order_no] => xxx-001
            [item_line_no] => 0003
            [delivery_indicator] => 1
        )
)
使用

这是将其添加到阵列的末尾,要将其添加到下一个插槽中,可能更容易复制数据

$output = [];
foreach($po as $key => $i){
    $output[] = $i;
    if($i['delivery_indicator'] === '0'){
        $output[] = array(); /* create new array */
    }
}
print_r($output);
使用

这是将其添加到阵列的末尾,要将其添加到下一个插槽中,可能更容易复制数据

$output = [];
foreach($po as $key => $i){
    $output[] = $i;
    if($i['delivery_indicator'] === '0'){
        $output[] = array(); /* create new array */
    }
}
print_r($output);

此代码在键
delivery\u指示器
等于
0
的数组之后添加一个新数组,其内容为
test
。小心使用
==
指示器。它使用for循环,而不使用foreach,因为数组是不断变化的。array_splice函数将新数组添加到实际索引之后的位置

for( $index = 0; $index < count( $po ); $index++ )  {
  if(isset($po[$index]['delivery_indicator']) && $po[$index]['delivery_indicator'] === '0'){
     array_splice( $po, $index + 1, 0, array( array( 'test' ) ) );
  }
}

此代码在键
delivery\u指示器
等于
0
的数组之后添加一个新数组,其内容为
test
。小心使用
==
指示器。它使用for循环,而不使用foreach,因为数组是不断变化的。array_splice函数将新数组添加到实际索引之后的位置

for( $index = 0; $index < count( $po ); $index++ )  {
  if(isset($po[$index]['delivery_indicator']) && $po[$index]['delivery_indicator'] === '0'){
     array_splice( $po, $index + 1, 0, array( array( 'test' ) ) );
  }
}

假设你是从 $po=数组

$pa = [];
 foreach($po as $key => $i){
    if($i['delivery_indicator'] === '0'){
       $i[emp_order_no] = "xxx-002" // Just guessing how is this filled
       $pa[]= $i;
       $pa[]= [];// new array, different syntax, same result
    }else{
       $i[emp_order_no] = "xxx-001" // Just guessing how is this filled
       $pa[]= $i;
    }    
 } 
 $po = $pa; // optional unset($pa);

假设你是从 $po=数组

$pa = [];
 foreach($po as $key => $i){
    if($i['delivery_indicator'] === '0'){
       $i[emp_order_no] = "xxx-002" // Just guessing how is this filled
       $pa[]= $i;
       $pa[]= [];// new array, different syntax, same result
    }else{
       $i[emp_order_no] = "xxx-001" // Just guessing how is this filled
       $pa[]= $i;
    }    
 } 
 $po = $pa; // optional unset($pa);

可能问题还在于,您严格比较文字和数字。 你说:

if($i['delivery_indicator'] === '0'){// string
但你有:

        [delivery_indicator] => 0 // number

除了逻辑之外,我的意思是

也许问题还在于你严格地比较文字和数字。 你说:

if($i['delivery_indicator'] === '0'){// string
但你有:

        [delivery_indicator] => 0 // number

除了逻辑之外,我的意思是问题是什么?我更新了我的问题问题是什么?我更新了我的问题您可以编辑您的第一个答案并将此信息添加到其中。这是预期用途吗?我的意思是,这两种解决方案完全不同,没有关联。这就是我为什么分开做的原因。即使我不想编辑他的问题,您也可以编辑您的第一个答案并将此信息添加到其中。这是预期用途吗?我的意思是,这两种解决方案完全不同,没有关联。这就是我为什么分开做的原因。我甚至想修改他的问题