PHP ARRAY get index of item for certain value - php

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.

Related

Array with strings to integers

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.

array value inside array

Ok I did some searching and everything I saw involved loops, but I don't think it would really require on. I'm trying to get the 'href' value which appears to be within an array in the initial array:
Trying to access data in an array that has an embedded array how do I get [actions][href] ?:
["domains"]=>
array(1) {
[0]=>
array(5) {
["resource"]=>
string(96) "xxxx"
["name"]=>
string(34) "yyyy"
["type"]=>
string(9) "Blacklist"
["count"]=>
int(2672)
["actions"]=>
array(1) {
[0]=>
array(1) {
["download"]=>
array(2) {
["href"]=>
string(105) "WANT THIS VALUE"
["method"]=>
string(3) "GET"

How can I detect the differences between two arrays?

I have two arrays, I would like to compare.
array1:
array(4) {
["123"]=>
array(5) {
["animal"]=>
string(2) "cat"
["name"]=>
string(4) "fred"
}
["345"]=>
array(5) {
["animal"]=>
string(3) "dog"
["name"]=>
string(4) "alan"
}
["order"]=>
string(2) "12"
}
array2:
array(4) {
["123"]=>
array(5) {
["animal"]=>
string(2) "cat"
["name"]=>
string(4) "fred"
}
["345"]=>
array(5) {
["animal"]=>
string(3) "fox"
["name"]=>
string(4) "tom"
}
["order"]=>
string(2) "12"
}
I compare them with array_diff:
$result = array_diff($array1, $array2);
But if I var_dump $result, I get the following output:
array(0) {
}
Does anyone have an idea why?
For associative arrays you should use array_diff_assoc. Also see the user contributed notes for how to do this recursively, if you need to.
With the help of sinaza I found out that no difference was displayed, because array_diff works different with multidimensional arrays.
Here is the code, that worked for me:
foreach ($array1 as $k1 => $v1) {
if (array_diff($array2[$k1], $array1[$k1])){
$result[$k1] = array_diff($array2[$k1], $array1[$k1]);
}
}

PHP, stdClass, select elements contains specifed element

I have an stClass object like this:
object(stdClass)#2 (6) {
[0]=>
object(stdClass)#44 (2) {
["uid"]=>
int(3232)
["type"]=>
string(7) "sibling"
}
[1]=>
object(stdClass)#43 (2) {
["uid"]=>
int(32323)
["type"]=>
string(7) "sibling"
}
[2]=>
object(stdClass)#42 (2) {
["uid"]=>
int(3213)
["type"]=>
string(10) "grandchild"
}
[3]=>
object(stdClass)#41 (3) {
["uid"]=>
int(-680411188)
["type"]=>
string(6) "parent"
}
[4]=>
object(stdClass)#40 (3) {
["uid"]=>
int(-580189276)
["type"]=>
string(6) "parent"
}
[5]=>
object(stdClass)#39 (2) {
["uid"]=>
int(3213)
["type"]=>
string(7) "sibling"
}
}
How can I get elements with specified value of type element?
For example, if I select "parent", I wanna get this:
object(stdClass)#2 (6) {
[3]=>
object(stdClass)#41 (3) {
["uid"]=>
int(-680411188)
["type"]=>
string(6) "parent"
}
[4]=>
object(stdClass)#40 (3) {
["uid"]=>
int(-580189276)
["type"]=>
string(6) "parent"
}
}
I know, how to write it with "foreach" and "if", but I hope that there is another way. Thanks
Your outer object is in fact, an array in disguise. You can convert it to a real array by typecasting:
$arr = (array)$obj;
Then you can use:
$filtered = array_filter(
$arr,
function($item) {
return $item->type == 'parent';
}
);
to get an array that contains only the objects you need.

Adding an extra row to a PHP array with new values

I have this array called $items. My CMS created this array with three products and I just did a var_dump with the results below.
array(3) { [0]=> array(11) { ["item_id"]=> string(4) "1320" ["name"]=> string(5) "ITEM_A" ["price"]=> string(6) "$5.00" }
[1]=> array(11) { ["item_id"]=> string(4) "1321" ["name"]=> string(5) "ITEM_B" ["price"]=> string(6) "$5.00" }
[2]=> array(11) { ["item_id"]=> string(4) "1323" ["name"]=> string(5) "ITEM_D" ["price"]=> string(6) "$5.00" }
}
Is there a way I can inject another item, "ITEM_C" into this array so that it's now
array(3) { [0]=> array(11) { ["item_id"]=> string(4) "1320" ["name"]=> string(5) "ITEM_A" ["price"]=> string(6) "$5.00" }
[1]=> array(11) { ["item_id"]=> string(4) "1321" ["name"]=> string(5) "ITEM_B" ["price"]=> string(6) "$5.00" }
[2]=> array(11) { ["item_id"]=> string(4) "1323" ["name"]=> string(5) "ITEM_D" ["price"]=> string(6) "$5.00" }
[3]=> array(11) { ["item_id"]=> string(4) "1322" ["name"]=> string(5) "ITEM_C" ["price"]=> string(6) "$5.00" }
}
and then sort it by "name"? I basically want to always add one new row to this array which will always have different names and then sort it by the name. When I do the foreach php command I don't want it to be in the order of ITEM_A, ITEM_B, ITEM_D, ITEM_C. It needs to be ITEM_A, ITEM_B, ITEM_C, ITEM_D.
Appending to an array in PHP is easy as $array[] :
$myArray = array(1,2,3);
$myArray[] = 4; //now array(1,2,3,4)
So to add to your existing array, first create your new array element
$element = array("item_id" => 1322, "name" => "ITEM_C", /*etc.*/);
Then add it your array
$myArray[] = $element;
As for sorting, there are various sort functions depending on your exact needs. Since you're sorting according to a given array key, you'll probably need to call usort with a given function.
function nameCompare($a, $b)
{
$a = $a["name"];
$b = $b["name"];
return strcmp($a, $b);
}
usort($myArray, 'nameCompare');

Categories