I need to pull data out of this array into variables - php

I have an array, which I have created by importing a csv file with the parsecsv library.
Array (
[0] => Array ( [cubcode] => EC023 [qty] => 2 )
[1] => Array ( [cubcode] => EC026A [qty] => 3 )
[2] => Array ( [cubcode] => EC025 [qty] => 7 )
[3] => Array ( [cubcode] => EC027 [qty] => 67 )
[4] => Array ( [cubcode] => EC031 [qty] => 567 )
[5] => Array ( [cubcode] => EC033 [qty] => 78 )
[6] => Array ( [cubcode] => EC034 [qty] => 234 )
[7] => Array ( [cubcode] => EC038 [qty] => 67 )
[8] => Array ( [cubcode] => EC038A [qty] => 67 )
[9] => Array ( [cubcode] => EC039 [qty] => 60 )
[10] => Array ( [cubcode] => EC039A [qty] => 100 )
)
I need to loop through the array to create two variables "cubcode" and "qty" and use them in a function that I have already created.
I am struggling to get to them because the seem to be nested in the array. I have not done much work with arrays, so please be gentle with me.
Thanks.

Sorry for the code only answer, but you literally just need to write it in PHP, and not in English. Since $csv->data (from comments) is the name of the array:
foreach ($csv->data as $item) {
function_you_already_created($item['cubcode'], $item['qty']);
}

Related

How to add merge subarrays of two multi-dimensional arrays

