Array with strings to integers - php

I have some trouble with an array (php, wordpress) like shown below:
array(2) {
[0] => array(1) { [0]=> string(3) "416" }
[1]=> array(1) { [0]=> string(4) "1591" }
}
How to convert it to an array with integers?
The problem is that values are also arrays and not values like this:
array(2) {
[0] => "416" ,
[1]=> "1591"
}
I'm trying to get id of some posts using get_post_meta().
It is only a piece of my code:
$course_product = array();
foreach ($comment_ids as $comment_id) {
$course_product[] = get_post_meta( intval($comment_id), '_llms_wc_product_id', true );
}
It is giving me this strange array:
array(2) {
[0]=>
array(1) {
[0]=>
string(3) "416"
}
[1]=>
array(1) {
[0]=>
string(4) "1591"
}
}

Apparently, your meta data _llms_wc_product_id is itself an array. So, get the first value from it by appending [0]:
get_post_meta( intval($comment_id), '_llms_wc_product_id', true )[0]

This array can looks like that:
array(2) {
[0]=>
array(1) {
[0]=>
string(3) "416"
}
[1]=>
array(2) {
[0]=>
string(4) "1591"
[1]=>
string(3) "416"
}
}
It can't be deeper and I only need values so I'm using:
call_user_func_array('array_merge', $course_products);
to flatten it and after that it looks like:
array(3) {
[0]=> int(416)
[1]=> int(1591)
[2]=> int(416)
}
Then I can do what I wanted.
BIG THX.

Related

Reverse sort array with rearranging of index

So currently I'm having a problem where I have an array setup like this
array(8) {
[0]=> array(2) {
[0]=> float(2.1166666666667)
[1]=> string(7) "9434493"
}
[1]=> array(2) {
[0]=> float(2.07)
[1]=> string(7) "8591971"
}
[2]=> array(2) {
[0]=> float(2.0566666666667)
[1]=> string(8) "17015102"
}
[3]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(7) "9637191"
}
[4]=> array(2) {
[0]=> float(2.015)
[1]=> string(8) "11405473"
}
[5]=> array(2) {
[0]=> float(1.9833333333333)
[1]=> string(8) "28233403"
}
[6]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(8) "14248330"
}
[7]=> array(2) {
[0]=> float(2.0933333333333)
[1]=> string(8) "14987165"
}
}
After I use the function arsort() it looks like this:
array(8) {
[0]=> array(2) {
[0]=> float(2.1166666666667)
[1]=> string(7) "9434493"
}
[7]=> array(2) {
[0]=> float(2.0933333333333)
[1]=> string(8) "14987165"
}
[1]=> array(2) {
[0]=> float(2.07)
[1]=> string(7) "8591971"
}
[2]=> array(2) {
[0]=> float(2.0566666666667)
[1]=> string(8) "17015102"
}
[6]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(8) "14248330"
}
[3]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(7) "9637191"
}
[4]=> array(2) {
[0]=> float(2.015)
[1]=> string(8) "11405473"
}
[5]=> array(2) {
[0]=> float(1.9833333333333)
[1]=> string(8) "28233403"
}
}
So all cool that my array is sorted by the value of [0] index. But.....
When I try to loop through like this...
$x = 0;
while ($x < count($sorted_array)) {
$sorted_array[$x][0];
$x++;
}
It kept printing out the original array order. I then realized when I used the function arsort() it kept the original order of the indexes so that's why it was printing in the original array order.
Is there a function to fix this so I can loop it with an index? Any help will be much appreciated.
When you use arsort() you preserve the keys.
Because you are iterating using $x, you are effectively ignoring your sort call.
Either use rsort() with your loop.
Or use a foreach() loop after your arsort() call.
Or best, just call array_column() instead of looping.
Here are some demonstrations: (Demo Link)
$array=$copy=[
[2.1166666666667,9434493],
[2.07,8591971],
[2.0566666666667,17015102],
[2.0366666666667,9637191],
[2.015,11405473],
[1.9833333333333,28233403],
[2.0366666666667,14248330],
[2.0933333333333,14987165]
];
arsort($array);
var_export(array_column($array,0)); // <-- you lose the keys you preserved
echo "\n---\n";
foreach($array as $index=>$row){ // <-- you keep the keys you preserved
echo "$index : {$row[0]}\n";
}
echo "\n---\n";
rsort($copy); // you don't preserve the keys
for($x=0, $count=sizeof($copy); $x<$count; ++$x){ // you should cache the count instead of calling count() on every iteration
echo "$x : {$copy[$x][0]}\n";
}
Output:
array (
0 => 2.1166666666667,
1 => 2.0933333333333,
2 => 2.07,
3 => 2.0566666666667,
4 => 2.0366666666667,
5 => 2.0366666666667,
6 => 2.015,
7 => 1.9833333333333,
)
---
0 : 2.1166666666667
7 : 2.0933333333333
1 : 2.07
2 : 2.0566666666667
6 : 2.0366666666667
3 : 2.0366666666667
4 : 2.015
5 : 1.9833333333333
---
0 : 2.1166666666667
1 : 2.0933333333333
2 : 2.07
3 : 2.0566666666667
4 : 2.0366666666667
5 : 2.0366666666667
6 : 2.015
7 : 1.9833333333333
rsort() is great fit here.
Sort an array in reverse order
This function does not maintain index association
A quick look at PHP documentation suggests rsort() would work.
http://php.net/manual/en/array.sorting.php

