0

Im trying to create a invoice data but i face problem where the driver name should be replaced with driver number .

for example current array

Array
(
[0] => Array
    (
        [quantity] => 20
        [unit_price] => 20
        [driver] => Ivan
        [driver_id] => 4
    )

[1] => Array
    (
        [quantity] => 10
        [unit_price] => 50
        [driver] => Ivan
        [driver_id] => 4
    )

[2] => Array
    (
        [quantity] => 20
        [unit_price] => 10
        [driver] => John
        [driver_id] => 5
    )

[3] => Array
    (
        [quantity] => 20
        [unit_price] => 20
        [driver] => John
        [driver_id] => 5
    )

)

i want to change [driver] => Ivan to [driver] => Driver 1 and [driver] => John to [driver] => Driver 2 and so on for the next driver , the problem is data is dynamic and i dont know how to do it , at the end im looking to get result like this

Array
(
[0] => Array
    (
        [quantity] => 20
        [unit_price] => 20
        [driver] => Driver 1
        [driver_id] => 4
    )

[1] => Array
    (
        [quantity] => 10
        [unit_price] => 50
        [driver] => Driver 1
        [driver_id] => 4
    )

[2] => Array
    (
        [quantity] => 20
        [unit_price] => 10
        [driver] => Driver 2
        [driver_id] => 5
    )

[3] => Array
    (
        [quantity] => 20
        [unit_price] => 20
        [driver] => Driver 2
        [driver_id] => 5
    )

)

What i tried to do isnt work correct

$i = 1;
foreach ($fetchCustomerProfitIn as $report) {
echo "Driver - $i";
echo "$report['quantity']";
echo "$report['unit_price']";
i ++
}

result become incorrect like this

Array
(
[0] => Array
(
    [quantity] => 20
    [unit_price] => 20
    [driver] => Driver 1
    [driver_id] => 4
)

[1] => Array
(
    [quantity] => 10
    [unit_price] => 50
    [driver] => Driver 2
    [driver_id] => 4
)

[2] => Array
(
    [quantity] => 20
    [unit_price] => 10
    [driver] => Driver 3
    [driver_id] => 5
)

[3] => Array
(
    [quantity] => 20
    [unit_price] => 20
    [driver] => Driver 4
    [driver_id] => 5
)

)
6
  • 2
    You could loop over the data and do the replacements, have you tried it yet? Commented Apr 3, 2020 at 7:04
  • @NigelRen yes but did not manage to replace their names with Driver 1 , Driver 2 , Driver 3 and so on.... for the current example i got result like Driver 1,Driver 2,Driver 3,Driver 4 when should be only displayed Driver 1 and Driver 2 Commented Apr 3, 2020 at 7:09
  • Please add the code you've tried and perhaps we can highlight how it can be made to work. Commented Apr 3, 2020 at 7:10
  • So the rule here is that the first name that occurs is replaced by "Driver 1" and so on? Does your array come already sorted by name/id? Commented Apr 3, 2020 at 7:14
  • @NigelRen topic updated Commented Apr 3, 2020 at 7:16

1 Answer 1

3

You could also try something like this:

$drivers = [];

$array = [
    [
        "driver" => "John",
        "quantity" => 30
    ],
    [
        "driver" => "Ivan",
        "quantity" => 25

    ],
    [
        "driver" => "John",
        "quantity" => 20

    ],
    [
        "driver" => "Ivan",
        "quantity" => 22

    ]
];

foreach($array as $key => $arr) {
    if(array_key_exists($arr["driver"], $drivers)) {
        $array[$key]["driver"] = $drivers[$arr["driver"]];
    } else {
        $number = count( $drivers)+1;
        $drivers[$arr["driver"]] = "Driver ".$number;
        $array[$key]["driver"] = $drivers[$arr["driver"]];
    }
}

print_r($array);

Prints:

Array ( 
    [0] => Array ( 
        [driver] => Driver 1 
        [quantity] => 30 
    ) 
    [1] => Array ( 
        [driver] => Driver 2 
        [quantity] => 25 
    ) 
    [2] => Array ( 
        [driver] => Driver 1 
        [quantity] => 20 
    ) 
    [3] => Array ( 
        [driver] => Driver 2 
        [quantity] => 22 
    ) 
)

You basically loop over it, get driver name, and set it as key in $drivers array with value "Driver + count of array (+1)" and you check if key exists, if so, get the value and replace in main array, else create it.

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

1 Comment

Just being pedantic, you could factor out the $array[$key]["driver"] = $drivers[$arr["driver"]]; as it's in both branches of the if.

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.