PHP Split Array - php

I need to split an array but its not something I have done before.
The array is taken from a MySQL database by mysql_fetch_array:
Example:
Array (
[0] => Audi
[make] => Audi
[1] => 80 1.3
[model] => 80 1.3
[2] => 1297
[cc] => 1297
[3] => 1297
[choice] => 1297
[4] => 60
[bhp] => 60
[5] => 08/81-03/87
[date] => 08/81-03/87
[6] => EP
[engcode] => EP
[7] => Petrol
[fuel] => Petrol
[8] => -
[notes] => -
[9] => CAM BELT KIT
[type] => CAM BELT KIT
[10] => KTB201
[partno] => KTB201
[11] => AUDI
[itemspec] => AUDI
)
I need to change this to:
Array (
[0] => Audi
[1] => 80 1.3
[2] => 1297
[3] => 1297
[4] => 60
[5] => 08/81-03/87
[6] => EP
[7] => Petrol
[8] => -
[9] => CAM BELT KIT
[10] => KTB201
[11] => AUDI )
I have tried a few methods using filter array but I cannot seem to get it to output correctly, any ideas?

Use the function properly in the first place instead of trying to fix the goof-up after.

You can use:
mysql_fetch_array ( $result, MYSQL_NUM );
It will return only numeric keys.

As far as I can see, those arrays are the same, it's just you have dropped the associative keys in favour of the numeric ones. What process can't you achieve without removing them?
The way I see it, you should be able to use the original array for the majority of processing purposes.

try using mysql_fetch_row instead of mysql_fetch_array
references
http://nz.php.net/manual/en/function.mysql-fetch-row.php
http://nz.php.net/manual/en/function.mysql-fetch-array.php

You can use array_unique to keep only different elements from your array, but I'm not sure this is the better solution.
The "problem" is, according to mysql_fetch_array documentation, that the default fetch mode is MYSQL_BOTH, you should try using MYSQL_NUM :
$array = mysql_fetch_array($result, MYSQL_NUM);

Seems like you using mysql_fetch_array($result, MYSQL_BOTH) statement. Change it to mysql_fetch_array($result, MYSQL_NUM)