Add array to array without key IDs? - PHP

I have a big array which looks like this:
array(2) {
["Final Fantasy VII"]=>
array(5) {
["rows"]=>
array(2) {
[0]=>
array(6) {
["price"]=>
string(5) "11.69"
["price_old"]=>
string(4) "4.66"
["currency"]=>
string(4) "euro"
["portal"]=>
string(0) ""
["link"]=>
string(77) "https://de.gamesplanet.com/game/final-fantasy-vii-download--1001-1?ref=gmkeys"
["shop"]=>
string(4) "9507"
}
[1]=>
array(6) {
["price"]=>
string(5) "14.99"
["price_old"]=>
...
}
}
}
["Battlefield 1"]=>
array(3) {
["rows"]=>
array(2) {
[0]=>
array(6) {
["price"]=>
...
}
[1]=>
array(6) {
["price"]=>
...
}
}
}
}
And I want to get only certain parts of this array where the name is matching my searched title. So, I use this code for that:
function createACFRepeater($title){
$repeater = array();
if(searchForGameXML($title)){
$count = count($GLOBALS["productsXML"][$title]['rows']);
for($i = 0; $i < $count; $i++){
array_push($repeater, $GLOBALS["productsXML"][$title]['rows'][$i]);
}
return $repeater;
}else{
return $repeater;
}
}
My problem now is that the the $repeater array looks like this:
array(2) {
[0]=>
array(6) {
["price"]=>
string(5) "19.98"
["price_old"]=>
...
}
[1]=>
array(6) {
["price"]=>
string(4) "7.99"
["price_old"]=>
...
}
}
There is a numeric key which is pointing to the array [0] => .... But what I want is simply an array in a array without any associative relations...
How can I create an array which looks like this:?
array(2) {
array(6) {
["price"]=>
string(5) "19.98"
["price_old"]=>
...
}
array(6) {
["price"]=>
string(4) "7.99"
["price_old"]=>
...
}
}
Greetings and Thank You!
According to the array definition it is impossible. Any array item must have key and value, documentation for array starts from:
An array in PHP is actually an ordered map. A map is a type that associates values to keys
You will always have numeric keys. As #lubart already said: it's impossible to have an array without keys. Btw., all of the the follwing arrays are completely equal:
$array1 = array([0] => array([0] => 'hi', [1] => array([0] => '23.5')));
$array2 = array(array('hi', array('23.5')));
$array3 = [['hi', ['23.5']]];
$array4 = [ [0] => [ [0] => 'hi', [1] => [ [0] => '23.5' ] ] ];

Group elements in collections of arrays

