4

I have 2 arrays:

$array_1 = [1,2,3,1,2];

and:

$array_2 = [0,0,0,0,0];

I want to change the values of $array_2, so it will show if the element in $array_1 is number 1 or 2 only.

foreach($array_1 as $item)
{
    if($item = 1 || $iten == 2)
    { 
        $index = ...;//how to get index of this element
        $array_2[$index] = 1; //I don't sure this syntax is right  
    }
}

Output $array_2 should look like: $array_2 = [1,1,0,1,1]

5 Answers 5

4

In Laravel 5 or above, you can do this:
Just pass a array to this function, use a dot notation to handle nested arrays

use Illuminate\Support\Arr;
Arr::set($data, 'person.name', 'Calixto');

Or you can use this Laravel Helpers:

$data = ['products' => ['desk' => ['price' => 100]]];
data_set($data, 'products.desk.price', 200, false);

For more details look at laravel documentation: Laravel Docs Helpers

Sign up to request clarification or add additional context in comments.

2 Comments

Here are some guidelines for How do I write a good answer?. This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From review.
Sorry, i edited my answer. I hope this can help now, thanks.
0
<?php

// your code goes here
$array_1 = [1,2,3,1,2];

$array_2 = [0,0,0,0,0];
$index = 0;
foreach($array_1 as $item)
{
    if($item == 1 || $item == 2)
    { 
        //how to get index of this emlement
        $array_2[$index] = 1; //I don't sure this syntax is right  
    }
    else
    {
        //do nothing
    }
    $index++;

}

echo "<pre>";
print_r($array_2);
echo "</pre>";

1 Comment

My above code just is example, $array_1 will be access many times. But this code maybe help me, variable $index will be set when $array_1 be access.
0

I guess you only want to replace items other than 1, 2 to 0, Correct me if I am wrong.

$array_3 = array();

foreach ($array_1 as $key => $item)
{
    if($item == 1 || $item == 2)
    { 
        $array_3[$key] = $item;  
    } 
    else 
    {
        $array_3[$key] = 0;
    }
}

Comments

0

Try this code

$array_1 = array(1,2,3,1,2);
foreach($array_1 as &$item)
{
  if($item = 1 || $item == 2) {
    $item = 1;
  }
}

echo $array_1;

1 Comment

My above code just is example, $array 1 cannot be changed. Actually, structure's $array_1 and structure's $array_2 are different
0

The following PHP code might do what you intend:

<?php
$valid = [1, 2]; // allows you to extend a bit later
$array_1 = [1,2,3,1,2];

$array_2 = array_map(function($var) use ($valid) {
    return in_array($var, $valid) ? 1 : 0; 
}, $array_1);

print_r($array_2); // [1, 1, 0, 1, 1]

Take a look at the array_map function at http://php.net/manual/en/function.array-map.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.