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.

user1551496
  • 169
  • 2
  • 12
  • 1
    It seems you forgot to include your question What happens when you try it? Do you get any error messages? – Johan Oct 02 '18 at 08:13
  • 2
    Possible duplicate of [How to loop through an associative array and get the key?](https://stackoverflow.com/questions/1951690/how-to-loop-through-an-associative-array-and-get-the-key) – LuckyStarr Oct 02 '18 at 08:13
  • @LuckyStarr `$this->text` is not an associative array, he just thinks it is. It is a multidimensional array instead – Philipp Maurer Oct 02 '18 at 08: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. – Philipp Maurer Oct 02 '18 at 08:18

2 Answers2

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.

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
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.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149