PHP Array - How to replace value with specific text for same values?
Get the solution ↓↓↓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
)
)
Answer
Solution:
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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: property [id] does not exist on this collection instance.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.