1

I've been stuck on this for like 2 days, and I know it's much more simpler than I think it is..

I have a foreach loop that goes like this.

foreach($appointments as $appointment){
  
  $list = $appointment['techs']

}

The $appointment['techs']; comes out of the database like this.

a:2:{i:0;s:1:"1";i:1;s:2:"12";}

My question is, how to I get loop through appointments and then show the users that are assigned to each appointment...

The desired output should look like this,

{ resource : 1, event : 1},{ resource : 12, event : 1}

I've literally tried everything! Any help would be greatly appreciated.

4
  • 1
    So you have determined that you have a serialized string. Have you tried unserializing it and then using the array to get the output you want? Commented Mar 3, 2021 at 11:21
  • “I've literally tried everything!” - please STOP stating stuff like this. First of all, you obviously haven’t, and second, this is not helpful at all. Show us what exactly you tried, and give us a proper problem description along with it. Commented Mar 3, 2021 at 12:12
  • @CBroe I didn't want to clutter up my question with all my mistakes, are you saying that the StackOverflow Community would prefer I do that, or is it something that you personally prefer? Commented Mar 4, 2021 at 17:10
  • stackoverflow.com/help/how-to-ask: “Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself.”, “Not all questions benefit from including code, but if your problem is with code you've written, you should include some.” Commented Mar 5, 2021 at 7:16

2 Answers 2

1
$array = [];
$string = 'a:2:{i:0;s:1:"1";i:1;s:2:"12";}';
$arr = (unserialize($string));

foreach ($arr as $item){
    array_push($array, json_encode(['resource'=> $item, 'event'=>1]));
}

$i = 0;
$numItems = count($array);

foreach ($array as $item) {
    if (++$i === $numItems) {
        echo $item;
    }
    else{
        echo $item.',';
    }
}
// Output: {"resource":"1","event":1},{"resource":"12","event":1}
Sign up to request clarification or add additional context in comments.

2 Comments

This is how it's coming out... ["{\"resource\":\"1\",\"event\":1}","{\"resource\":\"12\",\"event\":1}"]["{\"resource\":\"1\",\"event\":1}","{\"resource\":\"12\",\"event\":1}"]["{\"resource\":\"1\",\"event\":1}","{\"resource\":\"12\",\"event\":1}" how I can I get like this. { resource : 1, event : 1},{ resource : 12, event : 1},{ resource : 1, event : 1},{ resource : 12, event : 1},{ resource : 1, event : 1},{ resource : 12, event : 1},
So the output is actually inside the appointments loop. I'm getting this back "{\"resource\":\"1\",\"event\":1}","{\"resource\":\"12\",\"event\":1}"] for each appointment. How can I get it to output without the [] and the \ slashes?
0

The PHP command unserialize it's what you are looking for.

foreach($appointments as $appointment){
  
  $list = unserialize($appointment['techs']);

}

1 Comment

Hi Jimmy, thank you for your reply, but I'm trying to foreach the serialized data inside of the foreach and output it like this..... { resource : 1, event : 1},{ resource : 12, event : 1}

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.