mysql_fetch_array already has an option to only return numeric keys:
$row = mysql_fetch_array($result, MYSQL_NUM);
Hope that helps.
(BTW, you wouldn't really call what you were asking "splitting" an array.)

Related

How to split 1 array into 2 arrays, remove certain items, and combine them again into 1 array in PHP?

i want to create something using array. I have 1 array and i need to split it into 2 array. After that search specific items from both array and remove it then combine it 2 array into 1 array.
How do i do that?
I already try to use unset for array but confuse how to use it for specific key since my array data format like 16/2/1/1 and 16/2/1/5. I need to remove data which have 1.
My format array is like this
Array
(
[1] => Array
(
[0] => 16/2/1/1 --> remove this have 1 after 2
[1] => 16/2/0/2
[2] => 16/2/0/3
[3] => 16/2/0/4
[4] => 16/2/0/5
[5] => 16/2/0/6
[6] => 16/2/0/7
[7] => 16/2/0/8
[8] => 16/2/0/9
[9] => 16/2/0/10
[10] => 16/2/0/11
[11] => 16/2/0/12
[12] => 16/2/0/13
[13] => 16/2/0/14
[14] => 16/2/0/15
[15] => 16/2/0/16
)
[2] => Array
(
[0] => 16/2/0/1
[1] => 16/2/0/2
[2] => 16/2/0/3
[3] => 16/2/0/4
[4] => 16/2/1/5 --> and this have 1 after 2 before 5
[5] => 16/2/0/6
[6] => 16/2/0/7
[7] => 16/2/0/8
[8] => 16/2/0/9
[9] => 16/2/0/10
[10] => 16/2/0/11
[11] => 16/2/0/12
[12] => 16/2/0/13
[13] => 16/2/0/14
[14] => 16/2/0/15
[15] => 16/2/0/16
)
)
i expect the output something like (after combine)
Array
(
[0] => 16/2/0/2
[1] => 16/2/0/3
[2] => 16/2/0/4
[3] => 16/2/0/6
[4] => 16/2/0/7
[5] => 16/2/0/8
[6] => 16/2/0/9
[7] => 16/2/0/10
[8] => 16/2/0/11
[9] => 16/2/0/12
[10] => 16/2/0/13
[11] => 16/2/0/14
[12] => 16/2/0/15
[13] => 16/2/0/16
)
Thanks for time to help me.
Make the array unique and then extract items that are digits/digits/NOT 1/digits:
$array = preg_grep('#^\d+/\d+/[^1]/\d+#', array_unique($array));
I would use preg_grep which allows you to search an array using a Regular expression.
$array =[
'16/2/0/13',
'16/2/0/16',
'16/2/1/5'
];
$array = preg_grep('~^16/2/0/\d+$~', $array);
print_r($array);
Output
Array
(
[0] => 16/2/0/13
[1] => 16/2/0/16
)
Sandbox
The Regex
^ match start of string
16/2/0/ - match literally (at the start of string, see above)
\d+ any digit one or more
$ match end of string
So Regular expressions is a way to do pattern matching, in this case the pattern is 16/2/0/{n} where {n} is any number. So by doing this we can find only those items that match that pattern.
Then if you have duplicates, you can do array_unique() and easily remove those.
There are many ways to do this array_filter with a custom callback etc. But this is the most straightforward way (if you know Regex).

PHP Array get first item

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.

Order a PHP array based on contents

I have an array in PHP which looks like this:
Array
(
[2] => post4.post
[3] => post7.post
[4] => post5.post
[5] => post3.post
[6] => post6.post
[7] => post1.post
[8] => post2.post
)
How could I arrange it so it could look like this?
Array
(
[0] => post7.post
[1] => post6.post
[2] => post5.post
[3] => post4.post
[4] => post3.post
[5] => post2.post
[6] => post1.post
)
The array has a list of the files contained in a folder. In my local server looks like the second example but on a standard server looks like the first one.
Thanks in advance :D
You are looking for rsort.
PHP has great documentation. I suggest you have a look at the array functions and the Sorting Arrays article (which can be easily find via Google ;))
rsort($array) will sort an array in reverse/descending order (from high to low).
For future reference use this page (http://php.net/manual/en/array.sorting.php) to identify the most suited array sorting method for your needs.
You don't want to use rsort because it will reassign array indexes. Instead use arsort().

serializing an array and storing in DB

i have an array like
$newArray = $_POST[$newId];
print_r($newArray);
it prints like
Array ( [1] => Yes [2] => a [3] => b [4] => c [5] => d [6] => e [7] => f [8] => [9] => [10] => [11] => [12] => [13] => [14] => )
but when i try to store in in db after serializing like
serialize($newArray)
it get stored like
s:211:"Array
(
[1] => Yes
[2] => ab
[3] => c
[4] => d
[5] => e
[6] => f
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
";
which is a single array element in DB..how do i properly serialize the element.
It looks like it's serializing a string, not an array. Are you sure $newArray is an array?
The string returned from serialize starts with 's:211'. This means that a string was passed into serialize(). If an array were passed into serialize() the returned string would start with 'a:14'.
#pradeep
where you storing $newArray in textfield, store it by serialling
$arrayString = $_POST['newId'];
You will get seriallized array in $arrayString.
If you want to use that array before storing in database , use unserialize , else directly store in database as that is already seriallized.
$array = unserialize($arrayString);
This will solve your problem
Not really sure I understand the question, if you don't want to serialize though, and if you want to pass it from a text field, maybe do custom syntax like - 1:a;b:2;c:3
then explode(';',$string); loop that and for the result, explode(':',$rows)
make the delimiters more difficult to clash
explode("[[;]]", string); // 1]]:[[b[[;]]

cakephp save array

what would be the efficient way of saving the following array using php (cakephp)?
each value needs to go into a new row in the table?
Array
(
[0] => 6786754654
[1] => 5643564545
[2] => 344544545
[3] => 233245654654
[4] => 453454654654
[5] => 6546542323
[6] => 654654654
[7] => 645654654
etc....
)
thanks
2 choices:
Format the array as required by Model::saveAll()
Loop through the array calling Model::create(), then Model:save()
I'd recommend option 1 as you can use Model::saveAll($data, array('validate' => 'first')); to ensure that all values are valid before saving any of them.

Categories