I have the following code that I am using on WordPress:
if($terms && !is_wp_error($terms) ) {
$colors = array();
foreach ($terms as $term) {
$colors[] = '\'' . $term->slug . '\'';
}
}
print_r(array_values($thePack));
The variable $color now is a basic Array, which print_r displays like this:
Array (
[0] => 'white'
[1] => 'green'
)
I'd like to make a condition in order recognize whether the array has or not a specific value, for example:
if(in_array('white', $colors) {
echo "This is white";
}
However, it is not working at all, because the in_array does not recognize the value in the array!
How could I make the condition work?
Your array values (the color names) include single quotes, which you need to include when you search for a value:
if(in_array("'white'", $colors) {
// ...
}
Why not do something like this:
while(list($key, $value) = each($array)){
if($value == 'white'){
echo 'this is white';
}
}
The problem is you are escaping the color into the array. Instead of using
$colors[] = '\''.$term->slug.'\''
Just do
$colors[] = $term->slug
And when you output the slug to the web page or the database, then you escape it.
Related
I need small doubt in PHP. how to get below PHP array data in to variables.
$arraydata = Array
(
[366] => 1084569.5892969
[181TO365] => -2128157.619635
[121TO180] => -59235.780429687
[91TO120] => -266089.29
[61TO90] => -56390
[0TO60] => 8212872.9800098
)
This is my array output data.i need to get these data in to as variables like below.
$366data= 1084569.5892969;
$181TO365data = -2128157.619635;
$121TO180data = -59235.780429687;
$91TO120data = -266089.29;
$61TO90data = -56390;
$0TO60data = 8212872.9800098;
Like this variables get data . Can anyone help
Apart from the fact that PHP variables cannot start with a number (_ or letter) you can just loop over it like so:
<?php
foreach( $array as $key => $value ) {
$$key = $value;
}
The $$ before key means that you assign the value of $key to a variable with that name. But you probably to prefix it with an _ to make this work.
For example:
<?php
foreach( $array as $key => $value ) {
$prefixed = '_' . $key;
$$prefixed = $value;
}
Why do you want this by the way?
I need to get values of array through a loop with dynamic vars.
I can't understand why the "echo" doesn’t display any result for "$TAB['b']".
Do you know why ?
The test with error message : https://3v4l.org/Fp3GT
$TAB_a = "aaaaa";
$TAB['b'] = "bbbbb";
$TAB['b']['c'] = "ccccc";
$TAB_paths = ["_a", "['b']", "['b']['c']"];
foreach ($TAB_paths as $key => $value) {
echo "\n\n\${'TAB'.$value} : "; print_r(${'TAB'.$value});
}
You are treating the array access characters as if they are part of the variable name. They are not.
So if you have an array $TAB = array('b' => 'something');, the variable name is $TAB. When you do ${'TAB'.$value}, you're looking for a variable that's actually named $TAB['b'], which you don't have.
Since you say that you just want to be able to access array indexes dynamically based on the values in another array, you just put the indexes alone (without the array access characters) in the other array.
$TAB['b'] = 'bbbbbb';
$TAB['c'] = 'cccccc';
$TAB_paths = array('b', 'c');
foreach ($TAB_paths as $key => $value) {
echo "\n\n".'$TAB['."'$value'".'] : ' . $TAB[$value];
}
Output:
$TAB['b'] : bbbbbb
$TAB['c'] : cccccc
DEMO
It's unclear what you're trying to do, although you need to include $TAB_all in $TAB_paths:
$TAB_paths = [$TAB_all['a'], $TAB_all['aside']['nav']];
Result:
${TAB_all.aaaaa} : ${TAB_all.bbbbb} :
Not certain what you're needing. My guess you need to merge two arrays into one. Easiest solution is to use the array_merge function.
$TAB_paths = array_merge($TAB_a1, $TAB_a2);
You can define the variable first
foreach ($TAB_all as $key => $value) {
${"TAB_all" . $key} = $value;
}
Now Explore the result:
foreach ($TAB_all as $key => $value) {
print_r(${"TAB_all" . $key});
}
I'm having a problem looping through my array to get just one value each time. I want to gray only 1 value because I'm calling that value in jquery. I'm new and sorry if this is too basic but I've been trying for days, pulling my hair out!
$designslist = array('design1','design2','design3','design4');
$current_index = 0;
$current_id = current($designslist);
$next = $designslist[$current_index + 1];
foreach( $designslist as $value )
{
if( $value !== $next )continue;
$nexts = $next;
echo $nexts;
$current_index++;
}
Inside my html
next
calling the id with jquery
$('#design1').on('click',function() {
$('#web1').removeClass().addClass('big1');
});
foreach( $designslist as $value )
needs to be
foreach( $designslist as $i => $value )
$i is the array index
You can try array search
$tag = array_search($value, $designslist);
$nexts = $$designslist[$tag];
Ok, you seem to be mixing for with foreach using these "current_id" and "next" variables.
You can use this kind of foreach statement:
foreach ($designList as $key => $value) {
echo "The key of $value is $key";
}
As you want to return only unique values, you should use the "array_unique" function. Try this:
foreach (array_unique($designList) as $key => $value) {
echo "The key of $value is $key";
}
I know it's not related to the question, but, as you said you're new to this, there are some PHP tips you should follow.
You're mixing snake_case with camelCase in your variables. I recommend using camelCase, but it's up to you.
I have a problem converting variables into array.
I am running foreach loop to get values from my multidimensional array $images. $images array contains image name eg: "Item Blue.png" or "Item Light Oak.png" and id of each image.
foreach ($images['images'] as $image) {
$image_name = explode(" ", substr_replace($image->filename ,"",-4));
if(!empty($image_name[2])) {
$colour = ucfirst($image_name[1] . " " . $image_name[2]);
}
else {
$colour = ucfirst($image_name[1]);
}
}
$colour variable is giving me Color name and $image->id can give me image id.
I would like to build $colors array with above variables that it would look like this:
$colors = array(
'Blue' => 1620,
'Green' => 1467,
);
Kind of like this:
$colors = array(
'$colour' => $image->id,
);
I have no idea how to do this and I will appreciate any help to give me at least some directions.
Thanks
This should be pretty straightforward ... Two things to do:
First initialize the colors array outside of your foreach:
$colors=array(); //<-- add this
foreach ($images['images'] as $image) {
$image_name = explode(" ", substr_replace($image->filename ,"",-4));
...
then just add one line after the if/else, still inside your foreach loop that will insert a new item into the $colors array.
...
else {
$colour = ucfirst($image_name[1]);
}
$colors[$colour]=$image->id; //<-- add this
}
This will create a colors array with contents like what you're looking for. I'm assuming that there is an 'id' key in the $image iterator. Did you need to create one?
All that said, you're not checking for these problems:
color names with spaces, like 'light oak'
item names with spaces like 'large item light oak.png'
duplicate colors with different IDs
Hope that helps
I'm trying to make a universal script that adds keywords to my individual pages (since header is in an include file) so I am getting the end of the url (multi.php) and retrieving the desc etc. from it's array. For some reason instead of returning keywords or descriptions it instead just returns "m" . . . it's kind of random and has me scratching my head. Here's what I got
<html>
<head>
<title>Multi-Demensional Array</title>
<?php
$path = pathinfo($_SERVER['PHP_SELF']);
$allyourbase = $path['basename'];
$pages = array
(
"multi.php" => array
(
"keywords" => "index, home, test, etc",
"desc" => "This is the INDEX page",
"style" => "index.css"
),
"header.php" => array
(
"keywords" => "showcase, movies, vidya, etc",
"desc" => "SHOWCASE page is where we view vidya.",
"style" => "showcase.css"
)
);
?>
</head>
<body>
<?php
foreach($pages as $key => $value)
{
if($key == $allyourbase)
{
echo $key['desc'];
}
}
?>
</body>
</html>
The reason why this is happening is because in PHP if I had the following code:
$hello = 'world';
and I attempted to do the following:
echo $hello[0];
PHP Would treat the string as an array and return me whatever is in position 0, which would result in w, when your using a foreach your asking PHP to set the key of the array to $key, and it's value to $value.
you then echo echo $key['desc'];, as the value is a string, php sees it as an integer based index, so it will ignore your call for desc and then return the first index, if you were to change echo $key['desc'] to echo $value['desc'] which is a hash based array it will return the desired results.
You should just be able to do this:
if(isset($pages[$allyourbase]))
{
echo $pages[$allyourbase]['desc'];
}
No need for the loop
try
echo $key['desc'];
replace with
echo $value['desc'];
Other people have provided some great solutions, but it's important that you understand exactly what is happening here, so you don't make the same mistake again. Pay careful attention to the comments, and you will be on your way to successful coding!
Here's what is happening:
foreach ($pages as $key => $value) {
if ($key == $allyourbase) {
// At this point: $key = 'multi.php'
// Also: $value = array( ... );
// Keep in mind: $key['desc'] = $key[0] = 'm';
// You are grabbing the first letter of the 'multi.php' string.
// When dealing with strings, PHP sees $key['desc'] as $key[0],
// which is another way to grab the very first character of 'multi.php'
echo $key['desc'];
// You really want $pages[$key]['desc'], but below
// is a better way to do it, without the overhead of
// the loop.
}
}
If you kept the loop, which is really unnecessary, it would look like this:
foreach ($pages as $key => $value) {
if ($key == $allyourbase) {
echo $value['desc'];
}
}
The best solution is to replace the loop with the following code:
if (isset($pages[$allyourbase])) {
echo $pages[$allyourbase]['desc'];
} else {
// error handling
}
If I'm reading this right, echo $key['desc']; should be echo $value['desc'];.