I have a file that use's this code
<?php echo $block->getStoreName(); ?>
to output the following on the website
First Second Third
However I only want to only output the Third element of the string above, First Second do not change they always stay the same.
Third
I'm using this code to retrieve the Third part of the string
echo explode('First Second', $block->getStoreName())[1];
Its throwing up an error.
Error filtering template: Notice: Undefined offset: 2 in
/home/xyz/m230.xyz.com/app/code/Vendor/Siteinfo/view/frontend/templates/storename.phtml
on line 1
Line 1 in storename.phtml is
<?php echo explode('First Second', $block->getStoreName())[1]; ?>
I'm unsure if thats the correct way of doing it.
UPDATE - Have attempted a clearer explanation of what I'm trying to achieve.
echo explode(' ', $block->getStoreName())[2];
This should do the trick.
// checks if string has "Unique"
if(mb_strpos($block->getStoreName(),'Unique') !== false){
// prints "Unique"
echo "Unique";
}
My print_r($view) function yields:
View Object
(
[viewArray:View:private] => Array
(
[title] => Projet JDelage
)
)
1 <--------------
What does the "1" at the end mean? The PHP manual isn't very clear on how to parse the output of print_r.
You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.
print_r called with one argument (or with its second argument set to false), will echo the representation of its parameter to stdout. If it does this, it returns TRUE. Thus if you echo print_r($foo) you will print the contents of foo, followed by a string representation of the return value (which is 1).
When using print_r to return, than than output/print the value, pass the 2nd parameter which defines return as true.
echo print_r($view, true);
This is useful if you want to save the results to a variable or concatenate with another string.
$var = 'The array is: ' . print_r($view, true);
<?php
$data="98.8degrees";
(double)$data;
(int)$data;
(string)$data;
echo $data;
?>
I was surprised/confused when the actual output was 98.8 degrees
I thought when $data uses (double), it converts to 98.8.
Then when moving to (int), it becomes 98 and forth
But I guess my analogy is wrong. Can someone explain to me how the output became like that?
Doing
(double)$data;
(int)$data;
(string)$data;
just return the double, int and string values but they don't change. To change them, you need to do assign the return values to the actual variable like this:
$data = (double)$data;
$data = (int)$data;
$data = (string)$data;
You keep casting it, but turn around and discard the results. The value in $data never changes. Try the settype() function.
What you probably meant to do was:
<?php
$data="98.8degrees";
$data=(double)$data;
$data=(int)$data;
echo $data;
?>
If you don't keep the result of your operations, you won't see the result of your operations...
Oh - and I have no idea what you hoped to achieve with the
(string)$string;
statement, since it's unclear if/how you ever defined $string...
I have the following structure:
$par4 = json_decode($source_code)->$par1->$par2->$par3;
$par5 = $par4[0]->attributes->attribute[1]->value;
where par1, par2 and par3 are strings. How do I chain the par4 and par5 on one line.
This does not work because of the array / object nesting I guess:
json_decode($source_code)->$par1->$par2->$par3[0]->attributes->attribute[1]->value;
Here's the error:
Undefined property: stdClass::$o
What about
$par5 = current(json_decode($source_code)->$par1->$par2->$par3)->attributes->attribute[1]->value;
This works if you are always need in the first (0th) value of the array.
You can also create a function that returns the nth-value:
function third_value($arr) { return $arr[2]; }
$par5 = third_value(json_decode($source_code)->$par1->$par2->$par3)->attributes->attribute[1]->value;
I'm not sure, what you really need but try using {} to highlight what you need
{json_decode($source_code)->$par1->$par2->$par3}[0] // I think this is right
json_decode($source_code)->$par1->$par2->${par3[0]}
json_decode($source_code)->$par1->$par2->{$par3[0]}
Can this be done in 1 line with PHP?
Would be awesome if it could:
$out = array("foo","bar");
echo $out[0];
Something such as:
echo array("foo","bar")[0];
Unfortunately that's not possible. Would it be possible like this?
So I can do this for example in 1 line:
echo array(rand(1,100), rand(1000,2000))[rand(0,1)];
So let's say I have this code:
switch($r){
case 1: $ext = "com"; break;
case 2: $ext = "nl"; break;
case 3: $ext = "co.uk"; break;
case 4: $ext = "de"; break;
case 5: $ext = "fr"; break;
}
That would be much more simplified to do it like this:
$ext = array("com","nl","co.uk","de","fr")[rand(1,5)];
Why not check out the array functions on the PHP site?
Well, if you're picking a random element from the array, you can use array_rand().
$ext = array_rand(array_flip(array("com","nl","co.uk","de","fr")));
echo array_rand(array_flip(array('foo', 'bar')));
array flip takes an array and swaps the keys with the values and vice versa. array_rand pulls a random element from that array.
You can use a shorthand form to keep things on one line and avoid creating an array that will never be used again.
echo rand(0,1) ? rand(1,100) : rand(1000,2000);
Yes, list a PHP language construct that allows the syntax below.
list( $first_list_element ) = array( "foo", ..... );
EDIT:
Still Yes, Missed the echo. Reset will return the first array item, which might not always be index 0, but if you create an array normally it will.
echo reset( array( "foo",... ) );
EDIT AGAIN:
After you updated your question I see that you want something that PHP just can't do well. I personally think it's a syntax design error of the PHP language/interpreter. If you just mean one-line you could do.
$array = array( .... ); echo $array[0];
I think your example may not be the best. The real syntax limitation here is that one can not immediately perform array access on the returned value of a function call, as in,
do_something_with(explode(',', $str)[0]);
And you pretty much can't get around it. Assign to a variable, then access. It's a silly limitation of PHP, but it's there.
You can technically do,
function array_access($array, $i) {
return $array[$i];
}
do_something_with(array_access(explode(',', $str), 0));
But please don't. It's even uglier than an extra variable asignment.
(Given the edit to the question, this no longer a sufficient answer. However, I will leave it up for reference.)
Like #Matchu's answer, this addresses the more general case, ie you have an array value that came from somewhere, be it a function call, an instantiated array, whatever. Since the return value from an arbitrary function is the least specific case, I'll use a call to some_function() in this example.
$first_element = current(array_slice(some_function(), 0, 1));
So you could randomize it with
$random_element = current(array_slice(some_function(), rand(0,1), 1));
but in that case (as in many in php) there is a more specialized function for that; see #animuson's answer.
edited
changed array_shift() call to current() call: this is more efficient, because array_shift() will modify the intermediate array returned by array_slice().
This is being discussed in the internals mailing list right now. You might want to check it: http://marc.info/?l=php-internals&m=127595613412885&w=2
print $a[ array_rand($a = array('com','nl','co.uk','de','fr')) ];