0

I had this i array and i try to loop trough.

Output of:<?php print_r($this);?>

[text] => a:2:{i:0;a:2:{s:5:"value";s:2:"de";s:5:"label";s:70:"<img src="assets/images/e/de-02d3d6ee.jpg"  width="40" height="28" alt="">";}i:1;a:2:{s:5:"value";s:2:"en";s:5:"label";s:70:"<img src="assets/images/7/en-c5c09767.jpg"  width="40" height="28" alt="">";}}

I tried:

<?php $str ='de'; ?>

    <?php foreach ($this->text as $key => $item):?>
        <?php if ($key == $str):?>
            <span class="firstelement hide-on-mobile"><?php echo $item; ?></span>
        <?php endif;?>
    <?php endforeach;?>

I got no output.

4
  • 1
    It seems you forgot to include your question What happens when you try it? Do you get any error messages? Commented Oct 2, 2018 at 8:13
  • 2
    Possible duplicate of How to loop through an associative array and get the key? Commented Oct 2, 2018 at 8:13
  • @LuckyStarr $this->text is not an associative array, he just thinks it is. It is a multidimensional array instead Commented Oct 2, 2018 at 8:16
  • @LuckyStarr And the question you flagged as duplicate has nothing to do with what he wants either, even if it was an associative array. Commented Oct 2, 2018 at 8:18

2 Answers 2

2

First of all, your array seems to be serialized. To access it with code, you would need to unserialize() it first. (In case you did not do this already)

$labels = unserialize($this->text);

The problem you have then is, that you misinterpreted the structure of your array. Your array is built like this:

$labels = [
    0 => [
        'value' => 'de',
        'label' => '<img ... />'
    ],
    1 => [
        'value' => 'en',
        'label' => '<img ... />'
    ]
]

As you can see, it is an array that contains arrays itself. (multidimensional array) When the array gets iterated in your for loop, $key gets assigned the outer keys, so 0 and 1. What you want instead, is to access the inner keys. This can only be done by accessing them directly:

for($labels as $value) {
    if ($value['value'] === $str) {
        ?>
        <span class="firstelement hide-on-mobile"><?= $value['label'] ?></span>
        <?php
    }
}

As you can see, the for loop neither accesses the outer keys anymore, because they are not relevant, as well as accessing the inner array's fields with $value['value'] and $value['label'] directly.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, except that the serialisation appears to have been incorrectly done when the data was serialised. See my answer for an explanation
Thats Odd, I get errors when trying to unserialize. Never mind I had a great time playing with it :)
I dont got any error :) $this->text is something i got for a cms i use.
2

Your $this->text is the result of a serialize() run on an PHP array presumably to store it on a table column. It must therefore be unserialized() back to a PHP array before you can make any use of it as a PHP array.

However, when I tried that there also appears to be errors in the original serialisation so as it is $this->text is corrupt and unusable as it stands.

Having copied your serialized string from your question, when trying to unserialise it I get this error

$x = 'a:2:{i:0;a:2:{s:5:"value";s:2:"de";s:5:"label";s:70:"<img src="assets/images/e/de-02d3d6ee.jpg"  width="40" height="28" alt="">";}i:1;a:2:{s:5:"value";s:2:"en";s:5:"label";s:70:"<img src="assets/images/7/en-c5c09767.jpg"  width="40" height="28" alt="">";}}';
$y = unserialize($x);
print_r($y);

Notice: unserialize(): Error at offset 123 of 256 bytes in D:\PHP-SOURCE\tst.php on line 4

This is because this part of the string,

s:70:"<img src="assets/images/e/de-02d3d6ee.jpg"  width="40" height="28" alt="">"

does not correctly count the following string, as there are 74 characters in the string

"<img src="assets/images/e/de-02d3d6ee.jpg"  width="40" height="28" alt="">"

If we fix that by correcting the count to 74 like this

s:74:"<img src="assets/images/e/de-02d3d6ee.jpg"  width="40" height="28" alt="">"

And rerunning the unserialise

$x = 'a:2:{i:0;a:2:{s:5:"value";s:2:"de";s:5:"label";s:74:"<img src="assets/images/e/de-02d3d6ee.jpg"  width="40" height="28" alt="">";}i:1;a:2:{s:5:"value";s:2:"en";s:5:"label";s:70:"<img src="assets/images/7/en-c5c09767.jpg"  width="40" height="28" alt="">";}}';
$y = unserialize($x);
print_r($y);

There is another similiar error in the string counts

Notice: unserialize(): Error at offset 248 of 256 bytes in D:\PHP-SOURCE\tst.php on line 8

s:70:"<img src="assets/images/7/en-c5c09767.jpg"  width="40" height="28" alt="">"

So if we also fix that error by making the count 74 and rerun

$x = 'a:2:{i:0;a:2:{s:5:"value";s:2:"de";s:5:"label";s:74:"<img src="assets/images/e/de-02d3d6ee.jpg"  width="40" height="28" alt="">";}i:1;a:2:{s:5:"value";s:2:"en";s:5:"label";s:74:"<img src="assets/images/7/en-c5c09767.jpg"  width="40" height="28" alt="">";}}';
$y = unserialize($x);
print_r($y);

We get a correctly unserialised array like this

Array
(
    [0] => Array
        (
            [value] => de
            [label] => <img src="assets/images/e/de-02d3d6ee.jpg"  width="40" height="28" alt="">
        )

    [1] => Array
        (
            [value] => en
            [label] => <img src="assets/images/7/en-c5c09767.jpg"  width="40" height="28" alt="">
        )

)

So in short, if you got this data from a database You will need to look at the code that is serialising this data before it is placed on the database to work out why it is Incorectly serialising the array in the first place.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.