array_chunk reseting id in each array position - php

I created a array which stores XML elements in it.
$itemArray = array();
$itemArray[] = array ('[{id:'.$item.'species:'.$gender.'}]');
Now I used array_chunk to split the Array in 3 item parts.
$arrayChunked = array_chunk($itemArray, 3, true);
If I use:
var_dump($arrayChunked);
then the stored Items look like this:
0 => array(0,1,2)
1 => array(3,4,5)
2 => array(6,7,8)
But i need them stored like:
0 => array(0,1,2)
1 => array(0,1,2)
2 => array(0,1,2)
Any Idea of how i could do this?

Stop passing "true" as the third argument; you're telling it to preserve the original keys. If you take that out, it will reindex it automatically (as noted in the array_chunk docs).
i.e.
$arrayChunked = array_chunk($itemArray, 3);

Related

PHP Array of arrays made unique

I have an array of arrays as following:
$a = array(
1 => array("sport", "geo"),
2 => array("sport", "geo", "history"),
3 => array("geo", "history"),
4 => array("golf", "sport"),
...
);
From that I need to get keys, in such a way so that values are unique.
So from that I would need to get something like:
$b = array( 1, 3, 4 );
$a[2] would be cut out, since it has the same values as $a[1], but since $a[2] is not there, $a[3] is fine.
If some values get completely cut out, that's fine. I will have 30+ keys, from which I need to get 10, which have unique values.
Key is a question ID, and values are tags.
I want to get 10 questions, which are different from each other (so that I don't get 10 questions about Sport).
I tried array_unique(), but that just returns this:
Array (
[1] => Array (
[0] => sport
[1] => geo
)
)
which doesn't seem to help much.
Can you guys point me towards something that could help me?
I guess I could try to list all possible categories, make that array unique, sort it by random. I would need to preserve Keys, but Keys are unique...
you can use array_diff() to detect unique tags
Returns an array containing all the entries from array1 that are not present in any of the other arrays.
Then use array_merge() to store unique value to our $tags variable
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
<?php
$a = array(
1 => array("sport", "geo"),
2 => array("sport", "geo", "history"),
3 => array("geo", "history"),
4 => array("golf", "sport")
);
$tags = [];
foreach($a as $tag){
$tags = array_merge($tags, array_diff( $tag, $tags));
}
print_r($tags);
Output :
Array
(
[0] => sport
[1] => geo
[2] => history
[3] => golf
)
Just iterate through the initial array of questions, every time save the value (array of tags) to another temporary array with checking if actual tags already exists in temporary array - if not add the question to temporary array, if exists go next. Do it until you have 10 questions in your temporary array, if you finish the question array without already having 10 questions - repeat the iteration but this time add other questions even if the tags are repeating - until you have 10.

Get the key value from a multidimensional array in PHP

I've the following type of array:
Array (
[2017-01-01] => Array (
[booking_nb] => 0
)
[2017-01-02] => Array (
[booking_nb] => 0
);
How can I get the value of booking_nb if the date is equal to 2017-01-02 ?
Do I need to loop into the array ?
Thanks.
Assuming 2017-01-02 is an array key, you can do the following:
$array['2017-01-02']['booking_nb']; // will return the value 0
However, I recommend that if you are only storing the booking_nb value inside of each sub-array (i.e. no other elements inside the sub-arrays), you simply store them like so:
array(
'2017-01-01' => 0,
'2017-01-02' => 0,
)
This way, you can select with the following:
$array['2017-01-01']; // gives 0
The simplicity gained from this method also has the downside of the inability to store additional data, so use according to your needs.

Define an element of an array using values of other elements in the same array

I want to define an element in a single dimensional array as (e.g.) the sum of two other elements.
I have tried this:
$results = array(
0 => 5,
1 => 10,
2 => $results[0] + $results[1]
);
But $results[2] returns empty.
What is the correct way to reference other elements of the current array?
This is an abstraction of a more situation - the general need is to be able to define elements as a function of sibling elements.
You first have to assign the other 2 values, otherwise you can't access them until you assigned it to the variable.
So just do it like this:
$results = [5, 10];
$results[] = $results[0] + $results[1];
print_r($results);
output:
Array ( [0] => 5 [1] => 10 [2] => 15 )
I think no way to do it in dis way, because you are traYng to obtain a element, which aren't yet defined.
I think the only way is if you set some constant value in definition, and after the definition replace the value:
$results = array(
0 => 5,
1 => 10,
2 => 0
);
$result[2] = $results[0] + $results[1];

Remove Values from PHP Array if Present

I have the following PHP array:
Array
(
[0] => 750
[1] => 563
[2] => 605
[3] => 598
[4] => 593
)
I need to perform the following action on the array using PHP:
Search the array for a value (the value will be in a
variable; let's call it $number). If the value
is present in the array, remove it.
If someone could walk me through how to do that, it would be much appreciated.
Note: If it makes it any easier, I can form the array so the keys are the same as the values.
$array = array_unique($array) // removes dupicate values
while(false !== ($num = array_search($num, $array))){
unset($array[$num]);
}
$max = max($array);
will search for all keys with value $num and unset them
lets say your $array
$array = array_unique($array) // removes dupicate values
$array = arsort($array)
$variable = $array[0] // the maximum value in the array, and place it in a variable.
$key = array_search($array, $number);
if($key){
unset($array[$key]) // Search array for a value, value is present in array, remove it.
}
array_search() and unset() seems a good method for your sample data in your question. I'll just show a different way for comparison's sake (or in case your use case is slightly different from what you have posted here).
Methods: (Demo)
$array=[750,563,605,598,593];
// if removing just one number apply the number as an array element
$number=605;
var_export(array_diff($array,[$number]));
// if you are performing this task with more than one $number, make $numbers=array() and do the same...
$numbers=[605,563]; // order doesn't matter
var_export(array_diff($array,$numbers));
// if you need to re-index the output array, use array_values()...
$numbers=[605,563]; // order doesn't matter
var_export(array_values(array_diff($array,$numbers)));
Output:
array (
0 => 750,
1 => 563,
3 => 598,
4 => 593,
)
array (
0 => 750,
3 => 598,
4 => 593,
)
array (
0 => 750,
1 => 598,
2 => 593,
)

PHP: how to 'cut' my array?

I have an array
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
How can I remove the latest 2 cells and make it shorter ?
Array
(
[0] => 0
[1] => 1
[2] => 2
)
Thanks
Check out array_slice()
So, if you wanted the first three elements only:
$array = array_slice($array, 0, 3);
If you wanted all but the last three elements:
$array = array_slice($array, 0, -3);
The second parameter is the start point (0 means to start from the begining of the array).
The third parameter is the length of the resulting array. From the documentation:
If length is given and is positive, then the sequence will have that many
elements in it. If length is given and is negative then the sequence will
stop that many elements from the end of the array. If it is omitted, then
the sequence will have everything from offset up until the end of the array.
Slice it. With a knife.
Actually, with this:
array_slice($array, 0, -3);
Assuming you meant cutting off the last 3 elements.
Use array_splice():
$new = array_splice($old, 0, 3);
The above line returns the first three elements of $old.
Important: array_splice() modifies the original array.
Use array_splice as:
$array = array(0,1,2,3,4,5);
array_splice($array,0,3);
http://dev.fyicenter.com/faq/php/php_array_function_6.php
Look at the one about truncating, particularly array_splice

Categories