Page 1 of 1

[SOLVED] Combine elements to a variable in a json array

Posted: Sun May 15, 2022 2:43 pm
by Joe1234
I don't know if the title fits, so let me know if I need to correct it. Here is what I'm trying to modify:

Code: Select all

		foreach ($results as $result) {
			$json[] = array(
				'id' => $result['product_id'],
				'status' => $result['status'],
				'image' => $this->model_tool_image->resize(($result['image'] ? $result['image'] : 'no_image.png'), 40, 40),
				'name' => strip_tags(html_entity_decode($result['product_id'], ENT_QUOTES, 'UTF-8')));
		}
I'm trying to do edit the 'name' like:

Code: Select all

'name' => strip_tags(html_entity_decode($result['product_id'], ENT_QUOTES, 'UTF-8')))." - ".strip_tags(html_entity_decode($result['sku'], ENT_QUOTES, 'UTF-8')));
But it doesn't work. How do I combine the two elements to a variable in the array.

Thanks.

Re: Combine elements to a variable in a json array

Posted: Sun May 15, 2022 10:07 pm
by mikeinterserv
Try

Code: Select all

'name' => $result['product_id'] . ' - ' . $result['sku'];

Re: Combine elements to a variable in a json array

Posted: Sun May 15, 2022 10:11 pm
by Joe1234
I was just coming back to say I figured it out:

Code: Select all

'name' => strip_tags(html_entity_decode($result['name'].' - '.$result['sku'], ENT_QUOTES, 'UTF-8')));
I'll try your way for the hell of it to see if it works. Thanks.

Re: [SOLVED] Combine elements to a variable in a json array

Posted: Sun May 15, 2022 10:25 pm
by mikeinterserv
If you want name why did you put product_id + Sku instead of name + sku :-)

Code: Select all

'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')) . ' - ' . $result['sku'];
Strip tags and decode are not required for integer. Only text.

Re: [SOLVED] Combine elements to a variable in a json array

Posted: Mon May 16, 2022 1:42 pm
by Joe1234
The product id was an accident, I put it in while I was hacking around trying to figure out the issue and happened to copy that iteration over.

Thanks for the info about the strip tags. Just so I know, is there any downside in keeping it the way I have it vs yours?