Evan Setiawan
Evan Setiawan

Reputation: 53

Change original array values in foreach

I have an array of arrays that contain array key with the value, here the example :

 $text = [
         [
         0 => ['Andi', 'NOB', false],
         1 => ['menginap', 'V', false],
         2 => ['di', 'PREP', false],
         3 => ['Hotel', 'N', false],
         4 => ['Neo', 'NE', false],
         5 => ['Malioboro', 'NE', false],
         6 => ['selama', 'N', false],
         7 => ['satu', 'NUM', false],
         8 => ['minggu', 'N',false]
        ]
    ];

and I also have this array :

$flag = [3,4,5,6]; 

Now, If flag element array is on text key then I will change third element from false to true.

For example : $flag[0] = 3, then I will change text with key 3 to :

[3] = > 'Hotel', 'N', true

Here's what I am doing for the moment:

foreach($text as $index => &$tok){
    foreach ($tok as $tokkey => &$tokvalue) {
        foreach($flag as $key => $value){
           if($value == $tokkey){
               $tokvalue[2] = true;
           }
        }
   }
}

But, it didn't change everything. Any help is much appreciated, Thank you.

Upvotes: 1

Views: 219

Answers (8)

Deepansh Sachdeva
Deepansh Sachdeva

Reputation: 1198

You could iterate over the array of flags and equate the those keys value with the array of your required values.

$flag = [1,3];

$text = [
    0 => ['a', 'x', true],
    1 => ['a', 'y', false],
    2 => ['x', 'd', true],
    3 => ['x', 's', true],
    4 => ['a', 'x', false],
];

foreach ($flag as $key) {
    if (isset($text[$key])) {
        $text[$key][2] = true;
    }
}

Solution fiddle

Upvotes: 0

Sergii K
Sergii K

Reputation: 866

And I'll put my two cents in as well:

foreach($flag as $key){
    $text[0][$key][2] = true;
}

Code snippet here

Upvotes: 1

catzilla
catzilla

Reputation: 1981

how about directly accessing the $text?

$text[$index][$tokkey][2] = true;

this way you don't need to use references.

$text = [
  [
    ['Andi', 'NOB', false],
    ['menginap', 'V', false],
    ['di', 'PREP', false],
    ['Hotel', 'N', false],
    ['Neo', 'NE', false],
    ['Malioboro', 'NE', false],
    ['selama', 'N', false],
    ['satu', 'NUM', false],
    ['minggu', 'N.', false]
  ]
];

$flag = [3,4,5,6];

foreach ($text as $index => $token) { // outer list
  foreach ($token as $i => $t) { //inner list
    foreach ($flag as $key) {
      if ($key == $i)
        $text[$index][$i][2] = true;
    }
  }
}

print_r($text);

Upvotes: 1

Sahil Gulati
Sahil Gulati

Reputation: 15141

Here we are using array_walk to achieve desired output.

Try this code snippet here

array_walk($text[0],function(&$value,$key) use($flag){
     if(in_array($key, $flag)){ // checking whether the $key is present in the flag array
        $value[2]=true;//change the $value second index to true
     }
});

Upvotes: 2

mickmackusa
mickmackusa

Reputation: 47883

There is no cleaner / more direct (error-proof) way than:

Code: (Demo) THE SIMPLEST WAY

$flag=3; // index 
if(isset($text[0][$flag][2])){$text[0][$flag][2]=true;}  // make sure the element exists

If you have multiple flags:

$flags=[3,6,7]; // indices 
foreach($flags as $flag){
   if(isset($text[0][$flag][2])){$text[0][$flag][2]=true;}  // make sure the element exists
}

Any method that is looping your $text array is at risk of running useless iterations. This would be inefficient / poor coding practice.

Upvotes: 1

gaurav
gaurav

Reputation: 657

try it,it'll work for you:

$text = [
        [
            '0' => 'Andi,NOB,false',
            '1' => 'menginap,V,false',
            '2' => 'di,PREP,false',
            '3' => 'Hotel,N,false',
            '4' => 'Neo,NE,false',
            '5' => 'Malioboro,NE,false',
            '6' => 'selama,N,false',
            '7' => 'satu,NUM,false',
            '8' => 'minggu,N,false'
        ]
        ];
$flag = [3,4,5,6] ;

foreach($text as $k=> $value){
foreach($flag as $key=> $val){
       $kk[] = explode(',',$value[$val]);
       $kk[$key][2] = 'true';
       $kkk[] = implode(',',$kk[$key]);
}
}
echo '<pre>'; print_r($kkk);

to print the array which we want to change false value to true value.

the below code for showing all data with replacing for required output:

$text = [
        [
            ['Andi', 'NOB', 'false'],
            ['menginap', 'V', 'false'],
            ['di', 'PREP', 'false'],
            ['Hotel', 'N', 'false'],
            ['Neo', 'NE', 'false'],
            ['Malioboro', 'NE', 'false'],
            ['selama', 'N', 'false'],
            ['satu', 'NUM', 'false'],
            ['minggu', 'N.', 'false']
        ]
        ];

$flag = [3,4,5,6];

foreach ($text as $k => $value) {
        foreach ($value as $kk => $val) {
            foreach ($flag as $key) {
                if ($key == $kk)
                    $text[$k][$kk][2] = 'true';
            }
        }
}

 echo '<pre>'; print_r($text);

Upvotes: 3

Rahul
Rahul

Reputation: 18557

I am assuming this would be array,

$text = [
    0 => ['Andi', 'NOB', false],
    1 => ['menginap', 'V', false],
    2 => ['di', 'PREP', false],
    3 => ['Hotel', 'N', false],
    4 => ['Neo', 'NE', false],
    5 => ['Malioboro', 'NE', false],
    6 => ['selama', 'N', false],
    7 => ['satu', 'NUM', false],
    8 => ['minggu', 'N.',false],
];
$flag = [0 => 3, 1 => 4, 2 => 5, 3 => 6];
foreach ($text as $key => &$value) {
    if(in_array($key, $flag)){
        $value[2] = true;
    }
}
echo "<pre>";
print_r($text);

Please check output here

Upvotes: 1

Aakash Martand
Aakash Martand

Reputation: 944

This might help.

foreach($text as $index => &$tok){
    foreach ($tok as $tokkey => &$tokvalue) {
        foreach($flag as $key => $value){
           if($value == $tokkey){
               $val = explode(",",$tokvalue);
               $val[2] = true;
               $tokvalue = implode(",",$val);
           }
        }
   }
}

Upvotes: 2

Related Questions