PHP Array Slice understanding - php

I have the following array:
Array
Array (
[0] => Array (
...
)
[41] => Array (
[name] => London
[company] => nhyt6t
[top25_1] => 8.75912088
)
[42] => Array (
[name] => Manchester
[company] => gtr4rf
[top25_1] => 6.56758398
)
...
[75] => Array (
[name] => Leeds
[company] => de3wsd6
[top25_1] => 7.58675398
)
)
If my reading and understanding of http://www.php.net/manual/en/function.array-slice.php is correct, then the following should return just those within the array with an index of between 40 and 65.
$array = array_slice($array, 40, 65);
However, in practise, what this does is remove Indexes 0 through 39 but leaves everything else.
Can anyone explained where I am going wrong?

Just try with:
$array = array_slice($array, 40, 65 - 40);
So:
$array = array_slice($array, 40, 25);
We start slicing from 40 position and get 25 elements (ending on 40+25=65 position).

Array slice chooses an offset and length, not a begin offset and end offset. It starts at the begin offset and chooses the next length elements. If your array has continuous indices (0,1,2,3,4...), then it will slice from [offset -> offset + length)

Related

Moving array element to beginning of array?

If I have an array like the one below how would I go about moving key [2] and its associated value to the beginning of the array ? (making it key [0] and increasing the other keys by 1)
Current:
[0] => Array
(
[name] => Vanilla Coke cans 355ml x 24
)
[1] => Array
(
[name] => Big Red Soda x 24
)
[2] => Array
(
[name] => Reeses White PB Cups - 24 CT
)
Desired outcome:
[0] => Array
(
[name] => Reeses White PB Cups - 24 CT
)
[1] => Array
(
[name] => Vanilla Coke cans 355ml x 24
)
[2] => Array
(
[name] => Big Red Soda x 24
)
EDIT
To clarify I do will always want to move an element to the begining of the array but it wont necessarily be the last element it can sometimes be the 3rd 4th e.c.t. it varies each time.
array_splice removes (and optionally replaces / inserts) values from an array returning an array with the removed items. In conjunction with the simple array_unshift function the job could be done.
$arr = [1,2,3,4,5];
function array_move_item_as_first(array $array, int $idx) : array
{
array_unshift( $array, array_splice($array, $idx, 1)[0] );
return $array;
}
print_r(array_move_item_as_first($arr, 2));
output:
Array
(
[0] => 3
[1] => 1
[2] => 2
[3] => 4
[4] => 5
)
Why don't you use array_unshift and array_pop together?
array_unshift($someArray, array_pop($someArray));
array_pop removes the last element, and array_shift prepends the entry to the array.

PHP Array / Multidimensional / Dynamic Array

I have an array from an xml file as follows:
Array
(
[0] => 1280
[1] => 1281
[2] => 1282
)
I have a second array of numbers that should be linked to the above array.
For example
1280 links to 0001, 0002, 0003
1281 links to 5000
1282 links to 3001, 2424
What is the best approach to link/associate the values in these two arrays?
All the values above are dynamic from XML and can be different any at time.
I think what I need is something like:
Array
(
[1280] => Array
(
[0] => 0001
[1] => 0002
[2] => 0003
)
[1281] => Array
(
[0] => 5000
)
[1282] => Array
(
[0] => 3001
[1] => 2424
)
)
and then loop through each array by 1280, 1281, 1282.
all values are provided from an XML file. There's at least 1 but can be as many as 100.
1280, 1281, 1282 are fitness classes and they are associated to a fitness instructor. All values are unique.
I can get the following:
1280, 0001
1280, 0002
1280, 0003
1281, 3000
etc.
Any suggestions?
Thanks.
UPDATE:
I am able to get the values in one array as such:
Array
(
[0] => 1280|0001
[1] => 1280|0002
[2] => 1280|0003
[3] => 1281|5000
[4] => 1282|3001
[5] => 1282|2424
)
Assuming that the values in arrays are integers and by the example of code that you want to get, this should do the trick.
$array1 = array(1280, 1281, 1282);
$array2 = array(array(1, 2, 3), array(5000), array(3001, 2424));
$result = array();
for ($i = 0; $i < $array1.size(); $i++) {
$result[$array1[$i]] = $array2[$i];
}

PHP array in array - DXF script

