Search code examples
phparrayskey

Push specific row values to result array if row column value matches search string


I have an array with a lot of different $keys and $values
What I did is create a form that allows the user to insert an emailadres.
Then a foreach loop start running and saves the values of every found emailaddress to a new array

This is the outcome of that array:

<?php 
Array
(
    [0] => [email protected]
    [1] => 103646
    [2] => Company
    [3] => 1140
    [4] => [email protected]
    [5] => 103689
    [6] => Company
    [7] => 3400
)
?>

What I would like it that every $value has it's own $key

I don't really know how to accomplish that.

This is my array making script:

<?php
$saved = array();
$input = $_GET['emailaddress'];
foreach ($data as $files){
    $bedrijf = $files['Name'];
    $bill = $files['Bill'];
    $amount = $files['Amount'];
    $email = $files['email'];
    if ($email == $input){
        foreach($files as $values){
            $saved[] = $values;
        }

    }
}
echo'<pre>';
print_r($saved);
echo'</pre>';
?>

Solution

  • You can do that by one foreach. No need to increase complexity of the code.

    $i = 0;
    foreach ($data as $files){
        $bedrijf = $files['Name'];
        $bill = $files['Bill'];
        $amount = $files['Amount'];
        $email = $files['email'];
        if ($email == $input){
                $saved[$i]['email'] = $email;
                $saved[$i]['Amount'] = $amount;
                $saved[$i]['bill '] = $bill;    
                $saved[$i]['Name'] = $bedrijf ;    
                $i++;
        }
    }