I have data array like
$data = [
'name' => [
(int) 0 => '095a108478345cac184f956b1e8dee91a5a89f87bbabd7b3fb4058f577adf.jpg',
(int) 1 => '02059.jpg',
(int) 2 => 'avatar.jpg'
],
'type' => [
(int) 0 => 'image/jpeg',
(int) 1 => 'image/jpeg',
(int) 2 => 'image/jpeg'
],
'tmp_name' => [
(int) 0 => 'C:\xampp\tmp\php17AA.tmp',
(int) 1 => 'C:\xampp\tmp\php17BA.tmp',
(int) 2 => 'C:\xampp\tmp\php17BB.tmp'
],
'error' => [
(int) 0 => (int) 0,
(int) 1 => (int) 0,
(int) 2 => (int) 0
],
'size' => [
(int) 0 => (int) 80542,
(int) 1 => (int) 6532,
(int) 2 => (int) 6879
]
]
And i need convert to array like this
$data = [
(int) 0 => [
'name' => '095a108478345cac184f956b1e8dee91a5a89f87bbabd7b3fb4058f577adf.jpg',
'type' => 'image/jpeg',
'tmp_name' => 'C:\xampp\tmp\php17AA.tmp',
'error' => (int) 0,
'size' => (int) 80542
],
(int) 1 => [
'name' => '02059.jpg',
'type' => 'image/jpeg',
'tmp_name' => 'C:\xampp\tmp\php17BA.tmp',
'error' => (int) 0,
'size' => (int) 6532
],
(int) 2 => [
'name' => 'avatar.jpg',
'type' => 'image/jpeg',
'tmp_name' => 'C:\xampp\tmp\php17BB.tmp',
'error' => (int) 0,
'size' => (int) 6879
]
]
I'm looking for the correct way to convert the first php array to the second. Is there any of the PHP array functions provided for these actions. Either is possible with CakePHP hash Array management?
Yes, I can make a few foreach loops and create an array what I need, but I'm not sure if there is a more elegant way.
From PHP 5.5 you can use array_column and array_combine to do it.
$ret = []; $keys = array_keys($data);
for ($i=0; $i<3; $i++) {
$ret[$i] = array_combine($keys, array_column($data, $i));
}
Where 3 is the number of elements of name, type, tmp_name, etc.
Demo.
The same thing for PHP < 5.5
$ret = []; $keys = array_keys($data);
for ($i=0; $i<3; $i++) {
$ret[$i] = array_combine($keys, array_map(function($element) use ($i){
return $element[$i];
}, $data));
}
Demo.
And here an example, for the sake of completeness, with only two foreach:
$ret = [];
foreach($data as $key => $val) {
$i = 0;
foreach ($val as $v) {
$ret[$i][$key] = $v;
$i++;
}
}
Which by the way I would use regardless to other implementations.
Related
So I have array like this one
[
'custid' => [
'customer_number_1' => '20098374',
'customer_number_8' => '20098037',
'customer_number_15' => '20098297'
],
'destid' => [
'destination_numbers_1' => [
(int) 0 => '20024838',
(int) 1 => '20041339'
],
'destination_numbers_8' => [
(int) 0 => '20008293'
],
'destination_numbers_15' => [
(int) 0 => '20016969',
(int) 1 => '20022919',
(int) 2 => '20025815',
(int) 3 => '20026005',
(int) 4 => '20027083',
(int) 5 => '20045497'
]
]
]
Goal is to merge cust id with destid in pairs and should look like so
[
(int) 0 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098374',
'sap_destination_id' => '20024838'
],
(int) 1 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098374',
'sap_destination_id' => '20041339',
],
(int) 2 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098037',
'sap_destination_id' => '20008293,
],
(int) 3 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098297'
'sap_destination_id' => '20016969',
],
...
I have tried with code below, but I am getting destination_id number as array and duplicated customer numbers. Also I have tried with array walk but result is same.
$data = [];
foreach ($sap_data['custid'] as $custid) {
foreach ($sap_data['destid'] as $destid) {
$data[] = [
'user_id' => 1,
'sap_customer_id' => $custid,
'sap_destination_id' => $destid
];
}
}
Thx for helping!
You should make inner loop in other way
foreach($sap_data['custid'] as $k => $custid) {
// Make destination key
$dkey = str_replace('customer_number', 'destination_numbers', $k);
// And get array, for example, destination_numbers_1 for customer_number_1
foreach ($sap_data['destid'][$dkey] as $destid) {
$data[] = [
'user_id' => 1,
'sap_customer_id' => $custid,
'sap_destination_id' => $destid
];
}
}
demo on eval.in
So I have array like this one:
[
(int) 0 => [
'user_id' => (int) 20098238,
'location_id' => (int) 20014727
],
(int) 1 => [
'user_id' => (int) 20098238,
'location_id' => (int) 20027167
],
(int) 2 => [
'user_id' => (int) 20098238,
'location_id' => (int) 20027168
],
(int) 3 => [
'user_id' => (int) 20098238,
'location_id' => (int) 20027169
],
(int) 4 => [
'user_id' => (int) 20011189,
'location_id' => (int) 20012490
],
(int) 5 => [
'user_id' => (int) 20011189,
'location_id' => (int) 20016161
],
(int) 6 => [
'user_id' => (int) 20011189,
'location_id' => (int) 20018679
],
(int) 7 => [
'user_id' => (int) 20011189,
'location_id' => (int) 20023569
],
(int) 8 => [
'user_id' => (int) 20102015,
'location_id' => (int) 20008315
],
(int) 9 => [
'user_id' => (int) 20102015,
'location_id' => (int) 20008689
],
(int) 10 => [
'user_id' => (int) 20102015,
'location_id' => (int) 20021267
]
]
I want to get this as restult array like this one
[
(int) 20098238 => [
(int) 0 => (int) 20014727,
(int) 1 => (int) 20027167,
(int) 2 => (int) 20027168,
(int) 3 => (int) 20027169
],
(int) 20011189 => [
(int) 0 => (int) 20012490,
(int) 1 => (int) 20016161,
(int) 2 => (int) 20018679,
(int) 3 => (int) 20023569
],
(int) 20102015 => [
(int) 0 => (int) 20008315,
(int) 1 => (int) 20008689,
(int) 2 => (int) 20021267
]
]
Here is my code, but I am getting only part of sorting
$customerData = [];
foreach ($userIds as $data) {
$i = 0;
foreach ($customerData as $k => $check) {
if ($k == $data['user_id']) {
$customerData[$k][$i] = $data['location_id'];
}
$i++;
}
$customerData[$data['user_id']][$i] = $data['location_id'];
}
As result this code make key like user_id but there is problem with pushing array for that key. I am getting always maximal 2 locations per key.
Thank you for helping!
Your Perfect Solution is here :
$customerData = [];
foreach ($userIds as $data) {
foreach ($customerData as $k => $check) {
if ($k == $data['user_id'] && $data['location_id'] == $check) {
$customerData[$k][] = $data['location_id'];
}
}
$customerData[$data['user_id']][] = $data['location_id'];
}
Live Demo
Simply remove the $i usage and use $customerData[$k][] in your loop. This will add a new occurance to the userid array automatically and as many as exist.
$customerData = [];
foreach ($userIds as $data) {
foreach ($customerData as $k => $check) {
$customerData[$k][] = $data['location_id'];
}
}
I need to compare elements in array which have same elment value ex. $array[0]['change'] == $array[1]['change'] and if it true than make array push.
I have array like this
[
(int) 0 => [
'id' => (int) 2,
'uuid' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 18
],
(int) 1 => [
'id' => (int) 3,
'uuid' => 'c3f388bc-9efb-4c72-b50e-3b6a9075d919',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 11
],
(int) 2 => [
'id' => (int) 4,
'uuid' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 17
],
(int) 3 => [
'id' => (int) 5,
'uuid' => '52732822-11a9-4c00-8685-2493b88983f8',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 19
],
(int) 4 => [
'id' => (int) 6,
'uuid' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'change' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 10
],
(int) 5 => [
'id' => (int) 7,
'uuid' => '8dbe1a81-d722-4261-9b13-ef0b68cbb759',
'change' => '8a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 18
],
(int) 6 => [
'id' => (int) 8,
'uuid' => '701a896a-7d19-4cdd-80a4-f9ca7042945b',
'change' => '701a896a-7d19-4cdd-80a4-f9ca7042945b',
'company_id' => (int) 19
],
(int) 7 => [
'id' => (int) 9,
'uuid' => '64e146bf-5d76-483f-992c-274cde1202ce',
'change' => '601a896a-7d19-4cdd-80a4-f9ca7042945b',
'company_id' => (int) 20
]
]
I need to get result like this
[
(int) 0 => [
(int) 0 => [
'id' => (int) 2,
'uuid' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 18
],
(int) 1 => [
'id' => (int) 3,
'uuid' => 'c3f388bc-9efb-4c72-b50e-3b6a9075d919',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 18
],
],
(int) 1 => [
(int) 0 => [
'id' => (int) 4,
'uuid' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 18
],
(int) 1 => [
'id' => (int) 5,
'uuid' => '52732822-11a9-4c00-8685-2493b88983f8',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 18
],
],
(int) 2 => [
'id' => (int) 6,
'uuid' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'change' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 18
],
(int) 3 => [
'id' => (int) 7,
'uuid' => '8dbe1a81-d722-4261-9b13-ef0b68cbb759',
'change' => '8a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 18
],
.
.
.
]
I have tried to make 2 arrays one with same values and another with different values, than make merge but the result is not as I want... and I hope so that there is "nicer" solution (maybe more readable logic).
$exchange_array = [];
$item_array = [];
foreach ($order->items as $item) {
foreach ($order->items as $exchange) {
if (
isset($item->change) && isset($exchange->change) &&
$item->change == $exchange->change &&
$item->id != $exchange->id
) {
$exchange_array[] = $exchange;
}
if (
!isset($item->change) && !isset($exchange->change) &&
$item->id != $exchange->id
) {
$item_array[] = $exchange;
}
}
}
$item = array_merge($exchange_array,$item_array);
Thank you!
you may use foreach or array_map with this as follows :
$arrays = [
['change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2'],
['change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2'],
['change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e'],
['change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e'],
['change' => '9a356688-d08b-42ee-b26d-19d76bf6543b'],
['change' => '8a356688-d08b-42ee-b26d-19d76bf6543b'],
['change' => '701a896a-7d19-4cdd-80a4-f9ca7042945b'],
['change' => '601a896a-7d19-4cdd-80a4-f9ca7042945b'],
];
$list = [];
foreach ($arrays as $array) {
$list[$array['change']][] = $array;
}
print_r(array_values($list));
// if you would like to use array_map instead
$list = [];
array_map(function ($array) use (&$list) {
$list[$array['change']][] = $array;
}, $arrays);
print_r(array_values($list));
live demo : https://3v4l.org/1JO58
Use the following approach:
$result = [];
foreach ($order->items as $item) {
if (isset($result[$item['change']])) {
// check if nested array is a single item(not multidimensional)
if (isset($result[$item['change']]['change'])) {
$result[$item['change']] = [$result[$item['change']], $item];
} else {
$result[$item['change']][] = $item;
}
} else {
$result[$item['change']] = $item;
}
}
$result = array_values($result);
I would like to detect same records and then change quantity of the one record and delete the others. For example, given the following array:
'Cart' => [
(int) 0 => [
'size' => '38',
'code' => '5',
'qn' => (int) 1
],
(int) 1 => [
'size' => '37',
'code' => '5',
'qn' => (int) 1
],
(int) 2 => [
'size' => '37',
'code' => '5',
'qn' => (int) 1
]
],
i would like to print:
'Cart' => [
(int) 0 => [
'size' => '38',
'code' => '5',
'qn' => (int) 1
],
(int) 1 => [
'size' => '37',
'code' => '5',
'qn' => (int) 2
]
],
It looks to me that you're trying to sum quantities (qn) on duplicate sizes (size) and codes (code). One way to achieve this is by looping through the array containing the cart items and building out a new array. I suggest reading about PHP arrays and array_key_exists to learn more as they're used below:
<?php
$arr = [
['size' => '38', 'code' => 5, 'qn' => 1],
['size' => '37', 'code' => 5, 'qn' => 1],
['size' => '37', 'code' => 5, 'qn' => 1],
['size' => '37', 'code' => 4, 'qn' => 1],
];
$newArr = [];
foreach ($arr as $value) {
$key = $value['size'] . ':' . $value['code'];
if (array_key_exists($key, $newArr)) {
$newArr[$key]['qn'] += $value['qn'];
continue;
}
$newArr[$key] = $value;
}
// Resets keys
$newArr = array_values($newArr);
print_r($newArr);
I dont know it is enough for your problem, but try function array_unique
Iterate your array and use array_diff on each subarray.
If the returned array doesn't contain neither size nor code, add the two qn
The function array_unique() works for single dimension array. To find unique elements from multi-dimension array, we need to do a little modification.
Try this:
$array = array_map("serialize", $array);
$output = array_map("unserialize", array_unique($array));
If you want to follow another approach, use this as a reference: Create multidimensional array unique
Please go through following code.
$result = [
0=> ['size' => '38',
'code' => '5',
'qn' => 1
],
1=> ['size' => '37',
'code' => '5',
'qn' => 1
],
2=> ['size' => '37',
'code' => '5',
'qn' => 1
]
];
$finalArr = [];
foreach($result as $k => $v) {
$flag = 0;
foreach($finalArr as $kc => $vc){
if($v['size']==$vc['size'] && $v['code']==$vc['code']){
$flag = 1;
$finalArr[$kc]['qn'] = $finalArr[$kc]['qn'] + 1;
break;
}
}
if($flag==0){
$finalArr[] =
[
'size' => $v['size'],
'code' => $v['code'],
'qn' => 1
];
}
}
echo "<pre>";
print_r($finalArr);
The code is tested against your question and i have explained the code. This gives you solution for any number of arrays and with similar array elements with quantity(qn) incremented as you wanted:
<?php
//Compare the 1st and 2nd array
//Compare the 2st and 3rd array
// and so on
function compare($arr1 = array(), $arr2 = array()) {
$result = array_diff($arr1, $arr2);
return $result;
}
$cart = array(
0 => array('size' => 38, 'code' => 5, 'qn' => 1),
1 => array('size' => 37, 'code' => 5, 'qn' => 1),
2 => array('size' => 37, 'code' => 5, 'qn' => 1),
);
$j = 1;
$cart_total_count = count($cart);//Gives the count of your cart
$final_cart = array();//Final array with qn incremented
for($i=0;$i<$cart_total_count; $i++) {
if (!empty($cart[$i+1])) {//If 2nd array is not present then stop the comparision
if (empty(compare($cart[$i], $cart[$i+1]))){
$j++;
$cart[$i]['qn'] = $j;
$final_cart = $cart[$i];
}
}
}var_dump($final_cart);
//Output
//1. If 2 array are same
//array(3) { ["size"]=> int(37) ["code"]=> int(5) ["qn"]=> int(2) }
//2. If no array are same
//array(0) { }?>
I trying to return data every time inside the foreach() but i keep on getting the data on the first iteration of the loop here is my code
for ($i = 0 ;$i<4;$i++)
{
var_dump($i);
$publisher = Publisher::find($results[$i]->publisherId);
//pr($publisher);
$channels =$publisher->channels()->get() ;
pr($channels[0]);
$data = ReviveAgent::getPublisherDailyStatistics($channels[0],$start,$end);
pr($data);
return Response::json($data);
}
on the var_dump($i);
It only shows data for the first 0 only.So how can i return the data also for 1,2,3
here is my output when pr($data) for var_dump($i) = 1
array (
0 =>
array (
'impressions' => 1867,
'clicks' => 14,
'requests' => 44,
'revenue' => 2.79,
'day' =>
stdClass::__set_state(array(
'scalar' => '20150518T00:00:00',
'timestamp' => 1431907200,
'xmlrpc_type' => 'datetime',
)),
),
1 =>
array (
'impressions' => 2197,
'clicks' => 17,
'requests' => 382,
'revenue' => 19.829999999999998,
'day' =>
stdClass::__set_state(array(
'scalar' => '20150519T00:00:00',
'timestamp' => 1431993600,
'xmlrpc_type' => 'datetime',
)),
),
2 =>
array (
'impressions' => 5484,
'clicks' => 3,
'requests' => 3680,
'revenue' => 6.7300000000000004,
'day' =>
stdClass::__set_state(array(
'scalar' => '20150520T00:00:00',
'timestamp' => 1432080000,
'xmlrpc_type' => 'datetime',
)),
),
3 =>
array (
'impressions' => 6909,
'clicks' => 105,
'requests' => 5141,
'revenue' => 378.88499999999999,
'day' =>
stdClass::__set_state(array(
'scalar' => '20150521T00:00:00',
'timestamp' => 1432166400,
'xmlrpc_type' => 'datetime',
)),
),
The return operator implicitly ends the current execution scope. The context of your usage is not given, but you could put your $data into an array prior to JSON encoding and returning it. It might look something like this:
$data = array();
for ($i = 0; $i < 4; $i++) {
...
$record = ReviveAgent::getPublisherDailyStatistics($channels[0], $start, $end));
pr($record);
$data[] = $record;
}
return Response::json($data);
You can take this example in laravel
Here I am returning multiple Prices
$prices = $this->Prices;
foreach ($prices as $price) {
$res[] = [
'travel_mode_id' =>$price->travel_mode_id,
'occupancy_id' => $price->occupancy_id,
'rider_id' => $price->occupancy_id,
'price' => $price->price,
];
}
return $res;