Sorry about such an easy question but I am new to PHP.
I am trying to add the isInStock keys and values from this array:
$stock array:
Array
(
[0] => Array
(
[name] => name of item 1
[price] => 45.00
[colour] => Neon yellow
[image] => http://images1
[url] => http://url1
[productid] => 7985894
[variants] => Array
(
[0] => Array
(
[variantId] => 7986029
[isInStock] => 1
)
[1] => Array
(
[variantId] => 7986070
[isInStock] => 1
)
[2] => Array
(
[variantId] => 7985916
[isInStock] => 1
)
[3] => Array
(
[variantId] => 7985929
[isInStock] => 1
)
[4] => Array
(
[variantId] => 7985918
[isInStock] => 1
)
[5] => Array
(
[variantId] => 7985935
[isInStock] => 1
)
[6] => Array
(
[variantId] => 7985945
[isInStock] => 1
)
[7] => Array
(
[variantId] => 7985994
[isInStock] => 1
)
)
[productId] => 7985894
)
[1] => Array
(
[name] => name of item 2
[price] => 45.00
[colour] => Multi
[image] => http://images
[url] => http://url
[productid] => 8040851
[variants] => Array
(
[0] => Array
(
[variantId] => 8040898
[isInStock] => 1
)
[1] => Array
(
[variantId] => 8041115
[isInStock] => 1
)
[2] => Array
(
[variantId] => 8040904
[isInStock] => 1
)
[3] => Array
(
[variantId] => 8041132
[isInStock] => 1
)
[4] => Array
(
[variantId] => 8041015
[isInStock] => 1
)
[5] => Array
(
[variantId] => 8040942
[isInStock] => 1
)
[6] => Array
(
[variantId] => 8040954
[isInStock] => 1
)
[7] => Array
(
[variantId] => 8040990
[isInStock] => 1
)
)
[productId] => 8040851
)
and put them in this array under each size according to the variantId value if they both match.
$data array:
Array
(
[0] => Array
(
[name] => name 1
[price] => 45.00
[colour] => Neon yellow
[image] => http://url
[url] => http://url1
[productid] => 7985894
[variants] => Array
(
[0] => Array
(
[variantId] => 7986029
[size] => US 0
)
[1] => Array
(
[variantId] => 7986070
[size] => US 2
)
[2] => Array
(
[variantId] => 7985916
[size] => US 4
)
[3] => Array
(
[variantId] => 7985929
[size] => US 6
)
[4] => Array
(
[variantId] => 7985918
[size] => US 8
)
[5] => Array
(
[variantId] => 7985935
[size] => US 10
)
[6] => Array
(
[variantId] => 7985945
[size] => US 12
)
[7] => Array
(
[variantId] => 7985994
[size] => US 14
)
)
)
[1] => Array
(
[name] => name 1
[price] => 45.00
[colour] => Multi
[image] => http://url
[url] => http://url
[productid] => 8040851
[variants] => Array
(
[0] => Array
(
[variantId] => 8040898
[size] => US 0
)
[1] => Array
(
[variantId] => 8041115
[size] => US 2
)
[2] => Array
(
[variantId] => 8040904
[size] => US 4
)
[3] => Array
(
[variantId] => 8041132
[size] => US 6
)
[4] => Array
(
[variantId] => 8041015
[size] => US 8
)
[5] => Array
(
[variantId] => 8040942
[size] => US 10
)
[6] => Array
(
[variantId] => 8040954
[size] => US 12
)
[7] => Array
(
[variantId] => 8040990
[size] => US 14
)
)
)
I have been trying to solve this for a while now, but I keep getting stuck. I can assign individual values but can't get it to do the whole array. Any help would be appreciated.
I would like my resulting array to be like this:
Array
(
[0] => Array
(
[name] => name 1
[price] => 45.00
[colour] => Neon yellow
[image] => http://url
[url] => http://url1
[productid] => 7985894
[variants] => Array
(
[0] => Array
(
[variantId] => 7986029
[size] => US 0
[isInStock] => 1
)
[1] => Array
(
[variantId] => 7986070
[size] => US 2
[isInStock] => 1
)
[2] => Array
(
[variantId] => 7985916
[size] => US 4
[isInStock] => 1
)
[3] => Array
(
[variantId] => 7985929
[size] => US 6
[isInStock] => 1
)
[4] => Array
(
[variantId] => 7985918
[size] => US 8
[isInStock] => 1
)
[5] => Array
(
[variantId] => 7985935
[size] => US 10
[isInStock] => 1
)
[6] => Array
(
[variantId] => 7985945
[size] => US 12
[isInStock] => 1
)
[7] => Array
(
[variantId] => 7985994
[size] => US 14
[isInStock] => 1
)
)
)
[1] => Array
(
[name] => name 1
[price] => 45.00
[colour] => Multi
[image] => http://url
[url] => http://url
[productid] => 8040851
[variants] => Array
(
[0] => Array
(
[variantId] => 8040898
[size] => US 0
[isInStock] => 1
)
[1] => Array
(
[variantId] => 8041115
[size] => US 2
[isInStock] => 1
)
[2] => Array
(
[variantId] => 8040904
[size] => US 4
[isInStock] => 1
)
[3] => Array
(
[variantId] => 8041132
[size] => US 6
[isInStock] => 1
)
[4] => Array
(
[variantId] => 8041015
[size] => US 8
[isInStock] => 1
)
[5] => Array
(
[variantId] => 8040942
[size] => US 10
[isInStock] => 1
)
[6] => Array
(
[variantId] => 8040954
[size] => US 12
[isInStock] => 1
)
[7] => Array
(
[variantId] => 8040990
[size] => US 14
[isInStock] => 1
)
)
)
So far I have tried array_merge which doesn't put the values in the right place.
I have tried this $data['isInstock'] = $stock[0]['variants'][0]['variantId']; which also won't work.
Sadly, it takes a fair amount of iterating/preparation to get your two arrays ready for use with array_merge_recursive(). I'll admit, I'm not proud of the convolution in my method. The major factor in all this is that array_merge_recursive() only "plays nicely" with non-numeric indexes, so I had to replace your numerically indexed keys with relative id values within the arrays. I'll do my best to explain my steps, but again, it's not pretty... (Demo)
Step #1: Prepare $stock array:
foreach($stock as $subarray){
$new_stock["#{$subarray['productid']}"]=$subarray; // replace outer key
$new_variants=[]; // declare a fresh array
foreach($subarray['variants'] as $varsub){
$new_variants["#{$varsub['variantId']}"]['isInStock']=$varsub['isInStock']; // one element only
// omitting variantId element this time as the next array will offer it.
}
$new_stock["#{$subarray['productid']}"]['variants']=$new_variants;
}
Step #2: Prepare $data array & merge:
foreach($data as $subarray){
$new_data["#{$subarray['productid']}"]=$subarray; // replace outer key
$new_variants=[]; // declare a fresh array
foreach($subarray['variants'] as $varsub){
$new_variants["#{$varsub['variantId']}"]=$varsub; // both elements from variants
}
$new_data["#{$subarray['productid']}"]['variants']=array_values(array_merge_recursive($new_variants,$new_stock["#{$subarray['productid']}"]['variants']));
// new variants subarray has been merged, re-indexed, and written to $new_data
}
Step #3: re-index outer array keys, and display:
$result=array_values($new_data);
var_export($result);
The bulk of the array preparations is to generate unique id's for the outer and inner arrays (in both $stock & $data). This permits the array_merge to isolate the related productids and recursively merge the variant elements.
If these two arrays are being generated from a database, then my high recommendation is to utilize available database functionality to merge this data instead of php.
For a simple example of how array_merge_recursive() works here's a small demo. Experiment with the keys in either of the arrays. If you so-much-as remove the # from the numeric string, array_merge_recursive() will assume that it's dealing with numeric indexes and mince things up. My technique to preserve your id's as strings was to prepend the #, but it could have been done by adding any of a range of non-digit characters to the key value.

Manipulate a Multi-Multi-Dimensional PHP Array into a nice Multi-Dimensional Array

I know this is considered a somewhat basic PHP Array question but i'm running on 35 hours no sleep and I really just need to finish this up as quickly as posibble so I can get to sleep...sorry just being honest!
In PHP I have this variable $design_values
If I print_r($design_values); this ARRAY it spits out what is show below. It is "Desing" database records.
In this case there is 2 Design records which make up the first 2 Array keyys the 0 and 1
In the application there can be any number of Designs from 0 up to any number.
Now under the 2 Design Records are 24 more Array keys for each of the 2 Design Arrays.
These 24 Array keys are numbered 0 to 23.
Now under each of the 24 Array keys, is 2 keys. One named name and the other named value.
I need to take the Array $design_values and create a new Array of that array. The new Array should be formatted much better amd easy to work with.
So the name and value keys should make up a key => value. The New array should look more like this....
The reason the current Array is a complete nightmare is because that is the Format I get it in from an existing library which returns this Data from an API call.
If someone can help me to manipulate this Array into the desired Array I will be grateful! I have been messing with it for 2 hours with no luck.
Desired New Array Format :
Array
(
[0] => Array
(
['assigned_user_name'] => 'Jason Administrator',
['modified_by_name'] => 'Jason Administrator',
['created_by_name'] => 'Jason Administrator',
['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
['date_entered'] => '2014-10-01 19:29:32',
....continued for all 24 record items
),
[1] => Array
(
['assigned_user_name'] => 'Jason Administrator',
['modified_by_name'] => 'Jason Administrator',
['created_by_name'] => 'Jason Administrator',
['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
['date_entered'] => '2014-10-01 19:29:32',
....continued for all 24 record items
)
)
Current Array Format :
Array
(
[0] => Array
(
[0] => Array
(
[name] => assigned_user_name
[value] => Jason Administrator
)
[1] => Array
(
[name] => modified_by_name
[value] => Jason Administrator
)
[2] => Array
(
[name] => created_by_name
[value] => Jason Administrator
)
[3] => Array
(
[name] => id
[value] => 4c5c3c08-2b14-9f9c-6cee-542c56cac7b1
)
[4] => Array
(
[name] => name
[value] => test
)
[5] => Array
(
[name] => date_entered
[value] => 2014-10-01 19:29:32
)
[6] => Array
(
[name] => date_modified
[value] => 2014-10-01 19:29:32
)
[7] => Array
(
[name] => modified_user_id
[value] => 1
)
[8] => Array
(
[name] => created_by
[value] => 1
)
[9] => Array
(
[name] => description
[value] =>
)
[10] => Array
(
[name] => deleted
[value] => 0
)
[11] => Array
(
[name] => assigned_user_id
[value] => 1
)
[12] => Array
(
[name] => chann_channelqms_id_c
[value] =>
)
[13] => Array
(
[name] => channelqms
[value] =>
)
[14] => Array
(
[name] => design_name
[value] =>
)
[15] => Array
(
[name] => design_number
[value] =>
)
[16] => Array
(
[name] => overall_height
[value] =>
)
[17] => Array
(
[name] => overall_width
[value] =>
)
[18] => Array
(
[name] => show_to_customer
[value] => 1
)
[19] => Array
(
[name] => uploadfile
[value] => 2014-09-29_21-57-50.png
)
[20] => Array
(
[name] => nam_channelletterqms_nam_channelletterqms_designs_name
[value] => Test
)
[21] => Array
(
[name] => price_c
[value] =>
)
[22] => Array
(
[name] => shipping_c
[value] =>
)
[23] => Array
(
[name] => totalprice_c
[value] =>
)
)
[1] => Array
(
[0] => Array
(
[name] => assigned_user_name
[value] => Jason Administrator
)
[1] => Array
(
[name] => modified_by_name
[value] => Jason Administrator
)
[2] => Array
(
[name] => created_by_name
[value] => Jason Administrator
)
[3] => Array
(
[name] => id
[value] => 86f21f44-4b21-1826-3592-542c59e4be66
)
[4] => Array
(
[name] => name
[value] => fdtgrfdhg
)
[5] => Array
(
[name] => date_entered
[value] => 2014-10-01 19:41:54
)
[6] => Array
(
[name] => date_modified
[value] => 2014-10-19 19:30:45
)
[7] => Array
(
[name] => modified_user_id
[value] => 1
)
[8] => Array
(
[name] => created_by
[value] => 1
)
[9] => Array
(
[name] => description
[value] =>
)
[10] => Array
(
[name] => deleted
[value] => 0
)
[11] => Array
(
[name] => assigned_user_id
[value] => 1
)
[12] => Array
(
[name] => chann_channelqms_id_c
[value] =>
)
[13] => Array
(
[name] => channelqms
[value] =>
)
[14] => Array
(
[name] => design_name
[value] => design name
)
[15] => Array
(
[name] => design_number
[value] => 313
)
[16] => Array
(
[name] => overall_height
[value] => 22
)
[17] => Array
(
[name] => overall_width
[value] => 22
)
[18] => Array
(
[name] => show_to_customer
[value] => 1
)
[19] => Array
(
[name] => uploadfile
[value] => 2014-09-29_21-57-50.png
)
[20] => Array
(
[name] => nam_channelletterqms_nam_channelletterqms_designs_name
[value] => Test
)
[21] => Array
(
[name] => price_c
[value] =>
)
[22] => Array
(
[name] => shipping_c
[value] =>
)
[23] => Array
(
[name] => totalprice_c
[value] =>
)
)
)
If you can't change it at the source then here is one way (PHP >= 5.5.0 needed for array_column):
foreach($design_values as $key => $values) {
$result[$key] = array_combine(
array_column($values, 'name'),
array_column($values, 'value'));
}
Or possibly, and easier:
foreach($design_values as $key => $values) {
$result[$key] = array_column($values, 'value', 'name');
}
Our use the PHP Implementation of array_column
You should probably create the array you want when making the first array, but if you don't have control over that and just want to conver then something like this should work:
$newArray = array();
foreach($oldArray as $row){
$tmp = array();
foreach($row as $values){
$tmp[$values['name']] = $values['value'];
}
$newArray[] = $tmp;
}
print_r($newArray);

How to get a sub-vale from a multi-dimensional array with only 1 sub-value

$gpph_array=
Array (
[0] => Array ( [product_id] => 83 [GPPH] => 18.80 )
[1] => Array ( [product_id] => 93 [GPPH] => 20.55 )
[2] => Array ( [product_id] => 94 [GPPH] => 30.75 )
[3] => Array ( [product_id] => 109 [GPPH] => 5.60 )
)
Lets say I have a product_id of 109 how do I extract only the associated GPPH value of 5.60 with php?
<?php
echo '[GPPH] where [product_id)=>109';
?>
I've read many post dealing with multidimensional arrays but only seen people loop through results. Any help is very much appreciated.
Thank you Vulcan that is awesome foresight for what I was trying to accomplish.
I was using this method to create my array:
$gpph_array=array();
while($grow=mysql_fetch_assoc($gquery)) {
$gpph_array[] = $grow;
}
which was creating the multi-dimensial array,
like
Array (
[0] => Array ( [product_id] => 83 [GPPH] => 18.80 )
[1] => Array ( [product_id] => 93 [GPPH] => 20.55 )
[2] => Array ( [product_id] => 94 [GPPH] => 30.75 )
[3] => Array ( [product_id] => 109 [GPPH] => 5.60 )
)
by changing to
$gpph_array=array();
while($grow=mysql_fetch_assoc($gquery)) {
$gpph_array[$grow['product_id']]=$grow['GPPH'];
}
I got a single array which is much easier to pluck values from since I will know the product id.
Array ( [83] => 18.80 [93] => 20.55 [94] => 30.75 [109] => 5.60)
Great thinking Vulcan

Adding Values them removing duplicates in a php array

I have an array that I need to process by adding the values, then removing any added duplicates.
Here's the Original Array...
Array (
[0] => Array ( [PID] => 2872 [QTY] => 1 )
[1] => Array ( [PID] => 3145 [QTY] => 2 )
[2] => Array ( [PID] => 3107 [QTY] => 1 )
[3] => Array ( [PID] => 2739 [QTY] => 1 )
[4] => Array ( [PID] => 3137 [QTY] => 1 )
[5] => Array ( [PID] => 3107 [QTY] => 1 )
[6] => Array ( [PID] => 2739 [QTY] => 1 )
[7] => Array ( [PID] => 3107 [QTY] => 1 )
[8] => Array ( [PID] => 3137 [QTY] => 4 )
[9] => Array ( [PID] => 3551 [QTY] => 1 )
[10] => Array ( [PID] => 3107 [QTY] => 1 )
[11] => Array ( [PID] => 3107 [QTY] => 1 )
[12] => Array ( [PID] => 3137 [QTY] => 1 )
[13] => Array ( [PID] => 3551 [QTY] => 2 )
[14] => Array ( [PID] => 3136 [QTY] => 1 )
[15] => Array ( [PID] => 3137 [QTY] => 1 )
[16] => Array ( [PID] => 3032 [QTY] => 1 )
[17] => Array ( [PID] => 3551 [QTY] => 1 )
[18] => Array ( [PID] => 3107 [QTY] => 1 )
[19] => Array ( [PID] => 3459 [QTY] => 1 )
[20] => Array ( [PID] => 3141 [QTY] => 1 )
[21] => Array ( [PID] => 2724 [QTY] => 1 )
[22] => Array ( [PID] => 2743 [QTY] => 1 )
[23] => Array ( [PID] => 3139 [QTY] => 2 )
[24] => Array ( [PID] => 3137 [QTY] => 2 )
[25] => Array ( [PID] => 3107 [QTY] => 1 )
)
What I need to do is take this array and Add the [QTY] values from the same [PID] values then after this end up with an array like this...
Array (
[0] => Array ( [PID] => 2724 [QTY] => 1 )
[1] => Array ( [PID] => 2739 [QTY] => 2 )
[2] => Array ( [PID] => 2743 [QTY] => 1 )
[3] => Array ( [PID] => 2872 [QTY] => 1 )
[4] => Array ( [PID] => 3032 [QTY] => 1 )
[5] => Array ( [PID] => 3107 [QTY] => 7 )
[6] => Array ( [PID] => 3136 [QTY] => 1 )
[7] => Array ( [PID] => 3137 [QTY] => 9 )
[8] => Array ( [PID] => 3139 [QTY] => 2 )
[9] => Array ( [PID] => 3141 [QTY] => 1 )
[10] => Array ( [PID] => 3145 [QTY] => 2 )
[11] => Array ( [PID] => 3459 [QTY] => 1 )
[12] => Array ( [PID] => 3551 [QTY] => 4 )
)
So add all the QTY then remove the duplicates.
I'm not quite sure the best way to proceed here. Any suggestions?
Loop over them, make a new array with PID as key and QTY as value so you can add the latter up.
foreach ($array as $row) {
list($pid, $qty) = $row;
$sums[$pid] += $qty;
}
Obviously you could throw an isset in to suppress the notices.
And if you want your former array structure, convert it once more.
Id just use a simple loop:
$combined = array();
// $org will be the original structure
foreach($org as $id => $data) {
$pid = $data['PID'];
if(!isset($combined[$pid])) {
$combined[$pid] = $data;
} else {
$combined[$pid]['QTY'] += $data['QTY'];
}
}
// return keys to normal indexs instead of the PID
array_values($combined);

How to pull out data from session in PHP?

I have the following data from print_r($_SESSION):
Array (
[totalprice] => 954
[cart] => Array (
[115] => Array (
[name] => MÃ…NESKINN
[price] => 268.00
[count] => 1 )
[80] => Array (
[name] => DELFINLEK
[price] => 268.00
[count] => 1 )
[68] => Array (
[name] => OPPDAGELSEN
[price] => 418.00
[count] => 1 )
)
[shipping] => 65 )
Now I want to pull out all the price 268.00 and 418.00 from this session.
How can I do it?
I tried $_SESSION['cart']['price'], but it does not work.
$_SESSION['cart'] is an array. If you need only one row - it would be
$_SESSION['cart'][115]['price']
But you'd better walk through array:
foreach($_SESSION['cart'] as $item){
echo $item['price'];
}

Categories