I found a php script to write DXF file based on coordinates of polygon. IT works on the test file with this code (for polygon):
$d->append(new DxfPolyLine(array('points'=>array(array(1, 1),
array(20, 10),
array(20, 20),
array(1, 15)),
'lineType'=>'DASHED',
// 'layer' => 'DXFWRITER',
'flag' => 0
//'width' => 1,
//'color'=>1
)
));
The DXF file result is like this:
VERTEX
8
0
6
DASHED
10
20.000
20
20.000
0
VERTEX
8
0
6
DASHED
10
1.000
20
15.000
0
A lot of vertex's inside a polyline.
Now I am trying to insert my own coordinates. I have the coordinates but how can I write an array in that array?
I have this: $g=array_merge($g,array(array($coord[$z]*3.527785, $coord[$z+1]*3.527785)));
With this code the result is:
Array ( [0] => Array ( [0] => -133.92170714209 [1] => -41.834100838885 ) [1] => Array ( [0] => -135.19600658422 [1] => -44.558002415365 ) [2] => Array ( [0] => -173.40700872797 [1] => -25.465001344078 ) [3] => Array ( [0] => -211.44500829533 [1] => -6.4740001788315 ) [4] => Array ( [0] => -209.93490817601 [1] => -3.255100166471 ) [5] =>
Array ( [0] => -190.0770099388 [1] => -13.202000524885 ) [6] => Array ( [0] => -171.92300716209 [1] => -22.296000898041 ) [7] => Array ( [0] => -172.13500940166 [1] => -22.749000947663 ) [8] => Array ( [0] => -171.12900859213 [1] => -23.251001225378 ) [9] => Array ( [0] => -152.49300807866 [1] => -32.559001622754 ) [10] => Array ( [0] => -133.92170714209 [1] => -41.834100838885 ) )
So far so good. It respects the format from example. But in the DXF file does write only 1 (from array number).
If I change the code in
$d->append(new DxfPolyLine(array('points'=>array($g[3]),
with array[3] - it does write the coordinates in DXF file. Is there a way to make php to read all arrays from an array? I've tried with foreach but it doesn't work. PHP gives an error for not closing the ).
The source code is here:
https://github.com/digitalfotografen/DXFwriter
With $g[3] I have the coordinates from array[3] in DXF file:
VERTEX
8
0
6
CONTINUOUS
10
-211.445
20
-6.474
0
If I put $g simple I have:
VERTEX
8
0
6
CONTINUOUS
10
1.000
20
1.000
30
1.000
40
1.000
50
1.000
60
1.000
70
1.000
I recreated the $g array based on your dump. Does this outputs the desired DXF file?
$g = [[-133.92170714209, -41.834100838885],
[-135.19600658422, -44.558002415365],
[-173.40700872797, -25.465001344078],
[-211.44500829533, -6.4740001788315],
[-209.93490817601, -3.255100166471 ],
[-190.0770099388 , -13.202000524885],
[-171.92300716209, -22.296000898041],
[-172.13500940166, -22.749000947663],
[-171.12900859213, -23.251001225378],
[-152.49300807866, -32.559001622754],
[-133.92170714209, -41.834100838885]
];
$d->append(new DxfPolyLine(['points' => $g]));
regarding building the array, you could do something like this:
for ($z = 0; $z < $numar; $z+=2) {
$g[] = [$coord[$z]*3.527785,$coord[$z+1]*3.527785];
}
Your formatting's a bit screwy, but looking at the dumped array-data I see a single array who's values are arrays, that have 2 float values each. Note that Array[0] and Array[10] have the same values which may or may not be a problem for the lib.
Also, what's with the asterisks in the $d->append... logic?
Is there a way to make php to read all arrays from an array?
Yes, but PHP will impose some restrictions depending on the nature of the keys and values. See http://php.net/manual/en/language.types.array.php for more specific info as to how PHP treats arrays.
I've tried with foreach but it doesn't work. PHP gives an error for not closing the ).
Can you post the failing code please? What you describe is simply a formatting / syntax error and nothing to do with the data itself.

PHP Slicing a multidimensional array

I have the following print_r() from a multi-dimensional array (for simplicity I have not included all information in the array:
print_r()
Array (
[0] => Array (
...
)
[41] => Array (
[name] => London
[company] => nhyt6t
[top25_1] => 8.75912088
)
[42] => Array (
[name] => Manchester
[company] => gtr4rf
[top25_1] => 6.56758398
)
...
[75] => Array (
[name] => Leeds
[company] => de3wsd6
[top25_1] => 7.58675398
)
)
What I am trying to achieve is to slice the array so that I get the information from array [41] upwards into a new array.
I know I can use `array_slice()' but my following code doesn't work and I am sure it's because the array I am slicing from is multi-dimensional and I can't work out how to achieve this.
Non working code
$array = array_slice($row, 40);
Any and all feedback and advice welcomed.
You just need to check the manual on php -> array_slice()
$array = array_slice($row, 40, (count($row) - 40))
You simply forget to supply the 3rd parameter ($length) which we do as a count from the 41 nth item to up :)
Example
<?php
$result = array();
for($i=40;$i<count($row);$i++)
{
if(array_key_exists ($i,$row))
{
$result[$i] = $row[$i];
}
}
print_r($result );
?>

Change value in first of two arrays (multidimensional) PHP

I want to change the number in the first array in a multidimensional array. I have a code that outputs the value to an array and there is no chance for it to start counting from one - in my code. So my idea is to change the value starting from one - after it has been declared. My array look like this:
Array
(
[53] => Array
(
[name] => Volkswagen
[regularePrice] => 2139.00
)
[54] => Array
(
[name] => BMW
[regularePrice] => 2219.00
)
[55] => Array
(
[name] => Chrysler
[regularePrice] => 2399.00
)
)
I want - through a while or for - go through the array and change the values 53 to 1, 54 to 2, 55 to 3 and so on depending on how long the array is.
How do I accomplish this?
The answer is:
array_values($arr);
did you try:
$array = array_values($array);

Categories