I have this code, it fetches data from CSV file using splFileObject:
while(!$this->_file->eof()){
$data[$i] = $this->_file->fgetcsv();
}
This is the result :
array(12) {
[0]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
[1]=>
array(1) {
[0]=>
string(41) "134333;651099594;3004051;1500.03;10/08/15"
}
[2]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
[3]=>
array(1) {
[0]=>
string(41) "134333;651099594;3004051;1500.03;10/08/15"
}
}
What I want to do , is to group the arrays by collections of 2 (or whatever) like this (e.g of count = 2):
array(12) {
[0] =>
array(2){
[0]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
[1]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
}
[1] =>
array(2){
[0]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
[1]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
}
}
Sounds like a job for array_chunk()
$data = array_chunk($data, 2);

PHP ARRAY get index of item for certain value

I have these 2 arrays $fonts['google'] and $data['value'] with the following content:
var_dump ($fonts['google']) outputs
array(4) {
[0]=> array(3) { ["family"]=> string(7) "ABeeZee" ["variants"]=> array(2) { [0]=> string(7) "regular" [1]=> string(6) "italic" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
[1]=> array(3) { ["family"]=> string(4) "Abel" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
[2]=> array(3) { ["family"]=> string(13) "Abril Fatface" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(2) { [0]=> string(5) "latin" [1]=> string(9) "latin-ext" } }
[3]=> array(3) { ["family"]=> string(8) "Aclonica" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
}
var_dump ($data['value']) outputs
array(4) {
["size"]=> int(17)
["family"]=> string(3) "Exo"
["style"]=> string(3) "200"
["subsets"]=> string(5) "latin"
}
Now I get the $data['value']['family'] = 'Abel' from my database.
Questions:
How can I get the ['variants'] for the given $data['value']['family'] value?
How can I get the index in $fonts['google'] for the sub-array where the $data['value']['family'] value is?
PHP supports Associative Arrays which let you use a (string) key rather than a numeric index for each element. These arrays are akin to javascript objects, Objective-C dictionaries, java HashMaps, etc.
That makes scenarios like this easy. Do you have control over building the original data array? If you can refactor your storage, set up the arrays like this:
$fonts['google'] = [
["ABeeZee"] => [
["variants"]=>["regular", "italic"],
["subsets"]=>["latin"]
],
["Abel"] => [
["variants"]=>["regular"],
["subsets"]=>["latin"]
],
["Abril Fatface"] => [
["variants"]=>["regular"],
["subsets"]=>["latin", "latin-ext"]
],
["Aclonica"] => [
["variants"]=>["regular"],
["subsets"]=>["latin"]
]
]
extra credit: if you have the original data as in the post, you could convert it:
$newArray = array(); // or just [] in PHP >= 5.3 I believe
foreach($fonts['google'] as $index=>$fontArray) {
$newArray[$fontArray['family']] = $fontArray;
// this leaves a redundant copy of the family name in the subarray
unset $newArray[$fontArray['family']]['family']; // if you want to remove the extra copy
}
Then it becomes trivial. Given a font family name, you just access $fonts['google'][$fontFamilyName] (or $newArray[$fontFamilyName]) using the family name as the array index.

PHP Array Search to get the second result

i got an Array like:
array(127) {
[0]=>
array(2) {
[0]=>
string(14) "Info"
[1]=>
int(9) "28491231"
}
[1]=>
array(2) {
[0]=>
string(16) "description"
[1]=>
string(9) "Webserver"
}
[2]=>
array(2) {
[0]=>
string(11) "server_type"
[1]=>
string(9) "HOST"
}
[3]=>
array(2) {
[0]=>
string(2) "os"
[1]=>
string(7) "Windows"
}
....
What would the fastest way to search for "Info" in this Array and get the Value "28491231" ?
Thanks
I assume you have an array like this:
$sourceArray = array(
array('Info', '28491231'),
array('description', 'webserver'),
array('server_type', 'HOST'),
array('os', 'Windows'),
);
i.e.
If you want to get something specific from it, you can create a helper function like this:
// Helper Function
function getData($targetKey, $sourceArray) {
foreach ($sourceArray as $arrayItem) {
list ($key, $val) = $arrayItem;
if ($key == $targetKey)
return $val;
}
return false;
}
// Usage
var_dump(getData('Info', $sourceArray));
Ouputs:

Categories