There is probably a really simple way to do this but i can't work it out.
Array ( [0] => Array ( [] => US [1] => U.S. [2] => 21 [3] => 34 [4] => 33 [5] => 35 [6] => 39 [7] => 50 [8] => 61 ) [1] => Array ( [] => 79 [1] => 45 [2] => 84 [3] => 89 [4] => 59 [5] => 64 [6] => 34 [7] => 58 [8] => 55 ) [2] => Array ( [] => 63 [1] => 105 [2] => 68 [3] => 62 [4] => 64 [5] => 67 [6] => CL [7] => Chile [8] => 56 ) [3] => Array ( [] => 40 [1] => 40 [2] => 63 [3] => 37 [4] => 57 [5] => 64 [6] => 59 [7] => 53 [8] => 68 ) [4] => Array ( [] => 70 [1] => 66 [2] => 88 [3] => 48 [4] => 76 [5] => 83 [6] => 80 [7] => 53 [8] => 45 ) [5] => Array ( [] => 44 [1] => 51 [2] => 52 [3] => [4] => [5] => [6] => [7] => [8] => ) )
This is my array. There will all ways be the same number of object (9) in each however the number currently at 6 may increase.
I need to get the 1st item in each so for the 1st one i need (US) I'm stuck as if i put
echo $array[0][1];
Then I get U.S. however i need the first item (US) so i tried both
echo $array[0][0];
echo $array[0][];
Neither return a value. What am i doing wrong?
Using an “empty” string as key in an associative array is possible in PHP.
It is distinctively different from any other, non-empty string key values - so it fulfills the most basic requirement you have for such a key (and the PHP guys didn’t seem to see any need to explicitly prevent this.)
$arr = [
'foo' => 'bar',
'' => 'baz',
];
print_r would show this as
Array
(
[foo] => bar
[] => baz
)
and var_dump’ed it would look like this,
array(2) {
["foo"]=>
string(3) "bar"
[""]=>
string(3) "baz"
}
The latter makes it more obvious that the key is in fact an empty string, and therefor $arr[""] resp. $arr[''] can be used to access this element.
One might consider this one of PHP’s peculiarities - it does work, but it is hardly ever used in practice, because it just does not make the most sense for most use cases.
The other answers offer good explanation about the empty key that I won't reiterate, but a simple way to get the first item in each of the sub-arrays is to map the reset function which we mentioned in the comments over your main array.
$firsts = array_map('reset', $your_array);
This should work regardless of what the key is.
If I were you, I would begin by asking myself why my array has an empty key, my best guess is that you set your subarrays with keys instead of letting it being indexed incrementally by writing something like this :
array(
'' => 'US', // Maybe a '0' was intended to be there, and it's a type.
'1' => 'U.S.',
'2' => '21',
'3' => '34',
'4' => '33',
// etc...
);
In that case, you may benefit from fixing your code, or at least updating it so that your array is confortable to use, for example by removing the keys so that they are replaced with successive indexes.
Anyway, if you want to use that current array, do this :
echo $array[0][''];
Or iterate through it :
foreach ($array as $sub) {
echo $sub[''],'<br>';
}
If you don't have control over how the array is set, you can also reindex it using array_values(). That function takes an array as an argument and returns its values with successive indexes instead of its original keys :
foreach ($array as $key => $sub) {
$array[$key] = array_values($sub);
}
That code should give you the same array than before with the exception that the empty keys are replaced with 0.
Related
I have a csv uploader i'm creating to push to an order api. Using phpSpreadsheets toArray method, i have an array like so:
Array
(
[0] => Array
(
[0] => product_sku
[1] => product_qty
[2] => shipping_name
[3] => shipping_address1
[4] => shipping_address2
[5] => shipping_city
[6] => shipping_county
[7] => shipping_postcode
[8] => shipping_type
[9] => customer_id
)
[1] => Array
(
[0] => test_sku_1
[1] => 3
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => B17MAN
[8] => 1
[9] => 14994333
)
[2] => Array
(
[0] => test_sku_2
[1] => 2
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => B17MAN
[8] => 1
[9] => 14994333
)
[3] => Array
(
[0] => test_sku_3
[1] => 7
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => L17MA2
[8] => 1
[9] => 14994333
)
)
Each order will be on a new line, however if a customer orders two different items, i need to group them together using their postcode. As such i was aiming to rearange into this format:
[orders] => Array(
Array(
[shipping_name] => "Bruce wayne",
[customer_id] => 14994333,
[address] => Array(
[shipping_address1] => "The Manor",
[shipping_address2] => "Near Arham Asylumn",
[shipping_city] => "Gotham",
[shipping_county] => "Greater Gotham",
[shipping_postcode] => "B17MAN",
)
[products] => Array(
Array(
[sku] => "test_sku_1",
[quantity] => 3
),
Array(
[sku] => "test_sku_2",
[quantity] => "2"
)
)
)
)
Once of the first problems i encountered was trying to match the postcodes. I managed to get a count using:
$getDuplicates = array_count_values(array_map(function($duplicates) {
return $duplicates[7]; //Where 7 is the postcode
}, $rows));
This worked in counting what i needed correctly. However from there i'm hitting a brick wall. If i'm counting the duplicates, i need it to also make a note of the rows it's already gone through so they aren't pushed incorrectly into the new array i want to make.
Pseudo it should be:
for each rows as row{
if row isn't set to be ignored{
for each count of this postcode{
array_push the product sku and qty
mark these rows as rows to be ignored
}
}
}
Can anyone help me with this?
So, long story short, i've been so caught up in this i completely missed the obvious. To solve this, i took the original array from phpspreadsheet and did an array_multisort:
foreach ($rows as $key => $row) {
$postcodes[$key] = $row[7]; //the postcode key
}
array_multisort($postcodes, SORT_DESC, $rows;
I then just worked my way through the array and if the postcode changed, i'd create a new array, otherwise i'd just push straight into the correct array for the products.
I feel really stupid i didn't think of this before. Thank you to #apokryfos for trying to help!
I want to make a array of arrays.
Problem:
My final array will be like this:
Array(Array1, Array2, Array3);
and arrays will be
Array1=Array ( [0] => 0 [1] => 100 [2] => 100 [3] => 0 [4] => 0 [5] => 0 [6] => 0
Array2=Array ( [0] => 0 [1] => 100 [2] => 100 [3] => 60 [4] => 0 [5] => 30 [6] => 0
Array3=Array ( [0] => 50 [1] => 100 [2] => 100 [3] => 0 [4] => 0 [5] => 0 [6] => 40
So how can make the multidimensional array and how can I access data from this array. Help will be appreciated. Thanks
Unlike a statically typed language, there is no need to declare these up front which can be confusing to newcomers. Really what you're describing is just a two-dimensional array. So really you have two options. Assuming these aren't associative like your example above, either putting arrays together:
$array = array($array1, $array2, $array3);
Or if you are doing something with loops/iterators you can just define your two-d array on the fly:
$array[$itr][$inner_itr] = $array1[$inner_itr];
Hope that helps.
Quite new to php. I would be grateful is anyone can provide guidance about mapping the values in this array using php this is the output from var_dump
array(3) {
["k"]=>
string(78) "method,from_tag,to_tag,callid,sip_code,sip_reason,time,from_user,to_user,token"
["v"]=>
string(326) "BYE,gFNk8BZBg,B2B.269.327,KjmE8oPOV1,200,OK,Wed May 28 23:11:43 2014
,patientdemo1.gmail,sip:join.me#192.168.1.20:5060;transport=udp,037d30d7239a0a16a658474822c3c9acf7995ac781a9c1c8b4b1a7361f24400d71216209c18eff8b8b0400bb55890bb2a78eb3064b603e6ac4e270b76c36be0e365bf096e426cfb6621aac13fdec54e7bbce74b3d63244b4cb622b16e27da1c4"
["query_type"]=>
string(6) "insert"
}
as you may have noticed the column keys are within first array with key "k" and the values are under "v"
I need to pull some of these values out by referencing the keys within "k"
As stated in the comment. You'd use explode() to set your keys and values into their arrays respectively.
Done so as below:
<?php
$things = array(
'k' => 'method,from_tag,to_tag,callid,sip_code,sip_reason,time,from_user,to_user,token',
'v' => 'BYE,gFNk8BZBg,B2B.269.327,KjmE8oPOV1,200,OK,Wed May 28 23:11:43 2014
,patientdemo1.gmail,sip:join.me#192.168.1.20:5060;transport=udp,037d30d7239a0a16a658474822c3c9acf7995ac781a9c1c8b4b1a7361f24400d71216209c18eff8b8b0400bb55890bb2a78eb3064b603e6ac4e270b76c36be0e365bf096e426cfb6621aac13fdec54e7bbce74b3d63244b4cb622b16e27da1c4'
);
$keys = explode(',', $things['k']);
$values = explode(',', $things['v']);
?>
Which returns:
Keys
Array
(
[0] => method
[1] => from_tag
[2] => to_tag
[3] => callid
[4] => sip_code
[5] => sip_reason
[6] => time
[7] => from_user
[8] => to_user
[9] => token
)
Values
Array
(
[0] => BYE
[1] => gFNk8BZBg
[2] => B2B.269.327
[3] => KjmE8oPOV1
[4] => 200
[5] => OK
[6] => Wed May 28 23:11:43 2014
[7] => patientdemo1.gmail
[8] => sip:join.me#192.168.1.20:5060;transport=udp
[9] => 037d30d7239a0a16a658474822c3c9acf7995ac781a9c1c8b4b1a7361f24400d71216209c18eff8b8b0400bb55890bb2a78eb3064b603e6ac4e270b76c36be0e365bf096e426cfb6621aac13fdec54e7bbce74b3d63244b4cb622b16e27da1c4
)
And now you just need to loop through the values like so using foreach():
$data = array();
foreach($keys as $i => $key) {
$data[$key] = $values[$i];
}
Which would product your final output of:
Array
(
[method] => BYE
[from_tag] => gFNk8BZBg
[to_tag] => B2B.269.327
[callid] => KjmE8oPOV1
[sip_code] => 200
[sip_reason] => OK
[time] => Wed May 28 23:11:43 2014
[from_user] => patientdemo1.gmail
[to_user] => sip:join.me#192.168.1.20:5060;transport=udp
[token] => 037d30d7239a0a16a658474822c3c9acf7995ac781a9c1c8b4b1a7361f24400d71216209c18eff8b8b0400bb55890bb2a78eb3064b603e6ac4e270b76c36be0e365bf096e426cfb6621aac13fdec54e7bbce74b3d63244b4cb622b16e27da1c4
)
Working Example
Footnotes
This probably isn't the most efficient way to handle/do what you want to do. You should rethink how the first array with the columns/values is created and restructure that to suit your needs.
You should use array_combine() instead!
If this answers your question, just click on the arrow to the left there until it is green :) to mark this question as answered!
very basic question however I have had some trouble finding the answers on PHP.NET.
I have the following array:
Array (
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
)
I want the array to be re-ordered so that the key number 3 in the first series of the array becomes the first, then the rest to be re-ordered from there to eventually get the result of:
Array (
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
)
I am looking for a way to do this so I can define the array, then the first level key I need to sort by, and then it will return the array in this way.
The standard PHP keys didn't seem to offer something like this, so it would be good to be able to have a separate function such as $newArray = reorder_array($array, $key);
I don't require any sorting of the second level, only the initial 4 main / first level array sections.
You help is greatly appreciated as I have been sitting on this one for awhile without a clear and simple solution.
You re-ordering can be simply implemented with one foreach loop, like:
function reorderArray($array, $key)
{
$found = false;
foreach($array as $k=>$v)
{
$found = $found || $k===$key;
if(!$found)
{
unset($array[$k]);
$array[$k] = $v;
}
//else break can be added for performance issues
}
return $array;
}
with usage
$array=[1=>'foo', 4=>'bar', 9=>'baz', 'test'=>51];
var_dump(reorderArray($array, 9));
var_dump(reorderArray($array, 'test'));
var_dump(reorderArray($array, 'no_such_key'));//original array in result
-check this demo. If keys are consecutive numerics, however, this can be easily implemented with array_slice() calls.
I have a little Problem with a 2-dimensional and associative array which i need to merge in PHP.
So what i'm trying to achieve as output is something like this:
Array ( [0] => 6 [1] => 5 [2] => 9 [3] => 8 [4] => 3 [5] => 16 [6] => 55
[7] => 59 [8] => 56 [9] => 3 [10] => 4 .... [1293] => 2)
At the moment my output is as follows:
foreach ($arrayList as $key => $list) {
print_r($list);
}
is
Array ( [hgeneral1] => 6 [hgeneral2] => 5 [hgeneral3] => 9 [hgeneral4] => 8
[hgeneral5] => 3 [hgeneral6] => 16 [hmusic1] => 55 [hmusic2] => 59 [hmusic3] => 56 )
Array ( [hgeneral1] => 3 [hgeneral2] => 4 [hgeneral3] => 8 [hgeneral4] => 10 [hgeneral5]
=> 16 [hgeneral6] => 17 [hsport1] => 26 [hsport2] => 32 [hsport3] => 35 [hsport4] => 38
[hsport5] => 41 [hsport6] => 42 [hmusic1] => 55 [hmusic2] => 56 [hmusic3] => 58
[hmusic4] => 60 [hmusic5] => 61 ) Array ....
and like 50 more arrays.
Now since it's a associative array merge will just overwrite the values (if i understood that right), so my question is: Is there a way to get all these values into one big array?
I would really appreciate any help and sorry for my bad english (and the maybe kinda noobish question, but i'm really new to programming).
Cheers
Jutschge
"If the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended." array_merge doc
So take the values with array_values first and then merge like array_merge(array_values(arr1),array_values(arr2) .. )